Category Archives: TestNG testing framework

* Selenium Automation Learning Path *

Selenium learning Path

Selenium learning Path

Advertisements

how to use testng annotation @BeforeTest? @BeforeTest (TestNG)

@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.

 &lt;!DOCTYPE suite SYSTEM &quot;http://testng.org/testng-1.0.dtd&quot;&gt;

&lt;suite name=&quot;Suite1&quot;&gt;

&lt;test name=&quot;testing Test_Base&quot;&gt;
 &lt;classes&gt;
 &lt;class name=&quot;testWordpress.Test_Base&quot; &gt;&lt;/class&gt;
 &lt;/classes&gt;
&lt;/test&gt;


&lt;test name=&quot;testing post&quot;&gt;
 &lt;classes&gt;
 &lt;class name=&quot;testWordpress.Test_post&quot; &gt;&lt;/class&gt;
 &lt;/classes&gt;
&lt;/test&gt;

&lt;test name=&quot;testing Tag&quot;&gt;
 &lt;classes&gt;
 &lt;class name=&quot;testWordpress.Test_tag&quot; &gt;&lt;/class&gt;
 &lt;class name=&quot;testWordpress.Test_Category&quot; &gt;&lt;/class&gt;
 &lt;/classes&gt;
&lt;/test&gt;


&lt;/suite&gt;

package testWordpress;

import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
public class Test_Base {

@BeforeSuite
 public void LogintoWordPress(){
 System.out.println(&quot;Login to wordpress&quot;);
 System.out.println(&quot;&quot;);
 }

 @AfterSuite
 public void LogoutfromWordPress(){
 System.out.println(&quot;&quot;);
 System.out.println(&quot;Logout from wordpress&quot;);
 }
}

package testWordpress;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Test_Category {
 @BeforeClass
 public void NavigateToCategoryPage(){
 System.out.println(&quot;&quot;);
 System.out.println(&quot;Click on Category page of wordpress-------------&quot;);
 System.out.println(&quot;&quot;);

 }

 @AfterClass
 public void writeStatusToExcel(){
 System.out.println(&quot;&quot;);
 System.out.println(&quot;Write Pass/Fail/Skip status to Excel / HTML report&quot;);
 }

 @BeforeMethod
 public void VerifyExecutionStatus(){
 System.out.println(&quot;&quot;);
 System.out.println(&quot;Verify testcase execution status from excel&quot;);
 ;

 }

 @AfterMethod
 public void WriteStatusToExcel(){

 System.out.println(&quot;Write Testcase Pass/Fail/Skip status to Excel / HTML report &quot;);
 System.out.println(&quot;&quot;);
 }

 @BeforeTest
 public void defineVariables(){
 System.out.println(&quot;-----------------------&quot;);
 System.out.println(&quot;@BeforeTest testing&quot;);
 }

 @Test
 public void AddnewCategory(){
 System.out.println(&quot; Testing Add new Category&quot;);
 }

 @Test
 public void EditCategory(){
 System.out.println(&quot; Testing Edit Category&quot;);
 }

 @Test
 public void ViewCategory(){
 System.out.println(&quot; Testing View Category&quot;);

 }

 @Test
 public void DeleteCategory(){
 System.out.println(&quot; Testing Delete Category&quot;);
 }

 @Test
 public void QuickEditCategory(){
 System.out.println(&quot; Testing Quick Edit Category&quot;);
 }
}

package testWordpress;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Test_post {

@BeforeClass
 public void NavigateToPostPage(){
 System.out.println(&quot;&quot;);
 System.out.println(&quot;Click on post page of wordpress-------------&quot;);
 System.out.println(&quot;&quot;);
 }

 @AfterClass
 public void writeStatusToExcel(){
 System.out.println(&quot;&quot;);
 System.out.println(&quot;Write Pass/Fail/Skip status to Excel / HTML report&quot;);
 }

 @BeforeMethod
 public void VerifyExecutionStatus(){
 System.out.println(&quot;&quot;);
 System.out.println(&quot;Verify testcase execution status from excel&quot;);

 }

 @AfterMethod
 public void WriteStatusToExcel(){

 System.out.println(&quot;Write Testcase Pass/Fail/Skip status to Excel / HTML report &quot;);
 System.out.println(&quot;&quot;);
 }

 @Test
 public void AddNewPost(){
 System.out.println(&quot; Testing Add New post&quot;);
 }

 @Test(dependsOnMethods = { &quot;AddNewPost&quot; })
 public void EditPost(){
 System.out.println(&quot;Edit post Testing&quot;);
 }

@Test(dependsOnMethods = { &quot;EditPost&quot; })
 public void ViewPost(){
 System.out.println(&quot;View post testing&quot;);
 }

 @Test(dependsOnMethods = { &quot;ViewPost&quot; })
 public void QuickEditPost(){
 System.out.println(&quot;Quick Edit post testing&quot;);
 }

 @Test(dependsOnMethods = { &quot;QuickEditPost&quot; })
 public void TrashPost(){
 System.out.println(&quot;Trash post testing&quot;);
 }
}

