SlideShare ist ein Scribd-Unternehmen logo
1 von 21
TestNG -Basic
Prabhanshu Saraswat
TestNG- Framework
• Open source
• Do not require Main method
• Efficient for testing
• Inspired by JUnit and NUnit.
• Use annotation for execution.
• Html Report
TestNG Installation to eclipse step-1
Step-2
Step-3
HTML-Report
Simple example
package SeleniumTests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class SampleTest {
@Test
public void verifytitle()
{
WebDriver driver = new FirefoxDriver();
driver.get("http:www.gmail.com");
String actual= driver.getTitle();
Assert.assertEquals(actual, "Gmail");
}
}
Multiple Test Support
• Priority – Test are executed based on define priority
• DependsOnMethod–Test are executed with dependency other test.
• Default – Test are executed in ascending order of name
Priority
package SeleniumTests;
/* Define priority of test. lowest executed first. Enable make test non-executable(like in test of priority of -1 */
import org.testng.annotations.Test;
public class multipletest{
@Test(priority =-1, enabled =false)//Test is non-executable.
public void login()
{
System.out.println("login success");}
@Test(priority =1)
public void search()
{
System.out.println("search success");}
@Test (priority = 2)
public void logout()
{
System.out.println("logout success");}
}
DependsOnMethod
package SeleniumTests;
/* dependson is used to define dependency execution*/
import org.testng.annotations.Test;
public class multipletestwithdependsonmethods2 {
@Test
public void login()
{
System.out.println("login success");}
@Test(dependsOnMethods ={"login"})
public void search()
{
System.out.println("search success");}
@Test (dependsOnMethods ={"search"})
public void logout()
{
System.out.println("logout success");}
}
Skip and always execution of Test
package SeleniumTests;
import org.testng.Assert;
/* depends on method also define dependency execution and skip of test */
import org.testng.annotations.Test;
public class multipletestforskip {
@Test
public void login()
{System.out.println("login success");}
@Test(dependsOnMethods ={"login"})
public void search()
{ Assert.assertEquals("abc", "test");}
@Test (dependsOnMethods ={"search"})
public void logoutskip()// logoutskip is skipped as search is fail. This is hard dependency.
{System.out.println("logout success");}
@Test (dependsOnMethods ={"search"}, alwaysRun=true)
public void logout()// logout is run even dependent case is fail.This is called soft dependency.
{System.out.println("logout success");} }
Scenario based examples-1.
/*
*
login
advance search
logout
login
search
logout
login
buyproduct
logout
*/
public class Sample2 {
@BeforeMethod
public void login()
{System.out.println("login success");}
@Test(priority=2)
public void search()
{System.out.println("search success");}
@Test(priority=1)
public void advancesearch()
{System.out.println("advance search success");}
@Test(priority=3)
public void buyproduct()
{System.out.println("buyproduct success");}
@AfterMethod
public void logout()
{System.out.println("logout success");}}
Scenario based examples-2.
/*
login
advance search
search
buyproduct
logout
*/
public class Sample3 {
@BeforeClass
public void login()
{System.out.println("login success");}
@Test(priority=2)
public void search()
{System.out.println("search success");}
@Test(priority=1)
public void advancesearch()
{System.out.println("advance search success");}
@Test(priority=3)
public void buyproduct()
{System.out.println("buyproduct success");}
@AfterClass
public void logout()
{System.out.println("logout success");}
}
Multiple test executions serially
• Create xml file to define classes
• Create test classes
Xml file
<suite name = "Ëcommerce suite">
<test name ="sanity tests">
<classes>
<class name = "SeleniumTests.Sample5"/>
<class name = "SeleniumTests.Sample4"/>
</classes>
</test>
</suite>
public class Sample4 {
@BeforeTest
public void login()
{System.out.println("login success");}
@Test(priority=2)
public void fundtransfer()
{System.out.println("fundtransfer
success");}
@Test(priority=1)
public void accountsummary()
{System.out.println("accountsummary
success");}
@Test(priority=3)
public void billpayment()
{System.out.println("billpayment
success");}
@Test(priority=4)
public void threadtest()
{System.out.println("thread success with
class"+getClass().getSimpleName()+"withthread
name"+Thread.currentThread().getId());}
@AfterTest
public void logout()
{System.out.println("logout success");}}
public class Sample5 {
@Test(priority=2)
public void search()
{
System.out.println("search success");}
@Test(priority=1)
public void advancesearch()
{
System.out.println("advance search success");}
@Test(priority=3)
public void buyproduct()
{
System.out.println("buyproduct success ");}
@Test(priority=4)
public void threadtest()
{
System.out.println("thread success with
class"+getClass().getSimpleName()+"withthread
name"+Thread.currentThread().getId());}
}
Parallel execution of multiple tests
• execution• Create xml file to define classes and define threads
• Create test classes
<suite name = "Parallel test suite" parallel = "classes" thread-count ="2">
<test name ="Sanity tests">
<classes>
<class name = "SeleniumTests.Sample5"/>
<class name = "SeleniumTests.Sample4"/>
</classes>
</test>
</suite>
Group execution of Methods
• Create xml file to define classes and groups
• Create test classes
<suite name = "Ëcommerce suite">
<test name ="sanity tests">
<groups>
<run>
<include name = "group1"/></run>
</groups>
<classes>
<class name = "SeleniumTests.Sample6"/>
<class name = "SeleniumTests.Sample7"/>
</classes>
</test>
</suite>
public class Sample6 {
@BeforeTest (groups = {"group2","group1"})
public void test1()
{System.out.println("test1 success");}
@Test(priority=2, groups= {"group1"} )
public void test2()
{System.out.println("test2 success");}
@Test(priority=1, groups= {"group2"})
public void test3()
{System.out.println("test3 success");}
@Test(priority=3, groups= {"group1"})
public void test4()
{System.out.println("test4 success");}
@Test(priority=4, groups= {"group1", "group2"})
public void threadtest()
{
System.out.println("thread success with
class"+getClass().getSimpleName()+"withthread
name"+Thread.currentThread().getId());
}
@AfterTest(groups = {"group2","group1"})
public void test5()
{System.out.println("test5 success");}}
public class Sample7 {
@Test(priority=2,groups= {"group1"})
public void stest1()
{
System.out.println("stest1 success");}
@Test(priority=1,groups= {"group2"})
public void stest2()
{
System.out.println("stest2 success");}
@Test(priority=3, groups= {"group1"})
public void stest3()
{
System.out.println("stest3 success ");}
@Test(priority=4, groups= {"group1", "group2"})
public void threadtest()
{
System.out.println("thread success with
class"+getClass().getSimpleName()+"withthread
name"+Thread.currentThread().getId());}
}
Data driven framework using TestNG
• Third party support needed in data driven framework like POI, JXl etc.
• Annotation- Data Provider is used for data connection.
• Excel should be created on defined location.
public class DataDrivern {
@Test(dataProvider ="testdata")
public void login(String username, String Password)
{String status;
WebDriver driver = new FirefoxDriver();
driver.get("http://lms.nagarro.com/");
WebElement element = driver.findElement(By.id("username"));
element.sendKeys(username);
element = driver.findElement(By.id("password"));
element.sendKeys(Password);
element = driver.findElement(By.name("submit"));
element.click();
if( driver.getTitle().contains("Nagarro Learning Management
System"))
{status= "Pass"; driver.findElement(By.linkText("Log out")).click();}
else { status= "Fail"; }
Assert.assertEquals(status, "Pass");
driver.close();}
@DataProvider(name="testdata")
public Object[][] Readexcel()throws
BiffException, IOException
{ File f = new File ("E:/input.xls");
Workbook w = Workbook.getWorkbook(f);
Sheet s= w.getSheet(0);
int rows = s.getRows();
int columns =s.getColumns();
String InputData [][] = new 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();
}}return InputData;}}
InputExcel
Thanks

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

TestNG with selenium
TestNG with seleniumTestNG with selenium
TestNG with selenium
 
Selenium TestNG
Selenium TestNGSelenium TestNG
Selenium TestNG
 
Setting up Page Object Model in Automation Framework
Setting up Page Object Model in Automation FrameworkSetting up Page Object Model in Automation Framework
Setting up Page Object Model in Automation Framework
 
Junit
JunitJunit
Junit
 
TestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit TestingTestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit Testing
 
Selenium Training | TestNG Framework For Selenium | Selenium Tutorial For Beg...
Selenium Training | TestNG Framework For Selenium | Selenium Tutorial For Beg...Selenium Training | TestNG Framework For Selenium | Selenium Tutorial For Beg...
Selenium Training | TestNG Framework For Selenium | Selenium Tutorial For Beg...
 
Selenium with java
Selenium with javaSelenium with java
Selenium with java
 
Selenium test automation
Selenium test automationSelenium test automation
Selenium test automation
 
Selenium Page Object Model Using Page Factory | Selenium Tutorial For Beginne...
Selenium Page Object Model Using Page Factory | Selenium Tutorial For Beginne...Selenium Page Object Model Using Page Factory | Selenium Tutorial For Beginne...
Selenium Page Object Model Using Page Factory | Selenium Tutorial For Beginne...
 
JUNit Presentation
JUNit PresentationJUNit Presentation
JUNit Presentation
 
Test ng
Test ngTest ng
Test ng
 
Data driven Automation Framework with Selenium
Data driven Automation Framework with Selenium Data driven Automation Framework with Selenium
Data driven Automation Framework with Selenium
 
Automation Testing by Selenium Web Driver
Automation Testing by Selenium Web DriverAutomation Testing by Selenium Web Driver
Automation Testing by Selenium Web Driver
 
AUTOMATION FRAMEWORK USING SELENIUM & TESTNG ALONG WITH MAVEN
AUTOMATION FRAMEWORK USING SELENIUM & TESTNG ALONG WITH MAVENAUTOMATION FRAMEWORK USING SELENIUM & TESTNG ALONG WITH MAVEN
AUTOMATION FRAMEWORK USING SELENIUM & TESTNG ALONG WITH MAVEN
 
Hybrid framework
Hybrid frameworkHybrid framework
Hybrid framework
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and MockitoAn Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
 
Data Driven Framework in Selenium
Data Driven Framework in SeleniumData Driven Framework in Selenium
Data Driven Framework in Selenium
 
Selenium Presentation at Engineering Colleges
Selenium Presentation at Engineering CollegesSelenium Presentation at Engineering Colleges
Selenium Presentation at Engineering Colleges
 

Andere mochten auch

Andere mochten auch (11)

Test ng tutorial
Test ng tutorialTest ng tutorial
Test ng tutorial
 
Selenium ppt
Selenium pptSelenium ppt
Selenium ppt
 
Test ng for testers
Test ng for testersTest ng for testers
Test ng for testers
 
Hybrid Automation Framework
Hybrid Automation FrameworkHybrid Automation Framework
Hybrid Automation Framework
 
TestNG vs. JUnit4
TestNG vs. JUnit4TestNG vs. JUnit4
TestNG vs. JUnit4
 
TestNg_Overview_Config
TestNg_Overview_ConfigTestNg_Overview_Config
TestNg_Overview_Config
 
Using Selenium 3 0
Using Selenium 3 0Using Selenium 3 0
Using Selenium 3 0
 
Selenium Tutorial For Beginners | What Is Selenium? | Selenium Automation Tes...
Selenium Tutorial For Beginners | What Is Selenium? | Selenium Automation Tes...Selenium Tutorial For Beginners | What Is Selenium? | Selenium Automation Tes...
Selenium Tutorial For Beginners | What Is Selenium? | Selenium Automation Tes...
 
Automation Testing using Selenium
Automation Testing using SeleniumAutomation Testing using Selenium
Automation Testing using Selenium
 
Stand up
Stand upStand up
Stand up
 
Test automation Frame Works
Test automation Frame WorksTest automation Frame Works
Test automation Frame Works
 

Ähnlich wie TestNG

Test in action week 4
Test in action   week 4Test in action   week 4
Test in action week 4
Yi-Huan Chan
 
Selenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalks
Selenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalksSelenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalks
Selenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalks
Lohika_Odessa_TechTalks
 
Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2
Yi-Huan Chan
 
JUnit源码分析
JUnit源码分析JUnit源码分析
JUnit源码分析
onemonkey
 

Ähnlich wie TestNG (20)

Testing basics for developers
Testing basics for developersTesting basics for developers
Testing basics for developers
 
Appium TestNG Framework and Multi-Device Automation Execution
Appium TestNG Framework and Multi-Device Automation ExecutionAppium TestNG Framework and Multi-Device Automation Execution
Appium TestNG Framework and Multi-Device Automation Execution
 
Introduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylightIntroduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylight
 
Good Practices On Test Automation
Good Practices On Test AutomationGood Practices On Test Automation
Good Practices On Test Automation
 
Test in action week 4
Test in action   week 4Test in action   week 4
Test in action week 4
 
Dev labs alliance top 20 testng interview questions for sdet
Dev labs alliance top 20 testng interview questions for sdetDev labs alliance top 20 testng interview questions for sdet
Dev labs alliance top 20 testng interview questions for sdet
 
Junit_.pptx
Junit_.pptxJunit_.pptx
Junit_.pptx
 
Testing with Junit4
Testing with Junit4Testing with Junit4
Testing with Junit4
 
3 j unit
3 j unit3 j unit
3 j unit
 
Principles and patterns for test driven development
Principles and patterns for test driven developmentPrinciples and patterns for test driven development
Principles and patterns for test driven development
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
 
Selenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalks
Selenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalksSelenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalks
Selenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalks
 
Building frameworks over Selenium
Building frameworks over SeleniumBuilding frameworks over Selenium
Building frameworks over Selenium
 
L08 Unit Testing
L08 Unit TestingL08 Unit Testing
L08 Unit Testing
 
JUnit
JUnitJUnit
JUnit
 
Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2
 
(Unit )-Testing for Joomla
(Unit )-Testing for Joomla(Unit )-Testing for Joomla
(Unit )-Testing for Joomla
 
8-testing.pptx
8-testing.pptx8-testing.pptx
8-testing.pptx
 
Structured Testing Framework
Structured Testing FrameworkStructured Testing Framework
Structured Testing Framework
 
JUnit源码分析
JUnit源码分析JUnit源码分析
JUnit源码分析
 

Kürzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Kürzlich hochgeladen (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

TestNG

  • 2. TestNG- Framework • Open source • Do not require Main method • Efficient for testing • Inspired by JUnit and NUnit. • Use annotation for execution. • Html Report
  • 3. TestNG Installation to eclipse step-1
  • 7. Simple example package SeleniumTests; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.Test; public class SampleTest { @Test public void verifytitle() { WebDriver driver = new FirefoxDriver(); driver.get("http:www.gmail.com"); String actual= driver.getTitle(); Assert.assertEquals(actual, "Gmail"); } }
  • 8. Multiple Test Support • Priority – Test are executed based on define priority • DependsOnMethod–Test are executed with dependency other test. • Default – Test are executed in ascending order of name
  • 9. Priority package SeleniumTests; /* Define priority of test. lowest executed first. Enable make test non-executable(like in test of priority of -1 */ import org.testng.annotations.Test; public class multipletest{ @Test(priority =-1, enabled =false)//Test is non-executable. public void login() { System.out.println("login success");} @Test(priority =1) public void search() { System.out.println("search success");} @Test (priority = 2) public void logout() { System.out.println("logout success");} }
  • 10. DependsOnMethod package SeleniumTests; /* dependson is used to define dependency execution*/ import org.testng.annotations.Test; public class multipletestwithdependsonmethods2 { @Test public void login() { System.out.println("login success");} @Test(dependsOnMethods ={"login"}) public void search() { System.out.println("search success");} @Test (dependsOnMethods ={"search"}) public void logout() { System.out.println("logout success");} }
  • 11. Skip and always execution of Test package SeleniumTests; import org.testng.Assert; /* depends on method also define dependency execution and skip of test */ import org.testng.annotations.Test; public class multipletestforskip { @Test public void login() {System.out.println("login success");} @Test(dependsOnMethods ={"login"}) public void search() { Assert.assertEquals("abc", "test");} @Test (dependsOnMethods ={"search"}) public void logoutskip()// logoutskip is skipped as search is fail. This is hard dependency. {System.out.println("logout success");} @Test (dependsOnMethods ={"search"}, alwaysRun=true) public void logout()// logout is run even dependent case is fail.This is called soft dependency. {System.out.println("logout success");} }
  • 12. Scenario based examples-1. /* * login advance search logout login search logout login buyproduct logout */ public class Sample2 { @BeforeMethod public void login() {System.out.println("login success");} @Test(priority=2) public void search() {System.out.println("search success");} @Test(priority=1) public void advancesearch() {System.out.println("advance search success");} @Test(priority=3) public void buyproduct() {System.out.println("buyproduct success");} @AfterMethod public void logout() {System.out.println("logout success");}}
  • 13. Scenario based examples-2. /* login advance search search buyproduct logout */ public class Sample3 { @BeforeClass public void login() {System.out.println("login success");} @Test(priority=2) public void search() {System.out.println("search success");} @Test(priority=1) public void advancesearch() {System.out.println("advance search success");} @Test(priority=3) public void buyproduct() {System.out.println("buyproduct success");} @AfterClass public void logout() {System.out.println("logout success");} }
  • 14. Multiple test executions serially • Create xml file to define classes • Create test classes Xml file <suite name = "Ëcommerce suite"> <test name ="sanity tests"> <classes> <class name = "SeleniumTests.Sample5"/> <class name = "SeleniumTests.Sample4"/> </classes> </test> </suite>
  • 15. public class Sample4 { @BeforeTest public void login() {System.out.println("login success");} @Test(priority=2) public void fundtransfer() {System.out.println("fundtransfer success");} @Test(priority=1) public void accountsummary() {System.out.println("accountsummary success");} @Test(priority=3) public void billpayment() {System.out.println("billpayment success");} @Test(priority=4) public void threadtest() {System.out.println("thread success with class"+getClass().getSimpleName()+"withthread name"+Thread.currentThread().getId());} @AfterTest public void logout() {System.out.println("logout success");}} public class Sample5 { @Test(priority=2) public void search() { System.out.println("search success");} @Test(priority=1) public void advancesearch() { System.out.println("advance search success");} @Test(priority=3) public void buyproduct() { System.out.println("buyproduct success ");} @Test(priority=4) public void threadtest() { System.out.println("thread success with class"+getClass().getSimpleName()+"withthread name"+Thread.currentThread().getId());} }
  • 16. Parallel execution of multiple tests • execution• Create xml file to define classes and define threads • Create test classes <suite name = "Parallel test suite" parallel = "classes" thread-count ="2"> <test name ="Sanity tests"> <classes> <class name = "SeleniumTests.Sample5"/> <class name = "SeleniumTests.Sample4"/> </classes> </test> </suite>
  • 17. Group execution of Methods • Create xml file to define classes and groups • Create test classes <suite name = "Ëcommerce suite"> <test name ="sanity tests"> <groups> <run> <include name = "group1"/></run> </groups> <classes> <class name = "SeleniumTests.Sample6"/> <class name = "SeleniumTests.Sample7"/> </classes> </test> </suite>
  • 18. public class Sample6 { @BeforeTest (groups = {"group2","group1"}) public void test1() {System.out.println("test1 success");} @Test(priority=2, groups= {"group1"} ) public void test2() {System.out.println("test2 success");} @Test(priority=1, groups= {"group2"}) public void test3() {System.out.println("test3 success");} @Test(priority=3, groups= {"group1"}) public void test4() {System.out.println("test4 success");} @Test(priority=4, groups= {"group1", "group2"}) public void threadtest() { System.out.println("thread success with class"+getClass().getSimpleName()+"withthread name"+Thread.currentThread().getId()); } @AfterTest(groups = {"group2","group1"}) public void test5() {System.out.println("test5 success");}} public class Sample7 { @Test(priority=2,groups= {"group1"}) public void stest1() { System.out.println("stest1 success");} @Test(priority=1,groups= {"group2"}) public void stest2() { System.out.println("stest2 success");} @Test(priority=3, groups= {"group1"}) public void stest3() { System.out.println("stest3 success ");} @Test(priority=4, groups= {"group1", "group2"}) public void threadtest() { System.out.println("thread success with class"+getClass().getSimpleName()+"withthread name"+Thread.currentThread().getId());} }
  • 19. Data driven framework using TestNG • Third party support needed in data driven framework like POI, JXl etc. • Annotation- Data Provider is used for data connection. • Excel should be created on defined location. public class DataDrivern { @Test(dataProvider ="testdata") public void login(String username, String Password) {String status; WebDriver driver = new FirefoxDriver(); driver.get("http://lms.nagarro.com/"); WebElement element = driver.findElement(By.id("username")); element.sendKeys(username); element = driver.findElement(By.id("password")); element.sendKeys(Password); element = driver.findElement(By.name("submit")); element.click(); if( driver.getTitle().contains("Nagarro Learning Management System")) {status= "Pass"; driver.findElement(By.linkText("Log out")).click();} else { status= "Fail"; } Assert.assertEquals(status, "Pass"); driver.close();} @DataProvider(name="testdata") public Object[][] Readexcel()throws BiffException, IOException { File f = new File ("E:/input.xls"); Workbook w = Workbook.getWorkbook(f); Sheet s= w.getSheet(0); int rows = s.getRows(); int columns =s.getColumns(); String InputData [][] = new 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(); }}return InputData;}}

Hinweis der Redaktion

  1. Open Help Select Install New software
  2. Select TestNG site. Click Finish
  3. HTML report is part of test-output folder.
  4. This is sample example to run TestNg.
  5. TestNG support execution of test in different manner. You can design any scenarios as per requirement. Priority, dependsonMethod allow you to design scenario.
  6. This example show execution of test based on priority. Priority =-1 will execute first. But we see enable status is false, this test will not execute.
  7. This example clearly say that search method is dependent to login. And log out is dependent to search. Execution will take place as login search logout.
  8. 1. Skip: As login skip is dependent to search. It should executed after search. In current scenario we see search will fail. Hence testing will skip Logoutskip Method. 2. Logout also dependent to search, but alwaysrun is true so it will ecute always without considering fail or pass of search. But it will execute after search.