TestNG Concepts
TestNG:
TestNG is an automation framework to run the test cases, test suites and do parallel testing. It is an advanced version of Junit. It consists of many features like annotations, groups, parameterization and listeners. It generates html reports as well which is an advanced feature.
Annotations of TestNg:
There are different types of annotations present in TestNG which are used for organized and reusable tests.
- @BeforeSuite: The method which has this annotation will run before all the tests in the suite runs
- @AfterSuite: The method which has this annotation will run after all the tests in the suite runs
- @BeforeTest: The method which has this annotation will run before the tests present inside the class tag of testing.xml is run
- @AfterTest: The method which has this annotation will run after the tests present inside the class tag of testing.xml is run
- @BeforeClass: The method which has this annotation will run before the first test in the class is run
- @AfterClass: The method which has this annotation will run after all tests in the class are run
- @BeforeMethod: The method which has this annotation will run before each test method runs
- @AfterMethod: The method which has this annotation will run after each test method runs
- @Test: This is the main part of the test case
- @DataProvider: This annotation is used to extract the data from excel and send it to all test methods.
- @Parameters : To take the inputs from TestNg xml
Example:
<suite name="SampleTestNGXml">
<test name="TestNGAnnotationsExample">
<parameter name="browser" value="Firefox"/>
<parameter> name=”Url” value=” https://www.google.com”
<classes>
<class name=" TestNgExample" />classes>test>suite>
public class TestNgExample()
{
@Parameters({“browser”})
@BeforeSuite
public void setup(String browser){
if(browser.equlas(“Firefox”)){
WebDriver driver = new FirefoxDriver();
}
else if(browser.equals(“chrome”)){
System.setProperty(“webDriver.chrome.driver”,path);
WebDriver driver = new ChromeDriver();
}
else if(browser.equals(“IE”)){
System.setProperty(“webDriver.ie.driver”,path);
WebDriver driver = new InternetExplorerDriver();
}
}
@BeforeClass
Public void browserSetup(){
DesiredCapabilities dc = new DesiredCapabilities.firefox();
driver = new FirefoxDriver(dc);
driver.manage().timeouts().implicitlywait(60,TimeUnit.SECONDS);
driver.manage().window().setSize(new Dimension(1920,1080));
}
@Parameters({“Url”})
@BeforeTest
public void beforeTest(String url){
driver.get(url);
driver.manage().window().maximize();
}
@DataProvider(name = “sample”)
public object[] getData(){
return new Object[]{
{“TestNg”}}}
@Test(dataProvider=”sample”)
Public void minTest(String searchText){
WebElement searchBox = driver.findElement(By.Id(“fakebox-input”));
searchBox.sendKeys(searchText);
}
@AfterTest
Public void afterTest(){
System.out.println(“any reporting activities using extent Reports”);
}
@AfterClass
Public void afterClass(){
driver.close();
}
@AfterSuite
Public void teardown(){
driver.quit();
}
}
List of Attributes which we use in @Test method:
- alwaysRun: If we want to run a @Test method always, we can set this attribute to true
eg: @Test(alwaysRun= true)
- enabled: If we don’t want a @Testmethod run, we can set the enabled attribute as false
eg: @Test(enabled = false)
- Priority: If we want to run our test cases based on priority ,we can use this attribute.
eg: @Test(Priority = 1)
- groups: If we want to group together @Test methods, we can use this attribute
eg: @Test(groups = “smoketesting”)
- dependsOnGroups : If we want to run a particular @Test method after a particular group of @Test method runs, we can use this attribute
eg: @Test(dependsOnGroups = “smoketesting”)
- dependsOnMethods : If we want to run a particular @Test method after a particular @Test method runs, we can use this attribute
eg: @Test(dependsOnMethods=”testMethodName”)
Example:
Public class attributeExample(){
@Test(groups=”smoketesting”)
public void test1(){
System.out.println(“This method belongs to smoketesting group”);
}
@Test(groups=”smoketesting”)
public void test2(){
System.out.println(“This method belongs to smoketesting group”);
}
@Test(dependsOnGroups=”smoketesting”)
public void test3(){
System.out.println(“This method runs after smoketesting group methods run”);
}
@Test(dependsOnMethods=”test1”)
public void test4(){
System.out.println(“This method runs after test1 runs”);
}
@Test(enabled = false)
Public void test5(){
System.out.println(“This method will not run till test case is enabled”);
}
@Test(alwaysRun = true)
Public void test6(){
System.out.println(“This method always runs”);
}
@Test(priority=1)
Public void test7(){
System.out.println(“This method runs before any other test method runs as it has first priority”);
}
}
depends tag in testing.xml:
Till now we have seen dependsOnMethods and dependsOnGroups used for @Test methods to depend on groups and methods. But , we can use the depends on tag in testing.xml to perform the same operation without using the attribute.
Example:
Include&Exclude Tags in TestNg.Xml:
Include and Exclude Tags are used in testing.xml to include or exclude any @Test method to run
Example:
TestNG Listeners:
TestNG listeners are used to execute before or after @test method or suite runs. There are different types of TestNG listeners. Please find them below.
IsuiteListener: This is an interface which has two methods called onStart() and onFinish(). If a class implements this listener, these methods will run before and after the suite runs.
ITestListener: This interface is also similar to IsuiteListener but the difference is that it will run before and after Test runs. It consists of below methods.
onStart(): This method will run before @Test method runs
onFinish():This method will run after @Test method runs
onTestFailure(ITestResult result): runs whenever a test fails
onTestSkipped(ITestResult result) : runs whenever a test is skipped
onTestSuccess(ItestResult result): runs whenever a test is succeded
IInvokedMethodListener: This listener will run before and after every method is run. It consists of two methods.
beforeInvocation(): Invokes before each method runs
afterInvoction(): Invokes after each method runs
Example:
public class sampleListener implements ItestListener,IsuiteListener,IInvokeMethodListener{
public void onStart(ISuite suite){
System.out.println(“runs before suite is started”);
}
public void onFinish(ISuite suite){
System.out.println(“runs after suite is started”);
}
public void onStart(ITestContext testContext){
System.out.println(“runs before test is started running”);
}
public void onFinish(ITestContext testContext){
System.out.println(“runs after test is started running”);
}
public void onTestSuccess(ITestContext testContext){
System.out.println(testContext .getName()+“ is successful”);
}
public void onTestFailure(ITestContext testContext){
System.out.println(testContext .getName()+“ is failed”);
//Taking failed Test cases screenshots
File src = ((TakeScreensot)driver).getScreenshotAs(OutputType.FILE)
FileUtils.copyFile(src, new File(path));
}
public void beforeInvocation(IInvokedMethod method,ItestResult result){
System.out.println(“This method is invoked before every method runs”);
}
public void afterInvocation(IInvokedMethod method,ItestResult result){
System.out.println(“This method is invoked after every method runs”);
}
}
Including listener in Testngclass:
@Listeners(sampleListener)
public class testngSampleTest(){
@Test
public void test1(){
Including listener tag in TestNg.xml: