TestNG Testing Framework for Selenium


TestNG Testing Framework for Selenium Part 1

I) Introduction to TestNG

II) Install TestNG together with write start TestNG Test Case

III) Create multiple Test Cases together with Run

IV) Execute multiple programs / classes using XML
--------------------------------------------------------
I) Introduction to TestNG
In Selenium using Java in that place are ii Testing frameworks available,

i) JUnit

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

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

> Initially developed for Unit Testing, at i time used for all kinds of Testing.

> TestNG is an opened upwards root framework, where NG stands for side yesteryear side generation.

> TestNG inspired from Junit(Java platform) together with NUnit (.NET platform), simply introduced around
   novel functionalities that arrive to a greater extent than powerful together with easier to use.
-------------------------------------------------------------------
Advantages of TestNG:
1) TestNG Annotations are slow to exercise Test Cases.

2) Test Cases tin survive grouped together with prioritized to a greater extent than 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 alongside other tools together with plug ins (Eclipse IDE, construct tools similar ANT, Maven etc...)
------------------------------------------------------------------
Note: using TestNG nosotros tin exercise Test Cases, grouping Test Cases, prioritize Test Case, execute Test Cases together with generate Test Reports.
----------------------------------------------------
II) Install TestNG together with write start TestNG Test Case

In Eclipse IDE:

> Help menu
> Install New Software
> Click Add
> Enter Name every bit "TestNG"
> Enter URL every bit "http://beust.com/eclipse/"
> Select "TestNG"
> Next > Next > get got the Agreement > Finish
-----------------------------------------------
Write start 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 together with Compare alongside expected.

Expected = Gmail

Actual =

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

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

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

1) primary method is non used for TestNG Programs.

2) TestNG programme contains exclusively methods that incorporate @Test Annotations.

3) If nosotros don't write @Test Annotation together with then the method is non going to survive executed.
---------------------------------------------------------------------------------
III) Write multiple Test Cases inwards a Program

public cast 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 every bit per Program:

verifyTitle
abcd
xyz

Execution Flow:

abcd
verifyTitle
xyz

Note: TestNG Test Cases are execute inwards Alphabetical order

If you lot desire to command the Test execution procedure together with then utilization priority attribute.


priority attribute Example:

public cast 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 nosotros tin utilization dependsOnMethods attribute
------------------------------------------------------------------
Example:

Using priority attribute

public cast 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 cast 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 cast 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 nosotros utilization priority attribute together with then all Test cases tin survive executed, no skips

If nosotros utilization dependsOnMethods attribute together with then it volition skip the exam example whenever dependsOnMethods
Test example fails, if you lot desire execute the exam example forcebly together with then utilization alwaysRun attribute.
---------------------------------
Note:

If in that place is no functionality dependency together with then utilization priority attribute.

If in that place is whatever functionality dependency together with then utilization dependsOnMethods attribute
-----------------------------------------
Scenario 1: No functionality dependency - Use priority attribute

Scenario 2: Functionality dependency - Use dependsOnMethods attribute

Scenario 3: Functionality dependency for around Test Cases exclusively - Use dependsOnMethods together with alwaysRun attributes
-----------------------------------------------------------
Test Cases
 

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

TestNG Program:

public cast TestngProgram {
public WebDriver driver;
@Test(priority =1)
public void launchBrowser(){
driver = novel 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 cast TestngProgram {
public WebDriver driver;
@Test
public void launchBrowser(){
driver = novel 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 cast TestngProgram {
public WebDriver driver;
@Test
public void launchBrowser(){
driver = novel 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 habitation Page Title
ii) Verify Yahoo habitation page Title

Test Execution Flow:

Launch Browser
Verify Gmail habitation Page Title
Close Browser

Launch Browser
Verify Yahoo habitation page Title
Close Browser
--------------------------------------
@BeforeMethod - Pre-condition for every Test example inwards a Class /Program
@AfterMethod - Post-condition for every Test example inwards a Class /Program

Example:
public cast TestngProgram {
public WebDriver driver;
@BeforeMethod
public void launchBrowser(){
driver = novel 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

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post