04 TestNG
TestNG Annotations
@BeforeSuite: It will run only once before all tests in the suite are executed.
@AfterSuite: A method with this annotation will run once after the execution of all tests in the suite is complete.
@BeforeTest: This will be executed before the first @Test annotated method. It can be executed multiple times before the test case.
@AfterTest: A method with this annotation will be executed when all @Test annotated methods complete the execution of those classes inside the <test> tag in the TestNG.xml file.
@BeforeMethod: This will be executed before every @test annotated method.
@AfterMethod: This will be executed after every @test annotated method.
@BeforeClass: This will be executed before first @Test method execution. It will be executed one only time throughout the test case.
@AfterClass: This will be executed after all test methods in the current class have been run
@BeforeGroups:
This method will run before the first test run of that specific group.
@AfterGroups: This method will run after all test methods of that group complete their execution.
- @BeforeSuite.
- @BeforeTest.
- @BeforeClass.
- @BeforeMethod.
- @Test.
- @AfterMethod.
- @AfterClass.
- @AfterTest.
- @AfterSuite
Normal Test Case: A normal test case is a test method without explicitly defined priority. TestNG executes these test methods in an arbitrary order, typically based on the method name or other factors.
@Factory: Make a method as a factory that returns objects that will be used by testNG as Test classes. The method must return object[];
@Listeners: Defines listeners on a test case.
@Parameters: Describe how to pass parameters to a @Test method.
@Test: Mark a class or a method as part of the test
TestNG Attribute
Priority →@Test(priority= -8)
Invocation Count → @Test(invocationCount=10)
Enabled(Ignore the test case)→ @Test(enabled=fales)
Always run: →@Test(alwaysRun =true)
Groups : → @test(groups=”grpname”)
Dependency: → (DependOnMethod and dependsOnGroups)
DependOnMethod: → @Test(dependsOnMethods= {"WebStudentLogin"})
dependsOnGroups: →@Test(dependsOnGroups="test")
Example @Test(groups="test")
public void testcase2() {}
@Test(dependsOnGroups="test")
public void testcase3() {}
Parameter: → @Parameters({“UserName”,”Passowd”})
Optional: → @Parameters({“UserName”,”Passowd”})
Public void sample(@Optional(“optionalValue”)String name String pass{}
DataProvider: →@DataProvider (name = "data-provider") → to set, generate, or create data
: → @Test (dataProvider = "data-provider")
DataProvider
: → @Test (dataProvider = "data-provider" dataProviderClass = "data.class")
Time out : @Test (timeOut = 1000)
expectedExceptions: @Test (expectedExceptions = IOException.class)
********************************************************************************************************************
Suite: → Collection of test cases
TestCases: → Collection of test Steps
Note: We can also convert to XML by using right the click class and selecting TestNg then selecting convert to TestNg
SUITE TESTS:
A Test suite is a collection of test cases that are intended to test a behavior or set of behaviors of software program. In TestNG, we cannot define a suite in testing source code, but it is represented by one XML file as a suite in the feature of execution. This also allows flexible configuration of the tests to be run. A suite can contain one or more tests and is defined by the <suite> tag.
Importance of testng.xml
It defines the order of the execution of all the test cases.
It allows you to group the test cases and can be executed as per the requirements.
It executes the selected test cases.
In TestNG, listeners can be implemented at the suite level.
It allows you to integrate the TestNG framework with tools such as Jenkins.
soft assertion and hard assertion?
Soft Assertion: In case of Soft Assertion, if TestNG gets an error during @Test, it will throw an exception when an assertion fails and continues with the next statement after the assert statement.
Hard Assertion: In the case of Hard Assertion, if TestNG gets an error during @Test, it will throw an AssertException immediately when an assertion fails and stops execution after the assert statement.
@Factory and @DataProvider annotation?
@DataProvider: It is annotation used by TestNG to execute the test method multiple numbers of times based on the data provided by the DataProvider.
@Factory: It is annotation used by the TestNG to execute the test methods present in the same test class using different instances of the respective class.
TestNG Annotation Attributes
description
timeout
priority
dependsOnMethod
enabled
groups
********************************************************************************************************************
Description – The ‘description’ attribute is used to provide a description to the test method. It generally contains a one-liner test summary.
@Test(description = "Test summary")
PRIORITY
When no 'priority' attribute is specified then the TestNG will run the test cases in alphabetical order. Priority determines the sequence of the execution of the test cases. The priority can hold the integer values between -5000 and 5000. When the priority is set, the lowest priority test case will run first and the highest priority test case will be executed last.
We can pass priority to the particular test case.
We can pass both positive and negative values.
It will execute based on ascending order.
If we give the Same priority then it will execute based on the alphabet order
@Test(priority= -8)
Public void sample(){}
@Test(priority= 10)
Public void sample1(){}
@Test(priority= 10)
Public void sample2(){}
@Test
Public void sample3(){}//Test with default priority 0”
@Test(priority= 100)
Public void sample4(){}
output:
Pass: sample
Pass: sample5
Pass: sample1
Pass: sample3
Pass: sample4
********************************************************************************************************************
Invocation Count
If we want to run the particular test case to run for many times, We can use one method called invocation test case.
It will run the test case for particular times
@Test(invocationCount=3)
Public void sample(){
}
@Test(invocationCount=2)
Public void sample1(){
}
outpt:
Passed: sample
Passed: sample
Passed: sample
Passed: sample1
Passed: sample1
********************************************************************************************************************
enabled/Ignoring the test case:
The 'enabled' attribute contains the boolean value. By default, its value is true. If you want to skip some test method, then you need to explicitly specify 'false' value.
To ignore the test case we can use one method called Enabled
When we use enabled= false, it will skip the particular test case
@Test(invocationCount=3,enabled=false)
Public void sample(){
}
@Test
Public void sample1(){
}
output
Passed: sample1
********************************************************************************************************************
Groups:
The 'groups' attribute is used to group the different test cases that belong to the same functionality.
We can group the multiple test cse by using group concept
We have to give groups name in the gest annotation
@test(groups=”grpname”)
Public class ParralelTest{
@Test(groups=”car”)
Public void sample1(){}
@Test
Public void sample2(){}
@Test(groups=”car”)
Public void sample3(){}
@Test
Public void sample4(){ }
@Test(groups=”car”)
Public void sample5(){ }
}
<suite name = “suite” parallel= “classes”> or parallel= “method” or parallel= “test”
<test name = “test1”>
<groups>
<define name = “one”>
<include name = “car”> </include>
</define>
<run>
<include name = “car”> </include>
</run>
</groups>
<classess>
<class name = “org.sample.ParralelTest”> </class>
</classess>
</test>
</suite>
Output
Passed : sample1
Passed : sample3
Passed : sample5
********************************************************************************************************************
TimeOut
If one of the test cases is taking a long time due to which other test cases are failing. To overcome such situation, you need to mark the test case as fail to avoid the failure of other test cases. The timeOut is a time period provided to the test case to completely execute its test case.
@Test(timeOut=200)
public void testcase1() throws InterruptedException
{
Thread.sleep(500);
System.out.println("This is testcase1");
}
********************************************************************************************************************
Re-execute the failed test:
When we know the particular test case is failed. We have to use RetryAnalyzer interface is used
Public class RetryFailed implements IRetryAnalyzer{
Privat int retrycount = 0;
Privat int retrymaxcunt= 3;
@Override
Public boolean retry(ITestResult arg0){
if(retrycount<retrymaxcount){
Retrycount++
return true;
}
return false ;
}
}
********************************************************************************************************************
Depends on metho (dependsOnMethod):
When the second test method want to be dependent on the first test method, then this could be possible by the use of “dependOnMehods” attribute. If the first test method fails, then the dependent method on the first test method, i.e., the second test method will not run
public class Class1 {
@Test
public void WebStudentLogin() {
System.out.println("Student login through web");
}
@Test
public void MobileStudentLogin()
{
System.out.println("Student login through mobile");
}
@Test(dependsOnMethods= {"WebStudentLogin"})
public void APIStudentLogin()
{
System.out.println("Student login through API");
}
}
********************************************************************************************************************
Parameter:
If we want to pass the input form XML sheet at that time parameters are used
We have to give@parameter annotation on the test case
When parameters are specific
@Test()
@Parameters(“apple”)
Public void sample(String fruitName,){
System.out.println(“Fruits name is ”+ fruitName)
}
When Parameters are applied below the tag
@Test()
@Parameters({“UserName”,”Passowd”})
Public void sample(String name, String password){
System.out.println(“user name is ”+ name)
System.out.println(“password is ”+ password)
}
@Test()
@Parameters({“UserName”,”Passowd”})
Public void sample1(String name, String pass){
System.out.println(“Test with normal method”)
System.out.println(“the second test name is ”+ name)
System.out.println(“the second test pass is ”+ pass)
}
In XML document
<suite name = “suite”>
<test thread-count= “5” name = “test1”>
<parameter name = “UserName” value= “mrchapai”> </parameter>
<parameter name = “Password” value= “abc123”> </parameter>
<classess>
<class name = “org.sample.ClassName”> </class>
</classess>
</test>
<test thread-count= “5” name = “test2”>
<parameter name = “UserName” value= “myusername”> </parameter>
<parameter name = “Password” value= “mypass123”> </parameter>
<classess>
<class name = “org.sample.ClassName”> </class>
</classess>
</test>
</suite>
Note: there are two tests (test 1 and test 2) that have two methods (sample and sample1)
Sute =1,Test = 2, Class.java=1 and method =2
Output
Test with normal method
The second test name is mrchapai
The second test pass is abc123
User name is mrchapai
User password is abc123
Test with normal method
The second test name is myusername
The second test pass is mypass123
User name is myusername
User password is mypass123
********************************************************************************************************************
Optional :
In case of parameter is not exactly matched @Optional is used
You have to pass the value at the time of initialization
@Test
@Parameters({“name”,”Passowd”}) // here is wrong parameter name name = “UserName
Public void sample(@Optional(“optionalValue”)String name String pass{
System.out.println(“user name is ”+ name)
System.out.println(“password is ”+ password)
}
Here I am wrongly pass the parameter and I pass the optional value to.
In XML document
<suite name = “suite”>
<test thread-count= “5” name = “test1”>
<parameter name = “UserName” value= “mrchapai”> </parameter>
<parameter name = “Password” value= “abc123”> </parameter>
<classess>
<class name = “org.sample.ClassName”> </class>
</classess>
</test>
</suite>
Output
User name is optionalValue
Password is abc123
********************************************************************************************************************
Data Provider in TestNG:
How To Use DataProvider In TestNG?
If you have understood the above-said points, using dataproviders is very easy. We will start with a straightforward and basic DataProvider test first. Observe the following code, which contains the
@DataProvider.
public class DP{
@DataProvider (name = "data-provider")
public Object[][] dpMethod(){
return new Object[][] {{"First-Value"}, {"Second-Value"}};
}
@Test (dataProvider = "data-provider")
public void myTest (String val) {
System.out.println("Passed Parameter Is : " + val);
}}
Inherited DataProvider In TestNG
Dataprovider and the test case method can also be in two different classes. It is inheriting the dataprovider since we are inheriting it from another file. It's required to slightly improve the above code to run the test case like this.
public class DataProvider {
@Test (dataProvider = "data-provider", dataProviderClass = DP.class)
public void myTest (String val) {
System.out.println("Current Status : " + val);
}
}
Pass Multiple Parameters
public class DProvider {
@DataProvider (name = "data-provider")
public Object[][] dpMethod(){
return new Object[][] {{2, 3 , 5}, {5, 7, 9}};
}
@Test (dataProvider = "data-provider")
public void myTest (int a, int b, int result) {
int sum = a + b;
Assert.assertEquals(result, sum);
}
DataProviders With Method As A Parameter
public class DProvider {
@DataProvider (name = "data-provider")
public Object[][] dpMethod (Method m){
switch (m.getName()) {
case "Sum":
return new Object[][] {{2, 3 , 5}, {5, 7, 9}};
case "Diff":
return new Object[][] {{2, 3, -1}, {5, 7, -2}};
}
return null;
}
@Test (dataProvider = "data-provider")
public void Sum (int a, int b, int result) {
int sum = a + b;
Assert.assertEquals(result, sum);
}
@Test (dataProvider = "data-provider")
public void Diff (int a, int b, int result) {
int diff = a - b;
Assert.assertEquals(result, diff);
}
}
DataProviderClass: This attribute is used to call the DataProvider method from another class.
********************************************************************************************************************
It defines the order of the execution of all the test cases.
It allows you to group the test cases and can be executed as per the requirements.
It executes the selected test cases.
In TestNG, listeners can be implemented at the suite level.
It allows you to integrate the TestNG framework with tools such as Jenkins.
description
timeout
priority
dependsOnMethod
enabled
groups
We can pass priority to the particular test case.
We can pass both positive and negative values.
It will execute based on ascending order.
If we give the Same priority then it will execute based on the alphabet order
If we want to run the particular test case to run for many times, We can use one method called invocation test case.
It will run the test case for particular times
To ignore the test case we can use one method called Enabled
When we use enabled= false, it will skip the particular test case
Groups:
The 'groups' attribute is used to group the different test cases that belong to the same functionality.
We can group the multiple test cse by using group concept
We have to give groups name in the gest annotation
@test(groups=”grpname”)
If one of the test cases is taking a long time due to which other test cases are failing. To overcome such situation, you need to mark the test case as fail to avoid the failure of other test cases. The timeOut is a time period provided to the test case to completely execute its test case.
When we know the particular test case is failed. We have to use RetryAnalyzer interface is used
When the second test method want to be dependent on the first test method, then this could be possible by the use of “dependOnMehods” attribute. If the first test method fails, then the dependent method on the first test method, i.e., the second test method will not run
If we want to pass the input form XML sheet at that time parameters are used
We have to give@parameter annotation on the test case
When parameters are specific
When Parameters are applied below the tag
In XML document
In case of parameter is not exactly matched @Optional is used
You have to pass the value at the time of initialization
Syntax:
@Test(dataProvider = "getData", dataProviderClass = Hello.class)
********************************************************************************************************************
Parallel Execution:
Thread-one person executes all the functions in the program.
Multi thread-more than one person will try to execute all the functions in the program parallel
The default thread count is 5
If you want to execute 10 test cases you have to set the test case as 10
Multiposting test
Run the test case in many browsers is called a multiposting test we can see what happens if we don’t give parallel execution
Ways to parallel execution
Test
Method
Classes
Org.sample;
Public class Invoc{
@Test
@Parameters({“name”,”Passowd”}) // here is wrong parameter name name = “UserName
Public void sample(@Optional(“optionalValue”)String name String pass{
}
Public void sample(String name, String password){
System.out.println(“user name is ”+ name)
System.out.println(“password is ”+ password)
System.out.println(“sample method is ”+ Thread.currentThread().getId());
}
}
Org.sample;
Public class ParralelTest{
@Test
Public void sample1(){
System.out.println(“sample method 1 is ”+ Thread.currentThread().getId());
}
@Test
Public void sample2(){
System.out.println(“sample method 2 is ”+ Thread.currentThread().getId());
}
@Test
Public void sample3(){
System.out.println(“sample method 3 is ”+ Thread.currentThread().getId());
}
@Test
Public void sample4(){
System.out.println(“sample method 4 is ”+ Thread.currentThread().getId());
}
@Test
Public void sample5(){
System.out.println(“sample method 5 is ”+ Thread.currentThread().getId());
}
}
In XML document
<suite name = “suite” parallel= “classes”> or parallel= “method” or parallel= “test”
<test name = “test1”>
<parameter name = “UserName” value= “mrchapai”> </parameter>
<parameter name = “Password” value= “abc123”> </parameter>
<classess>
<class name = “org.sample.ParralelTest”> </class>
<class name = “org.sample.Invoc”> </class>
</classess>
</test>
</suite>
Output
Sample method 1 is 13
Sample method 2 is 13
User name is mrchapai
Password is abc123
Sample method is 14
Method 4 is 13
Method 5 is 13
Note: in case depended method is false is skip the method
***************************************************************************************
@Listener
TestNG provides us different kinds of listeners using which we can perform some action in case an event has triggered. Usually, testNG listeners are used for configuring reports and logging.
@Listeners(PackageName.CustomizedListenerClassName.class)
public class TestClass {
WebDriver driver= new FirefoxDriver();
@Test
public void testMethod(){
//test logic
}
}
***************************************************************************************
@Factory
The @Factory annotation helps in the dynamic execution of test cases. Using @Factory annotation, we can pass parameters to the whole test class at run time. The parameters passed can be used by one or more test methods of that class.
In the below example, the test class TestFactory will run the test method in TestClass with a different sets of parameters – ‘k1’ and ‘k2’.
public class TestClass{
private String str;
//Constructor
public TestClass(String str) {
this.str = str;
}
@Test
public void TestMethod() {
System.out.println(str);
}
}
public class TestFactory{
//Because of @Factory, the test method in class TestClass
//will run twice with data "k1" and "k2"
@Factory
public Object[] factoryMethod() {
return new Object[] { new TestClass("K1"), new TestClass("k2") };
}
}
********************************************************************************************************************
TestNG Listeners:
TestNG provides the @Listeners annotation which listens to every event that occurs in a selenium code. Listeners are activated either before the test or after the test case. It is an interface that modifies the TestNG behavior. For example, when you are running a test case either through selenium or appium and suddenly a test case fails. We need a screenshot of the test case that has been failed, to achieve such scenario, TestNG provides a mechanism, i.e., Listeners. When the test case failure occurs, then it is redirected to the new block written for the screenshot.
Listeners are implemented by the ITestListener interface. An ITestListener interface has the following methods:
onTestStart(): An onTestStart() is invoked only when any test method gets started.
onTestSuccess(): An onTestSuccess() method is executed on the success of a test method.
onTestFailure(): An onTestFailure() method is invoked when test method fails.
onTestSkipped(): An onTestSkipped() run only when any test method has been skipped.
onTestFailedButWithinSuccessPercentage(): This method is invoked each time when the test method fails but within success percentage.
onStart(): An onStart() method is executed on the start of any test method.
onFinish(): An onFinish() is invoked when any test case finishes its execution.
Create the TestNG Listeners
First case: First, we will create the listeners within the class.
Step1
Class1.java
Step 2
Listener.java
public class Listener implements ITestListener {
@Override
public void onTestStart(ITestResult result) {
// TODO Auto-generated method stub
}
@Override
public void onTestSuccess(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("Success of test cases and its details are : "+result.getName());
}
@Override
public void onTestFailure(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("Failure of test cases and its details are : "+result.getName());
}
@Override
public void onTestSkipped(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("Skip of test cases and its details are : "+result.getName());
}
@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("Failure of test cases and its details are : "+result.getName());
}
@Override
public void onStart(ITestContext context) {
// TODO Auto-generated method stub
}
@Override
public void onFinish(ITestContext context) {
// TODO Auto-generated method stub
}
}
When sum() test case has been passed, then TestNG calls the onTestSuccess() listener. When testtofail() test case has been failed, then TestNG calls the onTestFailure() listener.
Step 3:
Now, we create the testng.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Listeners">
<classes>
<class name="com.javatpoint.Class1"></class>
</classes>
</test>
</suite> <!-- Suite -->
Second case: Now we create the listeners by using testng.xml file.
public class Testcases {
@Test
public void testtopass()
{
Assert.assertTrue(true);
}
@Test
public void testtofail()
{
Assert.assertFalse(false);
}
}
public class Listener implements ITestListener {
@Override
public void onTestStart(ITestResult result) {
}
@Override
public void onTestSuccess(ITestResult result) {
System.out.println("Success of test cases and its details are : "+result.getName());
}
@Override
public void onTestFailure(ITestResult result) {
System.out.println("Failure of test cases and its details are : "+result.getName());
}
@Override
public void onTestSkipped(ITestResult result) {
System.out.println("Skip of test cases and its details are : "+result.getName());
}
@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
System.out.println("Failure of test cases and its details are : "+result.getName());
}
@Override
public void onStart(ITestContext context) {
}
@Override
public void onFinish(ITestContext context) {
}
}
*******************************************************************************************************************
How to re-run only failed test case using TestNG
Failed Test Re-run (by using testng-failed.xml)
Automatic test run after first start-up. Right click on the object – click Refresh.
A list named “test-output” will be created. You can find the “testng-failed.xml” file in the “test-output” folder.
To rerun the failed test, run “testng-failed.xml”.
Below is the sample program:
//Java Program for Failed test re-run by using testng-failed.xml
import org.testng.Assert;
import org.testng.annotations.Test;
public class OnlyFailedTestExecution {
@Test
public void testOne() {
Assert.assertTrue(true);
System.out.println("Pass Test Case");
}
@Test
public void testTwo() {
Assert.assertTrue(false);
System.out.println("Faile Test Case");
}
}
xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<classes>
<class name="OnlyFailedTestExecution"/>
</classes>
</test>
</suite>
1. Create a class to implement TestNG IRetryAnalyzer interface:
//Java program for Failed test re-run by implementing IRetryAnalyzer
//Java program for Failed test re-run by implementing IRetryAnalyzer
public interface IRetryAnalyzer {
/**
* Returns true if the test method has to be retried, false otherwise.
* @param result The result of the test method that just ran.
* @return true if the test method has to be retried, false otherwise.
*/
public boolean retry(ITestResult result);
}
This interface has only one method: public boolean Repeat(ITestResult result);
This method will be called when the test method fails. You can get test results from the estResult input parameter of this method as shown in the method definition above.
This method should return true if you want to rerun the failed test, or false if you do not want to rerun the test. The most common use of this dialog is to determine how many times to retry a failed test based on a fixed counter or complex logic based on your needs.
An easy-to-use interface is shown below.
package Tests;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
public class RetryAnalyzer implements IRetryAnalyzer {
int counter = 0;
int retryLimit = 4;
/*
* (non-Javadoc)
* @see org.testng.IRetryAnalyzer#retry(org.testng.ITestResult)
* This method decides how many times a test needs to be rerun.
* TestNg will call this method every time a test fails. So we
* can put some code in here to decide when to rerun the test.
* Note: This method will return true if a tests needs to be retried
* and false it not.
*/
@Override
public boolean retry(ITestResult result) {
if(counter < retryLimit)
{
counter++;
return true;
}
return false;
}
We now have a simple implementation of IRetryAnalyzer. Note the implementation of the retry method, which allows the failed test to be repeated 4 times. This is because we set retryLimit = 4;
Let’s see how to use it, there are two ways to include retry parameters in the test
By specifying retryAnalyzer value in the @Test annotation
By adding Retry analyser during run time by implementing on the of the Listener interfaces
→ In Test Method specify Retry Class:
Specifying the value of retryAnalyzer in the @Test annotation. We can do this using the following @Test(retryAnalyzer = “IRetryAnalyzer Implementation class”) expression.
import org.testng.Assert;
import org.testng.annotations.Test;
public class Test001 {
@Test(retryAnalyzer = Tests.RetryAnalyzer.class)
public void Test1()
{
Assert.assertEquals(false, true);
}
@Test
public void Test2()
{
Assert.assertEquals(false, true);
}
}
→ By adding Retry analyser during runtime by implementation of the listener interface.
The ITestAnnotationTransformer interface belongs to a large class of interfaces called TestNG Listeners.
This interface is used to programmatically annotate test methods at runtime. It calls each test variable method during the test run.
When we use IAnnotationTransformer, we add it as a listener to the TestNG runtime xml.
package Tests;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;
public class AnnotationTransformer implements IAnnotationTransformer {
@Override
public void transform(ITestAnnotation annotation,
Class testClass,
Constructor testConstructor,
Method testMethod) {
annotation.setRetryAnalyzer(RetryAnalyzer.class);
}
}
This interface is used to programmatically annotate test methods at runtime. It calls any attempt to change the path during the test run. An easy-to-use interface helps us create back testing tools.
package Tests;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;
public class AnnotationTransformer implements IAnnotationTransformer {
@Override
public void transform(ITestAnnotation annotation,
Class testClass,
Constructor testConstructor,
Method testMethod) {
annotation.setRetryAnalyzer(RetryAnalyzer.class);
}
}
After implementing IAnotationTransformer, we just need to add it as a listener in the xml test run. like this
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="RetryFailedTests" verbose="1" >
<listeners>
<listener class-name="Tests.AnnotationTransformer"/>
</listeners>
<test name="RetryMulitple">
<classes>
<class name="Tests.Test001"/>
</classes>
</test>
</suite>
We no longer need to specify the retryAnalyzer attribute in the @Test annotation. The new test will look like this
package Tests;
import org.testng.Assert;
import org.testng.annotations.Test;
public class Test001 {
@Test
public void Test1()
{
Assert.assertEquals(false, true);
}
@Test
public void Test2()
{
Assert.assertEquals(false, true);
}
}
********************************************************************************************************************
TestNg with cucumber
On pom xml
Add testng dependency
Selenium
Cucumber java
Cucumber jvm
Cucumber testng dependency
F
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>7.11.0</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
Add plugin
Maven-compiler-plugin
Maven-surefire-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
t
JUnit 4
https://www.scientecheasy.com/2020/07/testng-t est-annotation.html/
Thread-one person executes all the functions in the program.
Multi thread-more than one person will try to execute all the functions in the program parallel
The default thread count is 5
If you want to execute 10 test cases you have to set the test case as 10
Run the test case in many browsers is called a multiposting test we can see what happens if we don’t give parallel execution
Test
Method
Classes
TestNG provides the @Listeners annotation which listens to every event that occurs in a selenium code. Listeners are activated either before the test or after the test case. It is an interface that modifies the TestNG behavior. For example, when you are running a test case either through selenium or appium and suddenly a test case fails. We need a screenshot of the test case that has been failed, to achieve such scenario, TestNG provides a mechanism, i.e., Listeners. When the test case failure occurs, then it is redirected to the new block written for the screenshot.
Listeners are implemented by the ITestListener interface. An ITestListener interface has the following methods:
Automatic test run after first start-up. Right click on the object – click Refresh.
A list named “test-output” will be created. You can find the “testng-failed.xml” file in the “test-output” folder.
To rerun the failed test, run “testng-failed.xml”.
By specifying retryAnalyzer value in the @Test annotation
By adding Retry analyser during run time by implementing on the of the Listener interfaces
On pom xml
Add testng dependency
Selenium
Cucumber java
Cucumber jvm
Cucumber testng dependency
F
Maven-compiler-plugin
Maven-surefire-plugin
Comments
Post a Comment