TestNG Testing Framework for Selenium


TestNG Testing Framework for Selenium Part 1

I) Introduction to TestNG

II) Install TestNG and write first TestNG Test Case

III) Create multiple Test Cases and Run

IV) Execute multiple programs / classes using XML
--------------------------------------------------------
I) Introduction to TestNG
In Selenium using Java there are two Testing frameworks available,

i) JUnit

ii) TestNG
------------------------------
TestNG Testing Framework

> TestNG is a Testing framework designed to simplify a broad range of testing needs, from Unit Testing to System Testing.

> Initially developed for Unit Testing, now used for all kinds of Testing.

> TestNG is an open source framework, where NG stands for next generation.

> TestNG inspired from Junit(Java platform) and NUnit (.NET platform), but introduced some
   new functionalities that make it more powerful and easier to use.
-------------------------------------------------------------------
Advantages of TestNG:
1) TestNG Annotations are easy to create Test Cases.

2) Test Cases can be grouped and prioritized more easily.

3) Supports Parameterization.

4) Supports Data driven Testing using DataProviders.

5) Executes multiple programs / classes using XML.

6) Generates HTML Reports

7) Parallel Test Execution is possible.

8) Supports integration with other tools and plug ins (Eclipse IDE, build tools like ANT, Maven etc...)
------------------------------------------------------------------
Note: using TestNG we can create Test Cases, group Test Cases, prioritize Test Case, execute Test Cases and generate Test Reports.
----------------------------------------------------
II) Install TestNG and write first TestNG Test Case

In Eclipse IDE:

> Help menu
> Install New Software
> Click Add
> Enter Name as "TestNG"
> Enter URL as "http://beust.com/eclipse/"
> Select "TestNG"
> Next > Next > accept the Agreement > Finish
-----------------------------------------------
Write first TestNG Test Case:

Manual Test Case:

Test Case name: Verify Title of the Page

Test Steps:

i) Launch the Browser
ii) Navigate to gmail.com

Verification point:

Capture the Page Title and Compare with expected.

Expected = Gmail

Actual =

Status =
-----------------------------------------------------
TestNG Test Case

public class VerifyPagetitle {
@Test
public void verifyTitle(){
WebDriver driver = new FirefoxDriver();
driver.get("https://www.gmail.com");

String pageTitle = driver.getTitle();
Assert.assertEquals(pageTitle, "Gmail");
driver.close();
}
}
-------------------------------------------------------
Note:

1) main method is not used for TestNG Programs.

2) TestNG program contains only methods that contain @Test Annotations.

3) If we don't write @Test Annotation then the method is not going to be executed.
---------------------------------------------------------------------------------
III) Write multiple Test Cases in a Program

public class VerifyPagetitle {
@Test
public void verifyTitle(){
Assert.assertEquals("Gmail", "Gmail");
}
@Test
public void abcd(){
Assert.assertEquals("Yahoo", "Yahoo");
}
@Test
public void xyz(){
Assert.assertEquals("abcde", "abcde");
}
}

Test Cases as per Program:

verifyTitle
abcd
xyz

Execution Flow:

abcd
verifyTitle
xyz

Note: TestNG Test Cases are execute in Alphabetical order

If you want to control the Test execution process then use priority attribute.


priority attribute Example:

public class VerifyPagetitle {
@Test(priority = 1)
public void verifyTitle(){
Assert.assertEquals("Gmail", "Gmail");
}
@Test (priority = 2)
public void abcd(){
Assert.assertEquals("Yahoo", "Yahoo");
}
@Test (priority = 3)
public void xyz(){
Assert.assertEquals("abcde", "abcde");
}
}
-----------------------------------------------------------
Instead of priority attribute we can use dependsOnMethods attribute
------------------------------------------------------------------
Example:

Using priority attribute

public class VerifyPagetitle {
@Test(priority = 1)
public void login(){
System.out.println("Login Successful");
}
@Test(priority = 4)
public void logout(){
System.out.println("Logout Successful");
}
@Test(priority = 2)
public void search(){
System.out.println("Search Successful");
}
@Test(priority = 3)
public void advancedSearch(){
System.out.println("Advanced Search Successful");
}
}
-----------------------------------------
Using dependsOnMethods attribute

