TestNG Tutorial for Selenium
TestNG Introduction together with practise Test cases using TestNG Annotations.
> In Selenium using Java in that place are 2 Automation frameworks available:
i) JUnit
ii) TestNG
---------------------------------
TestNG Introduction
> TestNG is to a greater extent than powerful than JUnit framework.
> TestNG is an opened upwardly root assay automation framework, where NG stands for Next Generation
> TestNG inspired from JUNit simply introduced around novel functionality.
Benefits of TestNG:-------------------
i) It generates HTML assay reports
ii) TestNG Annotations are slow to practise Test cases.
iii) Test cases tin endure grouped together with prioritized to a greater extent than easily.
iv) Parallel Testing is possible
v) Parameterization is possible.
---------------------------------------
Using TestNG nosotros tin practise Test cases, execute Test cases together with generate reports using HTML format.
----------------------------------------------
Manual Test Case:
------------------
Test Case Id: TC_001
Test Case Name: Verify championship of the Page
Test Steps:
---------------
i) Launch the Browser WebDriver driver = novel FirefoxDriver();
ii) Navigate to gmail.com driver.get("htttp:\\www.gmail.com");
iii) banking concern check the page title String Actual = driver.getTitle();
Assert.assertEquals(Actual, "Gmail");
Expected:
Actual:
Status:
-------------------------
@Test
public void verifyTitle()
{
WebDriver driver = novel FirefoxDriver();
driver.get("htttp:\\www.gmail.com");
String Actual = driver.getTitle();
Assert.assertEquals(Actual, "Gmail");
}
Note: primary method is non required for TestNG programs.
TestNG programs incorporate solely methods that incorporate @Test Annotations.
If don't write @Test Annotation therefore the methods are non going to endure executed.
--------------------------------------------------------------
Install TestNG:
In Eclipse IDE,
Help card -> Install New Software -> Click Add
-> Enter Name equally "TestNG"
-> Enter URL equally "http://beust.com/eclipse/"
-> Select TestNG
-> Next -> Next -> Accept the Agreement -> Finish
------------------------------------------
TestNG Test instance example:
package seleniumTests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public shape SampleTest {
@Test
world void verifyTitle()
{
WebDriver driver = novel FirefoxDriver();
driver.get("http:/www.gmail.com");
String Actual = driver.getTitle();
Assert.assertEquals(Actual, "Gmail");
}
}
-------------------------------------------------
Create Multiple Test cases
TestNG assay cases tin endure executed inward alphabetical order, if y'all desire to command the
Test execution therefore utilization Attributes.
Ex 1:
public shape SampleTest {
@Test
world void testA()
{
Assert.assertEquals("Google", "Google");
}
@Test
world void testC()
{
Assert.assertEquals("Gmail", "Google");
}
@Test
world void testB()
{
Assert.assertEquals("Yahoo", "Yahoo");
}
}
----------------------------------
public shape SampleTest {
@Test (priority = 1, description ="Verify Login", enabled =false)
world void Login()
{
System.out.println("Login Successful");
}
@Test (priority = 2)
world void Search()
{
System.out.println("Search functioning successful");
}
@Test (priority = 3)
world void Logout()
{
System.out.println("Logout Successful");
}
}
------------------------------------
In TestNG Test nosotros utilization Java programming features, Webdriver methods together with TestNG Annotations.
------------------------------
@Test -TestNG Annotation
@Test (priority=1) - Test method execution priority
@Test (description="**************") - description of the Test case
@Test (enabled =false) - Ignores the Test case
@Test(dependsOnMethods={"method name"})
---------------------------------------------
priority Versus dependsOnMethods
Test cases 10
----------------------------------------------
public shape SampleTest {
@Test
world void Login() // This is a Test
{
System.out.println("Login Successful");
}
@Test (dependsOnMethods ={"Login"})
world void Search()
{
Assert.assertEquals("abc", "xyz");
}
@Test (dependsOnMethods ={"Search"}, alwaysRun=true)
world void Logout()
{
System.out.println("Logout Successful");
}
}
------------------------------------------------
Hard dependency:
@Test (dependsOnMethods={"Search"})
---------------------------------
Soft dependency:
@Test (dependsOnMethods={"Search"}, alwaysRun=true)
-----------------------------------------------------------
TestNG Annotations
@Test
Login -precondition for every assay case
Logout -Post status for every assay case
----------------
Search
Advanced Search
Buy products
--------------------
Execution Flow:
Login
Search
Logout
Login
Advanced Search
Logout
Login
Buy products
Logout
----------------------------------
public shape Sample2 {
@BeforeMethod
world void Login()
{
System.out.println("Login Successful");
}
@Test (priority=1)
world void Search()
{
System.out.println("Search Successful");
}
@Test (priority=2)
world void AdvancedSearch()
{
System.out.println("Advanced Search Successful");
}
@Test (priority=3)
world void BuyProducts()
{
System.out.println("Buy Products Successful");
}
@AfterMethod
world void Logout()
{
System.out.println("Logout Successful");
}
}
-------------------------------------
Login -precondition for every assay case
Logout -Post status for every assay case
----------------
Search
Advanced Search
Buy products
------------------
Execution Flow:
Login
Search
AdvancedSearch
BuyProducts
Logout
---------------------
@BeforeClass
@AfterClass
------------------------
Example:
public shape Sample2 {
@BeforeClass
world void Login()
{
System.out.println("Login Successful");
}
@Test (priority=1)
world void Search()
{
System.out.println("Search Successful");
}
@Test (priority=2)
world void AdvancedSearch()
{
System.out.println("Advanced Search Successful");
}
@Test (priority=3)
world void BuyProducts()
{
System.out.println("Buy Products Successful");
}
@AfterClass
world void Logout()
{
System.out.println("Logout Successful");
}
}
------------------------------------------------
Executing i or to a greater extent than programs using XML file
<suite advert = "EcomerceSuite">
<test advert ="UserTest">
<classes>
<class advert = "package.Class1Name"/>
<class advert = "package.Class2Name"/>
</classes>
</test>
</suite>
----------------------------------------------------------
suite tag
test tag
classes tag
class tag
---------------------------------
Create XML file inward Eclipse
Select Java Project -> Right click -> New -> Other
-> Enter testng together with Select TestNG Class
-> Enter root together with parcel names
-> Enter xml file advert equally "testng.xml"
------------------------------------------
Executing multiple classes using XML file.
@BeforeMethod - executes earlier every assay instance inward a class
@AfterMethod -executes afterwards every assay instance inward class
---------------
@BeforeClass -executes earlier all assay cases inward a class
@AfterClass - executes afterwards all assay cases inward a class
--------------------------------
@BeforeTest - executes earlier all assay cases from all classes inward XML file
@AfterTest -executes afterwards all assay cases from all classes inward XML file
------------------------------------------------
XML file for executing multiple classes:
-----------------------------------------
<suite name="Suite" >
<test name="Test">
<classes>
<class name="seleniumTests.Abcd1"/>
<class name="seleniumTests.Abcd2"/>
</classes>
</test>
</suite>
-----------------------
Class 1-----------------------------------------------
public shape Abcd1 {
@BeforeTest
world void Login(){
System.out.println("Login Successful");
}
@AfterTest
world void Logout(){
System.out.println("Logout Successful");
}
@Test (priority = 1)
world void FundTransfer (){
System.out.println("Fund Transfer Successful");
}
@Test (priority=3)
world void PrepaidRecharge(){
System.out.println("Prepaid Recharge Successful");
}
@Test (priority=2)
world void BillPayments(){
System.out.println("Bill Payments Successful");
}
}
-----------------------------------------------
Class 2----------------------------------------------
public shape Abcd2 {
/*@BeforeMethod
world void Login() {
System.out.println("Login Successful");
}
@AfterMethod
world void Logout() {
System.out.println("Logout Successful");
}*/
@Test(priority=1)
world void Search() {
System.out.println("Search Successful");
}
@Test(priority=2)
world void AdvancedSearch() {
System.out.println("Advanced Search Successful");
}
}
-----------------------------------------
Grouping Test Cases
XML file
<suite name="Suite">
<test name="Test">
<groups>
<run>
<include advert ="regression"/>
</run>
</groups>
<classes>
<class name="seleniumTests.GroupingTests"/>
</classes>
</test>
</suite>
----------------------------
Class File
public shape GroupingTests {
@BeforeGroups (groups ="regression")
world void Login(){
System.out.println("Login Successful");
}
@AfterGroups (groups ="regression")
world void Logout(){
System.out.println("Logout Successful");
}
@Test (groups ={"sanity"})
world void FundTransfer (){
System.out.println("Fund Transfer Successful");
}
@Test (groups ={"sanity", "regression"})
world void Search (){
System.out.println("Search Successful");
}
@Test (groups ={"regression"})
world void PrepaidRecharge(){
System.out.println("Prepaid Recharge Successful");
}
@Test (groups ={"regression"})
world void BillPayments(){
System.out.println("Bill Payments Successful");
}
}
-----------------------------------------------
Data driven Testing using DataProvider annotation
public shape DataDriven {
@Test (dataProvider ="testdata")
world void Addition(String val1, String val2, String val3){
int a = Integer.parseInt(val1);
int b = Integer.parseInt(val2);
int c = Integer.parseInt(val3);
int outcome = a + b + c;
System.out.println(result);
}
@DataProvider(name="testdata")
public Object [] [] readExcel() throws BiffException, IOException{
File f = novel File("C:/Users/gcreddy/Desktop/Input.xls");
Workbook w = Workbook.getWorkbook(f);
Sheet second = w.getSheet(0);
int rows = s.getRows();
int columns = s.getColumns();
//System.out.println(rows);
// System.out.println(columns);
String InputData [] [] = novel String [rows] [columns];
for (int i = 0; i<rows; i++){
for (int j=0; j<columns; j++){
Cell c = s.getCell(j, i);
InputData [i] [j] = c.getContents();
//System.out.println(InputData [i][j]);
}
}
return InputData;
}
}
-----------------------------------------------------
Parallel Test execution
Parallel assay execution using TestNG (using unmarried machine/computer)
Parallel assay execution Using Selenium Grid (using multiple computers)
----------------------------------------
Take tests together with execute using dissimilar browsers (IE, Firefox, together with Chrome)
---------------------------------------------
Steps:
Before Class - Launch Browsers (Firefox, Internet Explorer together with Chrome)
Execute Tests on all Browsers
After Class - Close all browsers
--------------------------------
Class File--------------------------
public shape ParallelTests {
world WebDriver driver;
@Parameters("browser")
@BeforeClass
world void LunchBrowser(String browser) {
if(browser.equalsIgnoreCase("FF")) {
driver = novel FirefoxDriver();
}
else if (browser.equalsIgnoreCase("IE")) {
System.setProperty("webdriver.ie.driver", "E:/IEDriverServer.exe");
driver = novel InternetExplorerDriver();
}
else if (browser.equalsIgnoreCase("CH")) {
System.setProperty("webdriver.chrome.driver", "E:/chromedriver.exe");
driver = novel ChromeDriver();
}
driver.get("http://www.google.com");
}
@Test
world void VerifyPageTitle() throws InterruptedException {
Assert.assertEquals(driver.getTitle(), "Google");
}
@AfterClass
world void CloseBrowsers() {
driver.quit();
}
}
-------------------------------
XML file--------------------------
<suite name="Suite" parallel="tests" thread-count ="3">
<test name="FireFoxTests">
<parameter advert = "browser" value = "FF"/>
<classes>
<class name="seleniumTests.ParallelTests"/>
</classes>
</test>
<test name="IETests">
<parameter advert = "browser" value = "CH"/>
<classes>
<class name="seleniumTests.ParallelTests"/>
</classes>
</test>
<test name="ChromeTests">
<parameter advert = "browser" value = "IE"/>
<classes>
<class name="seleniumTests.ParallelTests"/>
</classes>
</test>
</suite>
----------------------------------------------------
TestNG Introduction together with practise Test cases using TestNG Annotations.
> In Selenium using Java in that place are 2 Automation frameworks available:
i) JUnit
ii) TestNG
---------------------------------
TestNG Introduction
> TestNG is to a greater extent than powerful than JUnit framework.
> TestNG is an opened upwardly root assay automation framework, where NG stands for Next Generation
> TestNG inspired from JUNit simply introduced around novel functionality.
Benefits of TestNG:-------------------
i) It generates HTML assay reports
ii) TestNG Annotations are slow to practise Test cases.
iii) Test cases tin endure grouped together with prioritized to a greater extent than easily.
iv) Parallel Testing is possible
v) Parameterization is possible.
---------------------------------------
Using TestNG nosotros tin practise Test cases, execute Test cases together with generate reports using HTML format.
----------------------------------------------
Manual Test Case:
------------------
Test Case Id: TC_001
Test Case Name: Verify championship of the Page
Test Steps:
---------------
i) Launch the Browser WebDriver driver = novel FirefoxDriver();
ii) Navigate to gmail.com driver.get("htttp:\\www.gmail.com");
iii) banking concern check the page title String Actual = driver.getTitle();
Assert.assertEquals(Actual, "Gmail");
Expected:
Actual:
Status:
-------------------------
@Test
public void verifyTitle()
{
WebDriver driver = novel FirefoxDriver();
driver.get("htttp:\\www.gmail.com");
String Actual = driver.getTitle();
Assert.assertEquals(Actual, "Gmail");
}
Note: primary method is non required for TestNG programs.
TestNG programs incorporate solely methods that incorporate @Test Annotations.
If don't write @Test Annotation therefore the methods are non going to endure executed.
--------------------------------------------------------------
Install TestNG:
In Eclipse IDE,
Help card -> Install New Software -> Click Add
-> Enter Name equally "TestNG"
-> Enter URL equally "http://beust.com/eclipse/"
-> Select TestNG
-> Next -> Next -> Accept the Agreement -> Finish
------------------------------------------
TestNG Test instance example:
package seleniumTests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public shape SampleTest {
@Test
world void verifyTitle()
{
WebDriver driver = novel FirefoxDriver();
driver.get("http:/www.gmail.com");
String Actual = driver.getTitle();
Assert.assertEquals(Actual, "Gmail");
}
}
-------------------------------------------------
Create Multiple Test cases
TestNG assay cases tin endure executed inward alphabetical order, if y'all desire to command the
Test execution therefore utilization Attributes.
Ex 1:
public shape SampleTest {
@Test
world void testA()
{
Assert.assertEquals("Google", "Google");
}
@Test
world void testC()
{
Assert.assertEquals("Gmail", "Google");
}
@Test
world void testB()
{
Assert.assertEquals("Yahoo", "Yahoo");
}
}
----------------------------------
public shape SampleTest {
@Test (priority = 1, description ="Verify Login", enabled =false)
world void Login()
{
System.out.println("Login Successful");
}
@Test (priority = 2)
world void Search()
{
System.out.println("Search functioning successful");
}
@Test (priority = 3)
world void Logout()
{
System.out.println("Logout Successful");
}
}
------------------------------------
In TestNG Test nosotros utilization Java programming features, Webdriver methods together with TestNG Annotations.
------------------------------
@Test -TestNG Annotation
@Test (priority=1) - Test method execution priority
@Test (description="**************") - description of the Test case
@Test (enabled =false) - Ignores the Test case
@Test(dependsOnMethods={"method name"})
---------------------------------------------
priority Versus dependsOnMethods
Test cases 10
----------------------------------------------
public shape SampleTest {
@Test
world void Login() // This is a Test
{
System.out.println("Login Successful");
}
@Test (dependsOnMethods ={"Login"})
world void Search()
{
Assert.assertEquals("abc", "xyz");
}
@Test (dependsOnMethods ={"Search"}, alwaysRun=true)
world void Logout()
{
System.out.println("Logout Successful");
}
}
------------------------------------------------
Hard dependency:
@Test (dependsOnMethods={"Search"})
---------------------------------
Soft dependency:
@Test (dependsOnMethods={"Search"}, alwaysRun=true)
-----------------------------------------------------------
TestNG Annotations
@Test
Login -precondition for every assay case
Logout -Post status for every assay case
----------------
Search
Advanced Search
Buy products
--------------------
Execution Flow:
Login
Search
Logout
Login
Advanced Search
Logout
Login
Buy products
Logout
----------------------------------
public shape Sample2 {
@BeforeMethod
world void Login()
{
System.out.println("Login Successful");
}
@Test (priority=1)
world void Search()
{
System.out.println("Search Successful");
}
@Test (priority=2)
world void AdvancedSearch()
{
System.out.println("Advanced Search Successful");
}
@Test (priority=3)
world void BuyProducts()
{
System.out.println("Buy Products Successful");
}
@AfterMethod
world void Logout()
{
System.out.println("Logout Successful");
}
}
-------------------------------------
Login -precondition for every assay case
Logout -Post status for every assay case
----------------
Search
Advanced Search
Buy products
------------------
Execution Flow:
Login
Search
AdvancedSearch
BuyProducts
Logout
---------------------
@BeforeClass
@AfterClass
------------------------
Example:
public shape Sample2 {
@BeforeClass
world void Login()
{
System.out.println("Login Successful");
}
@Test (priority=1)
world void Search()
{
System.out.println("Search Successful");
}
@Test (priority=2)
world void AdvancedSearch()
{
System.out.println("Advanced Search Successful");
}
@Test (priority=3)
world void BuyProducts()
{
System.out.println("Buy Products Successful");
}
@AfterClass
world void Logout()
{
System.out.println("Logout Successful");
}
}
------------------------------------------------
Executing i or to a greater extent than programs using XML file
<suite advert = "EcomerceSuite">
<test advert ="UserTest">
<classes>
<class advert = "package.Class1Name"/>
<class advert = "package.Class2Name"/>
</classes>
</test>
</suite>
----------------------------------------------------------
suite tag
test tag
classes tag
class tag
---------------------------------
Create XML file inward Eclipse
Select Java Project -> Right click -> New -> Other
-> Enter testng together with Select TestNG Class
-> Enter root together with parcel names
-> Enter xml file advert equally "testng.xml"
------------------------------------------
Executing multiple classes using XML file.
@BeforeMethod - executes earlier every assay instance inward a class
@AfterMethod -executes afterwards every assay instance inward class
---------------
@BeforeClass -executes earlier all assay cases inward a class
@AfterClass - executes afterwards all assay cases inward a class
--------------------------------
@BeforeTest - executes earlier all assay cases from all classes inward XML file
@AfterTest -executes afterwards all assay cases from all classes inward XML file
------------------------------------------------
XML file for executing multiple classes:
-----------------------------------------
<suite name="Suite" >
<test name="Test">
<classes>
<class name="seleniumTests.Abcd1"/>
<class name="seleniumTests.Abcd2"/>
</classes>
</test>
</suite>
-----------------------
Class 1-----------------------------------------------
public shape Abcd1 {
@BeforeTest
world void Login(){
System.out.println("Login Successful");
}
@AfterTest
world void Logout(){
System.out.println("Logout Successful");
}
@Test (priority = 1)
world void FundTransfer (){
System.out.println("Fund Transfer Successful");
}
@Test (priority=3)
world void PrepaidRecharge(){
System.out.println("Prepaid Recharge Successful");
}
@Test (priority=2)
world void BillPayments(){
System.out.println("Bill Payments Successful");
}
}
-----------------------------------------------
Class 2----------------------------------------------
public shape Abcd2 {
/*@BeforeMethod
world void Login() {
System.out.println("Login Successful");
}
@AfterMethod
world void Logout() {
System.out.println("Logout Successful");
}*/
@Test(priority=1)
world void Search() {
System.out.println("Search Successful");
}
@Test(priority=2)
world void AdvancedSearch() {
System.out.println("Advanced Search Successful");
}
}
-----------------------------------------
Grouping Test Cases
XML file
<suite name="Suite">
<test name="Test">
<groups>
<run>
<include advert ="regression"/>
</run>
</groups>
<classes>
<class name="seleniumTests.GroupingTests"/>
</classes>
</test>
</suite>
----------------------------
Class File
public shape GroupingTests {
@BeforeGroups (groups ="regression")
world void Login(){
System.out.println("Login Successful");
}
@AfterGroups (groups ="regression")
world void Logout(){
System.out.println("Logout Successful");
}
@Test (groups ={"sanity"})
world void FundTransfer (){
System.out.println("Fund Transfer Successful");
}
@Test (groups ={"sanity", "regression"})
world void Search (){
System.out.println("Search Successful");
}
@Test (groups ={"regression"})
world void PrepaidRecharge(){
System.out.println("Prepaid Recharge Successful");
}
@Test (groups ={"regression"})
world void BillPayments(){
System.out.println("Bill Payments Successful");
}
}
-----------------------------------------------
Data driven Testing using DataProvider annotation
public shape DataDriven {
@Test (dataProvider ="testdata")
world void Addition(String val1, String val2, String val3){
int a = Integer.parseInt(val1);
int b = Integer.parseInt(val2);
int c = Integer.parseInt(val3);
int outcome = a + b + c;
System.out.println(result);
}
@DataProvider(name="testdata")
public Object [] [] readExcel() throws BiffException, IOException{
File f = novel File("C:/Users/gcreddy/Desktop/Input.xls");
Workbook w = Workbook.getWorkbook(f);
Sheet second = w.getSheet(0);
int rows = s.getRows();
int columns = s.getColumns();
//System.out.println(rows);
// System.out.println(columns);
String InputData [] [] = novel String [rows] [columns];
for (int i = 0; i<rows; i++){
for (int j=0; j<columns; j++){
Cell c = s.getCell(j, i);
InputData [i] [j] = c.getContents();
//System.out.println(InputData [i][j]);
}
}
return InputData;
}
}
-----------------------------------------------------
Parallel Test execution
Parallel assay execution using TestNG (using unmarried machine/computer)
Parallel assay execution Using Selenium Grid (using multiple computers)
----------------------------------------
Take tests together with execute using dissimilar browsers (IE, Firefox, together with Chrome)
---------------------------------------------
Steps:
Before Class - Launch Browsers (Firefox, Internet Explorer together with Chrome)
Execute Tests on all Browsers
After Class - Close all browsers
--------------------------------
Class File--------------------------
public shape ParallelTests {
world WebDriver driver;
@Parameters("browser")
@BeforeClass
world void LunchBrowser(String browser) {
if(browser.equalsIgnoreCase("FF")) {
driver = novel FirefoxDriver();
}
else if (browser.equalsIgnoreCase("IE")) {
System.setProperty("webdriver.ie.driver", "E:/IEDriverServer.exe");
driver = novel InternetExplorerDriver();
}
else if (browser.equalsIgnoreCase("CH")) {
System.setProperty("webdriver.chrome.driver", "E:/chromedriver.exe");
driver = novel ChromeDriver();
}
driver.get("http://www.google.com");
}
@Test
world void VerifyPageTitle() throws InterruptedException {
Assert.assertEquals(driver.getTitle(), "Google");
}
@AfterClass
world void CloseBrowsers() {
driver.quit();
}
}
-------------------------------
XML file--------------------------
<suite name="Suite" parallel="tests" thread-count ="3">
<test name="FireFoxTests">
<parameter advert = "browser" value = "FF"/>
<classes>
<class name="seleniumTests.ParallelTests"/>
</classes>
</test>
<test name="IETests">
<parameter advert = "browser" value = "CH"/>
<classes>
<class name="seleniumTests.ParallelTests"/>
</classes>
</test>
<test name="ChromeTests">
<parameter advert = "browser" value = "IE"/>
<classes>
<class name="seleniumTests.ParallelTests"/>
</classes>
</test>
</suite>
----------------------------------------------------