package testWordpress;

import org.testng.SkipException;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Test_tag {
 @BeforeClass
 public void NavigateTotagPage(){
 System.out.println(&quot;&quot;);
 System.out.println(&quot;Click on tag page of wordpress-------------&quot;);
 System.out.println(&quot;&quot;);
 }

 @AfterClass
 public void writeStatusToExcel(){
 System.out.println(&quot;&quot;);
 System.out.println(&quot;Write Pass/Fail/Skip status to Excel / HTML report&quot;);
 }

 @BeforeMethod
 public void VerifyExecutionStatus(){
 System.out.println(&quot;&quot;);
 System.out.println(&quot;Verify testcase execution status from excel&quot;);

 }

 @AfterMethod
 public void WriteStatusToExcel(){

 System.out.println(&quot;Write Testcase Pass/Fail/Skip status to Excel / HTML report &quot;);
 System.out.println(&quot;&quot;);
 }

 @Test
 public void AddnewTag(){
 System.out.println(&quot; Testing Add new Tag&quot;);
 }

@Test
 public void EditTag(){
 System.out.println(&quot; Testing Edit Tag&quot;);
 }

@Test
 public void ViewTag(){
 System.out.println(&quot; Testing View Tag&quot;);
 throw new SkipException(&quot;Skipping - This is not ready for testing &quot;);
 }

@Test
 public void DeleteTag(){
 System.out.println(&quot; Testing Delete Tag&quot;);
 }
}

Difference between BeforeTest and BeforeMethod annotations in testNG ??

Please Refer this blog entry

Please Refer this blog entry

How to download and configure apache ant?

  1. Go to google and type download Apache ant
  2. Select link http://ant.apache.org/bindownload.cgi
  3. Download file named like apache-ant-1.8.4-bin.zip
  4. Extract the zip file into a directory structure of your choice. Set the “ANT_HOME” environment variable to this location and include the “ANT_HOME/bin” directory in your path.
  5. Make sure that also the JAVA_HOME environment variable is set to the JDK. This is required for running Ant.
  6. Check your installation by opening a command line and typing “ant -version” into the commend line. The system should find the command ant and show the version number of your installed ant.

What is apache ant?

Apache Ant is a software tool for automating software build processes. It is similar to Make but is implemented using the Java language, requires the Java platform, and is best suited to building Java projects.

 

Apache Ant (Another Neat Tool) is a general purpose build tool. A build tool can be used to automate certain repetitive tasks, e.g. compiling source code, running software tests and creating jar files and Javadoc.

 

Apache Ant is used primarily used for building and deploying Java projects.

 

A Java build process typically includes:

 

the compilation of the Java source code into Java bytecode

creation of the .jar file for the distribution of the code

creation of the Javadoc documentation

Apache Ant uses a xml file for its configuration. This file is usually called “build.xml”. Ant builds are based on three blocks: tasks, targets and extension points.

 

A task in Ant is a unit of work which should be performed. Tasks are small, atomic steps, for example compile source code or create Javadoc. Tasks can be grouped into targets.

 

A target is a group of tasks which can be directly invoked via Ant. Tasks can also specify their dependencies. If a target depends on other targets then Ant will automatically perform these task first and so on and so on.

 

For example if target A depends on target B, Ant will first perform target B and then target A.

 

In your build.xml you can also specify the default target. Apache Ant will execute this target, if no explicit target is specified.

Compare TestNG HTML report – TestNG xslt Reports

This slideshow requires JavaScript.

what is the difference between assert and verify in selenium ?

When an “assert” fails, the test will be aborted.

