1. Explain the Difference between BeforeTest and BeforeMethod annotations
BeforeTest: This method will execute only once in the entire execution before calling the first test method. It doesn’t matter how many test methods, or how many test methods are in a single class it executes only once.
BeforeMethod: This method will execute before every test method. Number of executions of BeforeMethod is similar to number of executions of test methods.
2. What is TestNG?
TestNG is a testing framework.
3. Explain soft assert in TestNG
SoftAssert collect errors when @Test is running. It doesn’t throw errors when assertions fail. Execution will continue with the next step. If we want to throw an error we can use the assertAll() statement as the last statement of the test script.
This is the package : org.testng.asserts.SoftAssert
- Create object from SoftAssert Class
- SoftAssert softassert = new SoftAssert();
- softassert.assertEquals()
- softassert.assertNotEqual()
- softassert.assertTrue()
- softassert.assertFalse()
- softassert.assertNull()
- softassert.assertNotNull()
- softassert.assertAll()
4. What are the key features of TestNG?
- Annotations
- Dependency handling
- Reporting
- Parallel execution
- Parameterization
- Grouping
- Prioritization of test
5. What is the purpose of the @Test method?
When we use the @Test annotation in a Java method testNg recognizes it as a test script and it can be used for test execution
@Test(groups = "reg")
public static int getIndexOfChar(String[] elements, String character)
6. What are the different testNG annotations?
- @Test: Marks a method as a test method
- BeoforeTest: This will execute once before all the test methods
- BeforeMethod: This will execute before each test method
- BeforeClass: will execute before each test class
- AfterClass: will execute after every test class
- AfterMethod: will execute after each test method
- AfterTest: execute only once after all test methods are executed
- BeforeSuite: execute before the test suite
- AfterSuite: execute after the test suite
7. Explain BeforeMethod and AfterMethod
When we use BeforeMethod annotation on top of a Java method, the method must execute before each test method.
When we use AfterMethod annotation on top of a Java method, the method must execute after each test method.
8. How do you specify a dependency in the test method?
- using dependOnMethod annotation
- dependsOnGroup(“CD-D14”)
- Here group name should be the test method name and that test method group name included below
@Test(groups = "reg", dependsOnGroups = "cd-d14")
public static int getIndexOfChar(String[] elements, String character)
@Test(groups = {"cd-d14", "data"}, dependsOnGroups = "cd-d14")
public static int cd-d14()
@Test(groups = "reg", dependsOnMethods = "data01")
public static int getIndexOfChar(String[] elements, String character)
9. How to parameterize test scripts?
- Using DataProvider and Parameterize annotations
- Use @Parameters annotatation
- Parameters(“name”)
- method(String name)
- Test suite XML <parameter name = “name” value=”kiyota”/>
- So here we can add only one parameter at a time
@Test(groups = "reg", dependsOnMethods = "data01")
@Parameters("elements")
public static int getIndexOfChar(String elements)
//xml file
<suite name="Regression Suite">
<test name="dataSet01">
<parameter name="elements" value="Tokyo"></parameter>
10. How to group test scripts?
Use group attributes with test annotations
@Test(groups = {"reg","smk"}
11. What types of reports are generated using TestNG annotations?
It generates an HTML report.
12. How to run single script with multiple data?
@Test(dataProvider = "dataSet01")
public static void getValues(String name)
//Data Set
@DataProvider(name = "dataSet01")
public static Object[][] getDataSet(){
return new Object[][] {{"kiyota"}, {"Sevi"}};
}
13. How do we define the order of methods if there are multiple methods in the same class?
- Use priority tag inside Test annotation — Test(priority = 0)
@Test(priority = 0)
public static void getValues(String name)
@Test(priority = 1)
public static void setValues(String name)
- Priority can be a negative number as well
- TestNG execute the lowest priority first
- Priority can be defined as 0, 1,2 ….
14. If priority is not provided what is the order of execution of methods in a single class?
Alphabetical order of test method names
15. How to skip a test script during the execution?
- Use enabled = false property inside of Test annotation
@Test(enabled = false)
public static void getValues(String name)
- Create separate group names and create separate test suite XML files and use the include name property in that XML file
@Test(groups = "failedTest")
public static void getValues(String name)
// testng.xml
<class name="com.example.MyTestClass">
<methods>
<include name="failedTest" />
</methods>
</class>
- use the exclude name property in the XML file
@Test(groups = "failedTest")
public static void getValues(String name)
// testng.xml
<class name="com.example.MyTestClass">
<methods>
<exclude name="failedTest" />
</methods>
</class>
16. How to achieve data-driven testing?
- Executing the same test case multiple times with different data sets
- We can use TestNG with parameter annotation
- We can use TestNG with DataProvider annotation
17. What is a hybrid framework?
It is a combination of frameworks, like data-driven and keyword-driven combinations.
18. How to customize test reports?
- Create listener
- Configure listener in testng.xml
<suite name="MyTestSuite">
<listeners>
<listener class-name="com.example.CustomListener" />
</listeners>
<!-- Your test configurations go here -->
</suite>
19. How to perform parallel test execution?
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite2" parallel="classes">
<!-- Test and class configurations go here -->
</suite>
20. What are the factors that need to be considered during parallel test execution?
- Different users add as user login
- Test scripts should not be conflicted
21. There are failures in your suite execution, where only a few test cases have passed. How will you optimize your upcoming test runs in Selenium?
Re-running only the failed test cases would be an optimal solution in this case. With the TestNG class, there are two methods to re-run only the failed test cases.
- In the test output folder, the testing-failed.xml suite file is generated. We can use this to run only the previously failed test cases.
- We can implement the TestNG IRetryAnalyzer interface. This can be done by creating a class ( say RetryAnalyzer) and implementing IRetryAnalyzer. Next, create a class ‘RetryListener’ by implementing an ‘IAnnotationTransformer’ interface, where the transform method is called for every test during the test run. Later, include the mentioned listeners in the testng.xml file.
22. What is the purpose of @listenerannotation in testNG?
- Defines listeners on a test class.
- Using listeners can be helpful for tasks like logging, reporting, or customizing the behavior of your test suite
import org.testng.ITestListener;
import org.testng.ITestResult;
public class MyTestListener implements ITestListener {
@Override
public void onTestStart(ITestResult result) {
System.out.println("Test Started: " + result.getName());
}
@Override
public void onTestSuccess(ITestResult result) {
System.out.println("Test Passed: " + result.getName());
}
@Override
public void onTestFailure(ITestResult result) {
System.out.println("Test Failed: " + result.getName());
}
// Other methods can be implemented based on your requirements
}
23. What are the methods in ITestListener and IInvokedMethodListener intefaces?
onFinish(ITestContext context)
Invoked after all the tests have run and all their Configuration methods have been called.onStart(ITestContext context)
Invoked after the test class is instantiated and before any configuration method is called.onTestFailedButWithinSuccessPercentage(ITestResult result)
Invoked each time a method fails but has been annotated with successPercentage and this failure still keeps it within the success percentage requested.onTestFailure(ITestResult result)
Invoked each time a test fails.onTestSkipped(ITestResult result)
Invoked each time a test is skipped.onTestStart(ITestResult result)
Invoked each time before a test will be invoked.onTestSuccess(ITestResult result)
Invoked each time a test succeeds.
Comments
Post a Comment