public class VerifyPagetitle {
@Test
public void login(){
System.out.println("Login Successful");
}
@Test(dependsOnMethods = {"advancedSearch"})
public void logout(){
System.out.println("Logout Successful");
}
@Test(dependsOnMethods = {"login"})
public void search(){
System.out.println("Search Successful");
}
@Test(dependsOnMethods = {"search"})
public void advancedSearch(){
System.out.println("Advanced Search Successful");
}
}
------------------------------------------------------
public class VerifyPagetitle {
@Test
public void login(){
Assert.assertEquals("xyz", "xyz");
}
@Test(dependsOnMethods = {"advancedSearch"}, alwaysRun=true)
public void logout(){
Assert.assertEquals("abcder", "abcder");
}
@Test(dependsOnMethods = {"login"})
public void search(){
Assert.assertEquals("abcd", "abcd");
}
@Test(dependsOnMethods = {"search"})
public void advancedSearch(){
Assert.assertEquals("abcd", "xyz");
}
}
--------------------------------------------------
If we use priority attribute then all Test cases can be executed, no skips

If we use dependsOnMethods attribute then it will skip the test case whenever dependsOnMethods
Test case fails, if you want execute the test case forcebly then use alwaysRun attribute.
---------------------------------
Note:

If there is no functionality dependency then use priority attribute.

If there is any functionality dependency then use dependsOnMethods attribute
-----------------------------------------
Scenario 1: No functionality dependency - Use priority attribute

Scenario 2: Functionality dependency - Use dependsOnMethods attribute

Scenario 3: Functionality dependency for some Test Cases only - Use dependsOnMethods and alwaysRun attributes
-----------------------------------------------------------
Test Cases
 

i) Launch Browser
ii) Verify Gmail home Page Title
iii) Verify Yahoo home page Title
iv) Close Browser

TestNG Program:

public class TestngProgram {
public WebDriver driver;
@Test(priority =1)
public void launchBrowser(){
driver = new FirefoxDriver();
}
@Test(priority =2)
public void verifyGmailpage(){
driver.get("https://www.gmail.com");
Assert.assertEquals("Gmail", driver.getTitle());
}
@Test(priority =3)
public void verifyYahoopage(){
driver.get("https://in.yahoo.com/");
Assert.assertEquals("Yahoo", driver.getTitle());
}
@Test(priority =4)
public void closeBrowser(){
driver.close();   
}
}
--------------------------------------------------------
public class TestngProgram {
public WebDriver driver;
@Test
public void launchBrowser(){
driver = new FirefoxDriver();
}
@Test(dependsOnMethods = {"launchBrowser"})
public void verifyGmailpage(){
driver.get("https://www.gmail.com");
Assert.assertEquals("Gmail", driver.getTitle());
}
@Test(dependsOnMethods = {"verifyGmailpage"})
public void verifyYahoopage(){
driver.get("https://in.yahoo.com/");
Assert.assertEquals("Yahoo", driver.getTitle());
}
@Test(dependsOnMethods = {"verifyYahoopage"})
public void closeBrowser(){
driver.close();   
}
}
--------------------------------------------------
public class TestngProgram {
public WebDriver driver;
@Test
public void launchBrowser(){
driver = new FirefoxDriver();
}
@Test(dependsOnMethods = {"launchBrowser"})
public void verifyGmailpage(){
driver.get("https://www.gmail.com");
Assert.assertEquals("Gmail", driver.getTitle());
}
@Test(dependsOnMethods = {"verifyGmailpage"})
public void verifyYahoopage(){
driver.get("https://in.yahoo.com/");
Assert.assertEquals("YahooA", driver.getTitle());
}
@Test(dependsOnMethods = {"verifyYahoopage"}, alwaysRun=true)
public void closeBrowser(){
driver.close();   
}
}
--------------------------------------------------------
Test Cases


Launch Browser - Pre-condition for every Test case
Close Browser - Post-condition for every Test case

i) Verify Gmail home Page Title
ii) Verify Yahoo home page Title

Test Execution Flow:

Launch Browser
Verify Gmail home Page Title
Close Browser

Launch Browser
Verify Yahoo home page Title
Close Browser
--------------------------------------
@BeforeMethod - Pre-condition for every Test case in a Class /Program
@AfterMethod - Post-condition for every Test case in a Class /Program

Example:
public class TestngProgram {
public WebDriver driver;
@BeforeMethod
public void launchBrowser(){
driver = new FirefoxDriver();
}
@Test(priority = 1)
public void verifyGmailpage(){
driver.get("https://www.gmail.com");
Assert.assertEquals("Gmail", driver.getTitle());
}
@Test(priority = 2)
public void verifyYahoopage(){
driver.get("https://in.yahoo.com/");
Assert.assertEquals("Yahoo", driver.getTitle());
}
@AfterMethod
public void closeBrowser(){
driver.close();   
}
}
------------------------------------------------------

TestNG Framework for Selenium Part-2 Link

3 comments:

Hi sir let me know if the multiple methods have same priority say priority=1 what is the output,or how it will execute

Ragards
Kiran

Post a Comment