Tag Archives: TestNG framework

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;);
 }
}

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



how to skip test in testng ? SkipException (TestNG)

SkipException help skip testcase / test-suite and give appropriate exception on testing result.

Lets see Example:

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("");
 System.out.println("Click on tag page of wordpress-------------");
 System.out.println("");
 }

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

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

 }

 @AfterMethod
 public void WriteStatusToExcel(){

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

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

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

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

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

TestNG HTML report

SkipException

SkipException

Reference: SkipException (TestNG)

how to ignore test in testng?

This “Ignored” means the method is not ready to test, the TestNG engine will just bypass this method.

 

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_tag {
 @BeforeClass
 public void NavigateTotagPage(){
 System.out.println("");
 System.out.println("Click on tag page of wordpress-------------");
 System.out.println("");
 }

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

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

 }

 @AfterMethod
 public void WriteStatusToExcel(){

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



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

@Test(enabled=false)
 public void EditTag(){
 System.out.println(" Testing Edit Tag");
 }

@Test(enabled=false)
 public void ViewTag(){
 System.out.println(" Testing View Tag");
 }

@Test(enabled=false)
 public void DeleteTag(){
 System.out.println(" Testing Delete Tag");
 }
}

 

EditTag, ViewTag, DeleteTag are not going to execute.

 

Lets check testNG result pane

 ignore test

ignore test

 

Check TestNG HTML report

dependsonmethods testng example

Here is an example of a dependency:

@Test
public void serverStartedOk() {}

@Test(dependsOnMethods = { "serverStartedOk" })
public void method1() {}

 

Real Example for dependsonmethods 

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("");
 System.out.println("Click on post page of wordpress-------------");
 System.out.println("");
 }

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

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

 }

 @AfterMethod
 public void WriteStatusToExcel(){

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

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

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

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

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

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

 

How to configure TestNG with testng.xml for Selenium?

We will make TestNG.xml for following Scenario

Test_Base.java
Login to WordPress
Logout from WordPress

Test_post.java
Add New post
Edit post
View post
Quick Edit post
Trash post

Test_Category.java
Add new Category
Edit Category
View Category
Delete Category
Quick Edit Category

Test_tag.java
Add new tag
Edit tag
View tag
Delete tag

Go to Eclipse

Select package in which we want to execute all classes using TestNG.xml

Create new file named testng.xml and add following code into it

 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Suite to test WordPress Post Tag Category">

<test name="testing Test_Base">
 <classes>
 <class name="testWordpress.Test_Base" ></class>
 </classes>
</test>

&nbsp;

<test name="testing post">
 <classes>
 <class name="testWordpress.Test_post" ></class>
 </classes>
</test>

<test name="testing Tag">
 <classes>
 <class name="testWordpress.Test_tag" ></class>
 </classes>
</test>

<test name="testing Category">
 <classes>
 <class name="testWordpress.Test_Category" ></class>
 </classes>
</test>

</suite>

Now we are Ready to RUN

select testng.xml and run it with testNG

Check Output in Eclipse console:

Login to wordpress
Click on post page of wordpress-------------
Verify testcase execution status from excel
 Testing Add New post
Write Testcase Pass/Fail/Skip status to Excel / HTML report
Verify testcase execution status from excel
Edit post Testing
Write Testcase Pass/Fail/Skip status to Excel / HTML report
Verify testcase execution status from excel
Quick Edit post testing
Write Testcase Pass/Fail/Skip status to Excel / HTML report
Verify testcase execution status from excel
Trash post testing
Write Testcase Pass/Fail/Skip status to Excel / HTML report
Verify testcase execution status from excel
View post testing
Write Testcase Pass/Fail/Skip status to Excel / HTML report
Write Pass/Fail/Skip status to Excel / HTML report

Click on tag page of wordpress-------------
Verify testcase execution status from excel
 Testing Add new Tag
Write Testcase Pass/Fail/Skip status to Excel / HTML report
Verify testcase execution status from excel
 Testing Delete Tag
Write Testcase Pass/Fail/Skip status to Excel / HTML report
Verify testcase execution status from excel
 Testing Edit Tag
Write Testcase Pass/Fail/Skip status to Excel / HTML report
Verify testcase execution status from excel
 Testing View Tag
Write Testcase Pass/Fail/Skip status to Excel / HTML report
Write Pass/Fail/Skip status to Excel / HTML report

Click on Category page of wordpress-------------
Verify testcase execution status from excel
 Testing Add new Category
Write Testcase Pass/Fail/Skip status to Excel / HTML report
Verify testcase execution status from excel
 Testing Delete Category
Write Testcase Pass/Fail/Skip status to Excel / HTML report
Verify testcase execution status from excel
 Testing Edit Category
Write Testcase Pass/Fail/Skip status to Excel / HTML report
Verify testcase execution status from excel
 Testing Quick Edit Category
Write Testcase Pass/Fail/Skip status to Excel / HTML report
Verify testcase execution status from excel
 Testing View Category
Write Testcase Pass/Fail/Skip status to Excel / HTML report
Write Pass/Fail/Skip status to Excel / HTML report

Logout from wordpress

===============================================
Suite to test WordPress Post Tag Category
Total tests run: 14, Failures: 0, Skips: 0
===============================================

Check TestNG result window:

and at the end lets verify testNG HTML report

  1. Select Java project and refresh this project
  2. New folder test-output will added to java project
  3. open index.html file in web-browser

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

Solutions365

.net, WCF, Unity, IOC, SQL

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