Other Useful Links:
- Manual Testing Interview Questions (01-40)
- Manual Testing Interview Questions (41-80)
- Selenium With Java Interview Questions - (01-35)
- Selenium With Java Interview Questions - (36-70)
- Java Interview Questions for QA (01-50)
- Java Interview Questions for QA (51-100)
- Java Programs for QA Automation Interview (01-15)
- Java Programs for QA Automation Interview (16-30)
- Maven Interview Questions for QA Automation Interview (01-15)
- Maven Interview Questions for QA Automation Interview (16-30)
- Most Frequently Asked API Testing Interview Questions (01-20)
- Most Frequently Asked API Testing Interview Questions (21-40)
- If we expect errors from some methods, we can include the exception in the @Test annotation so that the test case does not fail.
- Example: If a method is intended to throw the "numberFormatException" exception, the test case will fail since there is no try-catch block given. However, using the "expectedException" feature in TestNG, we can accomplish this.
- By using throw new SkipException()
- When SkipException() is issued, the remaining test method code is not executed, and control is passed to the next test method.
- throw new SkipException("Skip");
- It accepts two strings.
- The test case executes successfully if both strings are equal; else, the test case fails.
- It accepts two strings.
- The test case succeeds if both strings are equal, otherwise it fails.
- If a test case fails, a message is printed.
- It accepts two boolean values.
- The test case executes successfully if both boolean values are equal; else, the test case fails.
- It accepts a boolean value.
- If the condition is True, the assertion succeeds; otherwise, an assertion error is displayed.
- It accepts a boolean value.
- If the condition is True, the assertion succeeds; otherwise, an assertion error is displayed.
- It accepts a boolean value.
- If the condition is True, the assertion succeeds; otherwise, an assertion error with the message is displayed.
- It accepts a boolean value.
- If the condition is False, the assertion succeeds; otherwise, an assertion error with the message is displayed.
- Listeners are a type of code in TestNG that listens for specific events and then executes the code associated with that event.
- As a result, we can adjust TestNG's default behavior using TestNG listeners. Furthermore, in TestNG, the tester has access to a large number of listeners with various features.
- With the help of TestNG listeners, a customized report is generated.
- We may control events like method start, method pass, fail, and so on using the TestNG interface ITestListener, and a tester can report relevant messages based on these occurrences.
- It specifies the order in which all test cases are executed.
- It allows you to arrange test cases and execute them according to the specifications.
- It executes the selected test cases.
- Listeners can be implemented at the suite level in TestNG.
- It enables you to use the TestNG framework with other tools like Jenkins.
- array of object (Object [ ][ ])
36. How to re-execute failed test cases using TestNG?
Run testng-failed.xml
- After the first run of an automated test run, right click on the project and then click on refresh.
- A folder will be created named ‘test-output’
- Inside the test output folder you will find testng-failed.xml.
- Run testng-failed.xml to execute failed test cases
IRetryAnalyzer
To implement IRetryAnalyzer, create a class called RetryFailedTestCases.
Import org.testng.IRetryAnalyzer;
Import org.testng.ITestResult;
public class RetryFailedTestCases implements IRetryAnalyzer{
private int retrycount=0;
private int maxretrycount=0;
public boolean retry(ITestResult result){
if(retrycount<maxretrycount){
System.out.println(“Retrying:”+result.getName());
retrycount++;
return true;
}
return false;
}
}
Import org.testng.IRetryAnalyzer;
Import org.testng.IAnnotationTransformer;
Import org.testng.annotations.*;
public class RetryListnerClass implements IAnnotationTransformer{
@override
public void transform(ITestAnnotation testannotation,Class testclass, Constructor testconstructor, Method testmethod){
IRetryAnalyzer retry = testannotation.getRetryAnalyzer();
if(retry == null){
testannotation.setRetryAnalyzer(RetryFailedTestCases.class);
}
}
}
TestNG.xml:
<listners>
<listner class-name=”packagename.RetryListnerClass”/>
<listners>
- tests: Test cases inside <test> tag of testng.xml file will run parallel
- classes: Test cases inside a java class will run parallel
- methods: Methods marked with the @Test annotation will run in parallel.
- instances: In the same instance, test cases will run on parallel, but two methods from two different instances will run in separate threads.
38. Running Test Methods Parallely in TestNG
import org.openqa.selenium.*;
import org.testng.annotations.*;
public class ParallelTestDemo{
public WebDriver driver;
@Test
public void FirefoxTest(){
driver = new FireFoxDriver();
driver.get(“http://www.demo.com”);
driver.findElement(By.id(“Button”)).click();
driver.close();
}
@Test
public void ChromeTest(){
driver = new ChromeDriver();
driver.get(“http://www.demo.com”);
driver.findElement(By.id(“Button”)).click();
driver.close();
}
}
TestNG.xml:
<suite name=”Parallel testing suite”>
<test name=”parallel tests” parallel=”methods”>
<classes>
<class name=”ParallelTestDemo”/>
</classes>
</test>
</suite>
- If parallel mode is enabled, the thread-count attribute in TestNG is utilised to run the maximum number of threads for each suite (otherwise ignore it).
- Thread-count = "2" for example: Your tests will be executed in two threads.
Greetings, reader! Your input is highly important to us. Please share your thoughts in the comments section below.
Contact:
Email: piyushagrawal.automation@gmail.com
Follow on LinkedIn: Piyush Agrawal - LinkedIn
Follow on YouTube: Piyush Agrawal - Youtube
No comments:
Post a Comment