Annotation & Description
- @BeforeMethod: A method with this annotation will be executed before every @test annotated method.
- @AfterMethod: This annotation will be executed after every @test annotated method.
- @BeforeClass: This annotation will be executed before first @Test method execution. It runs only once per class.
- @AfterClass: This annotation will be executed after all the test methods in the current class have been run
- @BeforeTest: A method with this annotation will be executed before first @Test annotated method.
- @AfterTest: A method with this annotation will be executed when all @Test annotated methods complete the execution of those classes which are inside <test> tag in TestNG.xml file.
- @BeforeSuite: This annotation will run only once before all tests in the suite have run
- @AfterSuite: A method with this annotation will run once after the execution of all tests in the suite have run
- @BeforeGroups: This annotated method will run before the first test run of that specific group.
- @AfterGroups: This annotated method will run after all test methods of that group completes its execution.
@Test
This is the main part of our automation script where we will write the business logic, the things which we want to automate. We can pass attributes to our test method.
Below are the lists of attributes that we can pass to our Test method:
- alwaysRun : This is used when we want to make sure a method always runs even if the parameters on which the method depends, fails. If set to true, this test method will always run. Eg: @Test(alwaysRun = true)
- dataProvider: TestNG dataProvider is used to provide any data for parameterization. Eg. @Test(dataProvider = “Hello”).
E.g.
public class SameClassDataProvider
{
@DataProvider(name = "data-provider")
public Object[][] dataProviderMethod() {
return new Object[][] { { "data one" }, { "data two" } };
}
@Test(dataProvider = "data-provider")
public void testMethod(String data) {
System.out.println("Data is: " + data);
}
}
- dataProviderClass: This is the class from where we pass the data to data provider. In our case dataProvider class name is “Hello”.
DataProvider.java
import org.testng.annotations.DataProvider;
public class DataProviderClass
{
@DataProvider(name = "data-provider")
public static Object[][] dataProviderMethod()
{
return new Object[][] { { "data one" }, { "data two" } };
}
}
TestClass.java
import org.testng.annotations.Test;
public class TestClass
{
@Test(dataProvider = "data-provider", dataProviderClass = DataProviderClass.class)
public void testMethod(String data)
{
System.out.println("Data is: " + data);
}
}
- dependsOnGroups: It is the list of groups this method depends on. Eg: @Test (groups = { “City” ,”State” })
- dependsOnMethods: This command is used to execute a method based on its dependent method. Eg: @Test (dependsOnMethods = { “OpenBrowser” ,”database is up” })
- description: It is the description for the method. Eg: @Test(description = “test method”)
- invocationCount: It refers to the number of times a method should be invoked. It will work as a loop. Eg: @Test(invocationCount = 7) . Hence, this method will execute 7 times.
- invocationTimeOut: This refers to the maximum number of milliseconds a method should take for all the invocationCount to complete. This attribute will be ignored if invocationCount is not specified. Eg: @Test(invocationCount =7,invocationTimeOut = 30 )
- priority: This command sets the priority of the test method. Lower priorities will be scheduled first. Eg: @Test(priority =1 )
import static org.testng.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class AnnotationsTestNG {
public WebDriver driver;
public String url="https://www.lambdatest.com/";
@BeforeSuite
public void setUp()
{
System.setProperty("webdriver.chrome.driver", "C:\\Users\\navyug\\workspace\\QAPractise\\src\\ChromeDriver\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
System.out.println("The setup process is completed");
}
@BeforeTest
public void profileSetup()
{
driver.manage().window().maximize();
System.out.println("The profile setup process is completed");
}
@BeforeClass
public void appSetup()
{
driver.get(url);
System.out.println("The app setup process is completed");
}
@BeforeMethod
public void checkLogin()
{
driver.get("https://accounts.lambdatest.com/login");
driver.findElement(By.xpath("//input[@name='email']")).sendKeys("sadhvisingh24@gmail.com");
driver.findElement(By.xpath("//input[@name='password']")).sendKeys("activa9049");
driver.findElement(By.xpath("//*[@id='app']/section/form/div/div/button")).click();
System.out.println("The login process on lamdatest is completed");
}
@Test(groups="urlValidation")
public void testCurrentUrl() throws InterruptedException
{
driver.findElement(By.xpath("//*[@id='app']/header/aside/ul/li[4]/a")).click();
Thread.sleep(6000);
String currentUrl= driver.getCurrentUrl();
assertEquals(currentUrl, "https://automation.lambdatest.com/timeline/?viewType=build&page=1", "url did not matched");
System.out.println("The url validation test is completed");
}
@AfterMethod
public void screenShot() throws IOException
{
TakesScreenshot scr= ((TakesScreenshot)driver);
File file1= scr.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file1, new File("C:\\Users\\navyug\\workspace\\QAPractise\\test-output\\test1.PNG"));
System.out.println("Screenshot of the test is taken");
}
@AfterClass
public void closeUp()
{
driver.close();
System.out.println("The close_up process is completed");
}
@AfterTest
public void reportReady()
{
System.out.println("Report is ready to be shared, with screenshots of tests");
}
@AfterSuite
public void cleanUp()
{
System.out.println("All close up activities completed");
}
@BeforeGroups("urlValidation")
public void setUpSecurity() {
System.out.println("url validation test starting");
}
@AfterGroups("urlValidation")
public void tearDownSecurity() {
System.out.println("url validation test finished");
}
}
No comments:
Post a Comment