I am using Gmail example to show . how Testng framework annotation is working in order.
Notes:-
1) Less test priority will execute first.
2) @BeforeMethod/Aftermethod will execute repetitively for each test.
3) A class can have more than one test.
4) A suite is made of one or more tests.
5) A test is made of one or more classes.
6) A class is made or one or more methods.
---------------------------------------------------------------------------------------------
package SachinGarg;
import junit.framework.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.*;
public class Gmail_TestNG_Annotation {
public WebDriver driver=new FirefoxDriver();
public String ex_Title= null;
public String ac_Title=null;
public boolean flag=true;
@BeforeTest
public void before_Test()
{
driver.get("http://www.gmail.com");
}
@BeforeMethod
public void befor_Method(){
System.out.println("Before method has be executed");
ex_Title="Gmail";
ac_Title=driver.getTitle();
//Generate assert if result does not match
Assert.assertEquals(ac_Title, ex_Title);
}
@Test ( priority =1)
public void Method_needhelp() {
System.out.println("Test method 1");
driver.findElement(By.linkText("Need help?")).click();
ex_Title="Google Account Recovery";
ac_Title=driver.getTitle();
//Generate assert if result does not match
Assert.assertEquals(ac_Title, ex_Title);
}
@Test ( priority =0)
public void Create_Account() {
System.out.println("Tets method 0");
driver.findElement(By.linkText("Create account")).click();
ex_Title="Create your Google Account";
ac_Title=driver.getTitle();
Assert.assertEquals(ac_Title, ex_Title);
}
@AfterMethod
public void After_Method(){
System.out.println("After Method executed");
if(flag)
{
driver.findElement(By.id("link-signin")).click();
flag=false;
}
}
@AfterTest
public void close_driver(){
System.out.println("driver quit");
driver.quit();
}
}
Notes:-
1) Less test priority will execute first.
2) @BeforeMethod/Aftermethod will execute repetitively for each test.
3) A class can have more than one test.
4) A suite is made of one or more tests.
5) A test is made of one or more classes.
6) A class is made or one or more methods.
---------------------------------------------------------------------------------------------
package SachinGarg;
import junit.framework.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.*;
public class Gmail_TestNG_Annotation {
public WebDriver driver=new FirefoxDriver();
public String ex_Title= null;
public String ac_Title=null;
public boolean flag=true;
@BeforeTest
public void before_Test()
{
driver.get("http://www.gmail.com");
}
@BeforeMethod
public void befor_Method(){
System.out.println("Before method has be executed");
ex_Title="Gmail";
ac_Title=driver.getTitle();
//Generate assert if result does not match
Assert.assertEquals(ac_Title, ex_Title);
}
@Test ( priority =1)
public void Method_needhelp() {
System.out.println("Test method 1");
driver.findElement(By.linkText("Need help?")).click();
ex_Title="Google Account Recovery";
ac_Title=driver.getTitle();
//Generate assert if result does not match
Assert.assertEquals(ac_Title, ex_Title);
}
@Test ( priority =0)
public void Create_Account() {
System.out.println("Tets method 0");
driver.findElement(By.linkText("Create account")).click();
ex_Title="Create your Google Account";
ac_Title=driver.getTitle();
Assert.assertEquals(ac_Title, ex_Title);
}
@AfterMethod
public void After_Method(){
System.out.println("After Method executed");
if(flag)
{
driver.findElement(By.id("link-signin")).click();
flag=false;
}
}
@AfterTest
public void close_driver(){
System.out.println("driver quit");
driver.quit();
}
}
Output is :-
[TestNG] Running:
C:\Users\sachinABCG\AppData\Local\Temp\testng-eclipse--1483946418\testng-customsuite.xml
Before method has be executed
Tests method 0
After Method executed
Before method has be executed
Test method 1
After Method executed
driver quit
PASSED: Create_Account
PASSED: Method_needhelp
After execution , open the auto generated report. Please see the screen shot.

No comments:
Post a Comment