Where if a “verify” fails, the test will continue executing and logging the failure.

So when Assertion fails all test steps after that line of code are skipped 😦

To resolve this we write this command in try catch block 🙂

Example 1 :


package testWordpress;

import org.testng.Assert;
import org.testng.annotations.Test;

public class AssertionsTest {

 @Test
 public void testMultiply() {
 System.out.println(&quot;Before Error &quot;);
 Assert.assertEquals(21, multiply(10, 5));
 System.out.println(&quot;After Error &quot;);
 }

 public int multiply(int x, int y) {
 return x / y;
 }

}

System.out.println(“After Error “); will never executed. 😦

Please check eclipse console to conform.

We can resolve this problem by using Try-Catch block.

How?

Lets check out following code

 package testWordpress;

import org.testng.Assert;
import org.testng.annotations.Test;

public class AssertionsTest {

 @Test
 public void testMultiply() {
 System.out.println(&quot;Before Error &quot;);

 try{
 Assert.assertEquals(21, multiply(10, 5));
 }catch(Throwable t){
 // recovered
 // java code to fail the test
 System.out.println(&quot;After Error &quot;);
 }

 }

 public int multiply(int x, int y) {
 return x / y;
 }
}

Reference Links: Link 1 Link 2 Link 3 Link 4

how to use assertion in testng ? – Assert (TestNG)

Refer Assert (TestNG) 

 

 

Sample Method Detail

assertTrue
assertFalse
assertEquals

 

Example 1 : what happen when assertion fail using  assertEquals ?

 

package testWordpress;

import org.testng.Assert;
import org.testng.annotations.Test;

public class AssertionsTest {

 @Test
 public void testMultiply() {

 Assert.assertEquals(50, multiply(10, 5));
 }


 public int multiply(int x, int y) {
 return x / y;
 }

}

 

Run above code using testNG and check TestNG result pane

assertEquals

assertEquals

 

 

Example 2 : what happen when assertion success using  assertEquals ?

 

package testWordpress;

import org.testng.Assert;
import org.testng.annotations.Test;

public class AssertionsTest {

 @Test
 public void testMultiply() {

 Assert.assertEquals(2, multiply(10, 5));
 }


 public int multiply(int x, int y) {
 return x / y;
 }
}

Condition when success

Condition when success

Example 3: what happen when assertion success using  assertTrue?

package testWordpress;

import org.testng.Assert;
import org.testng.annotations.Test;

public class AssertionsTest {

package testWordpress;

import org.testng.Assert;
import org.testng.annotations.Test;

public class AssertionsTest {

 @Test
 public void testMultiply() {

 Assert.assertTrue(multiply(10, 5)!=20, "Sorry");
 }


 public int multiply(int x, int y) {
 return x / y;
 }

}

 

what happen when assertion success using  assertTrue

what happen when assertion success using assertTrue

 

 

Example 4: what happen when assertion fail using  assertTrue?

package testWordpress;

import org.testng.Assert;
import org.testng.annotations.Test;

public class AssertionsTest {

 @Test
 public void testMultiply() {

 Assert.assertTrue(multiply(10, 5)!=2, "Sorry");
 }


 public int multiply(int x, int y) {
 return x / y;
 }

}
what happen when assertion fail using  assertTrue

what happen when assertion fail using assertTrue

 

Example 4: what happen when assertion success using  assertFalse?

 

package testWordpress;

import org.testng.Assert;
import org.testng.annotations.Test;

public class AssertionsTest {

 @Test
 public void testMultiply() {

 Assert.assertFalse(multiply(10, 5)>10, "Caught");
 }


 public int multiply(int x, int y) {
 return x / y;
 }
}

what happen when assertion success using  assertFalse

what happen when assertion success using assertFalse

 

Lets add TestCase with assertion in testng.xml and verify testNG html report

 

<test name="test4">
 <classes>
 <class name="testWordpress.AssertionsTest" ></class>
 </classes>
</test>

 

 

How to Parameterized Test using xml file in testng ?

Check This site

how to parameterized test in TestNG using @dataprovider annotation ?

mapping of complex parameter types to a test method with  @DataProvider annotation

See example:

 public class ParameterizedLoginTest {

@Test(dataProvider = "parameterIntTestProvider")
 public void parameterIntTest(String UserName, String Password) {
 System.out.println("UserName " + UserName);
 System.out.println("Password " + Password);
 }

