TestNG, as most of you know, is an automation framework widely used in Selenium. It is important for all testers to understand the annotations.
Annotations have a number of uses, among them:
- Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings.
- Compile-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.
- Runtime processing — Some annotations are available to be examined at runtime.
Annotations in TestNG can control the order of execution. @Test is one annotation which makes a method a test case.
Difference between before test before method.?
@BeforeTest is getting called only once when @test is invoked in a class
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
@BeforeMethod will be executed before every @test invoked
or
@BeforeTest : It will call Only once, before Test method.
@BeforeClass : It will call Only once, before Test method.
@BeforeMethod It will call Every time before Test Method.
**client: why do you use before test: you mug many test suit undrer regression test then in the xml file,you will use the test, in xml file you will merge differnet test differnt suit and before test comes on picture.
Order of testNg Annotation:
package com.automation.eng.AutomationProject;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TestNgAnnotation {
@BeforeSuite
public void beforeSuite() {
System.out.println("1st - BeforeSuite - Execute Only one times- always execute prior to all annotations or tests in the suite ");
}
@BeforeTest
public void beforeTest() {
System.out.println("2nd - BeforeTest - always execute prior to Before Class, ,Before Method and Test Method ");
}
@BeforeClass
public void beforeClass() {
System.out.println("3rd - BeforeClass - always execute prior to Before Method and Test Method");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("4th - BeforeMethod - Execute before every test method");
}
@Test(priority = 2)
public void verifyTiltle() {
System.out.println("Test : 2. Title is verified. - Pass");
}
@Test(priority = 1 )
public void verifyURL() {
System.out.println("Test : Passed - 1. URL is verified - Test is Pass");
}
@AfterMethod
public void afterMethod() {
System.out.println("AfterMethod - Execute after every test");
}
@AfterClass
public void afterClass() {
System.out.println("AfterClass - Execute once");
}
@AfterTest
public void afterTest() {
System.out.println("AfterTest - Execute once");
}
@AfterSuite
public void afterSuite() {
System.out.println("AfterSuite - Execute once at the last");
}
}
/*
1st - BeforeSuite - Execute Only one times- always execute prior to all annotations or tests in the suite
2nd - BeforeTest - always execute prior to Before Class, ,Before Method and Test Method
3rd - BeforeClass - always execute prior to Before Method and Test Method
4th - BeforeMethod - Execute before every test method
Test : Passed - 1. URL is verified - Test is Pass
AfterMethod - Execute after every test
4th - BeforeMethod - Execute before every test method
Test : 2. Title is verified. - Pass
AfterMethod - Execute after every test
AfterClass - Execute once
AfterTest - Execute once
PASSED: verifyURL
PASSED: verifyTiltle
===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================
AfterSuite - Execute once at the last
===============================================
Default suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
*/
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”).
- dataProviderClass: This is the class from where we pass the data to data provider. In our case dataProvider class name is “Hello”.
- 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 )
No comments:
Post a Comment