 //This function will provide the parameter data
 // Selenium framework - read the data from xls file and put it in Object array
 // rows - number of time test has to be repeated
 // cols - number of parameters in test data
 @DataProvider(name = "Data-Provider-Function")
 public Object[][] parameterIntTestProvider() {

 Object[][] data = new Object[3][2];

 data[0][0] = "UserName1";
 data[0][1] = "UserName1@123";

 data[1][0] = "UserName2";
 data[1][1] = "UserName2@123";

 data[2][0] = "UserName2";
 data[2][1] = "UserName2@123";

 return data;
 }
}

Run this and Checkout TestNG result pane

Now Add new tag in testng.xml


<test name="test3">
 <classes>
 <class name="testWordpress.ParameterizedLoginTest" ></class>
 </classes>
</test>

 

And run TestNG.xml and see testng HTML report

 

parameterized test

parameterized test



Pupunzi

For a better web

Tech

News and reviews from the world of gadgets, gear, apps and the web

VentureBeat

News About Tech, Money and Innovation

Digital Inspiration

Tips and tutorials around all things tech.

Thinkwareglobal's Blog

Just another WordPress.com weblog

Kenazari's Blog

Just another WordPress.com weblog

Richfowler1's Blog

Just another WordPress.com weblog

Romaicus's Blog

Just another WordPress.com weblog

botlaguduri

Just another WordPress.com site

chandanag89

Just another WordPress.com site

meghshetty

Just another WordPress.com site

digitalmediaexpert

digital media expert blog

Thinking in Silverlight

Silverlight/WPF/Windows 8 Store App

Different Journeys

Let's make life easier!

LAW RESOURCE INDIA

LEGAL RESOURCE CENTRE / COURT JUDGMENTS / LEGAL ARCHIVES

BookConnect

Promoting Indian language Books

Actively Lazy

Software, crafted with passion

SAPonPower

An ongoing discussion about SAP infrastructure

SAP Career

Your Career Mentor

hrsapcertification

Just another WordPress.com site

The secret product manager

Thoughts about product management, SUSE, Linux, ARM processors, Cloud, AIX, Power Systems, and other things that interest me

Sap Certification Material . Com

Quality store for sap software and courses

Simon Les Flex's Blog

SAP Process Integration Specialist - NOW WE CAN DO BETTER!

Oracle Technologies Primer

An Oracle Fusion Middleware and iPaaS blog!

Gilberto Holms

Java, Middleware, SOA Architecture Blog

The Skeleton

Java code skeletons, tips and pointers

J@n van Zoggel

integration, innovation & random technology stuff

The Pragmatic Integrator

Hints and tips from a pragmatic integration specialist

briskwalk

The pace of life..

Optimalbg's Blog

Just another WordPress.com site

IT Jobs in USA

Keep the track of this Blog if you are looking out for some good Opportunities to work in USA....

Referral Jobs | Right Place to Get a Job

Hurry you are one Click away to get a job - Fresher Jobs for 2012, 2013 fresher’s and also high paid jobs for well Experienced IT professionals on Referal jobs.

eccinternational.wordpress.com/

ISO Consulting | ISO 27001 | ISO 20000 | CMMI | BCM | GRI Sustainability Reporting & Assurance | eLearning | Trainings | Philippines

Michael Korn's Blog

Musings on Life, Career, Faith and Technology

WORKINGWITHQTP

Just another WordPress.com weblog

Topics

.Net Core, AspNetCore, Entity Framework, Entity Framework Core, AspNetIdentity, AspNetIdentityCore, Unit Test, WCF, Workflows

SQA and Testers Blog

Just another WordPress.com weblog

Expertqtp's Weblog

Just another WordPress.com weblog

Sai Chamarthi

Experienced Testing Professional

saxenavinay

A great WordPress.com site

Suresh4qtp's Blog

Just another WordPress.com site

jonah95hill

This WordPress.com site is the cat’s pajamas

It's always something...

Just another WordPress.com weblog

Faculty of Information Technology - Lê Ngọc Tiến

Be Yourself! Simplify Our Goals! Nothing is Impossible!

Anti-Malware Testing

Thoughts on security product testing past and present

Methods & Tools

Practical knowledge for the software developer, tester and project manager