86. What are the advantages of using TestNG in Selenium?
Answer:
TestNG offers several powerful features that enhance Selenium automation:
- Provides various assertions to compare actual and expected outcomes.
- Allows parallel execution of test methods.
- Enables method dependency using dependsOnMethods.
- Supports prioritizing test cases using the priority attribute.
- Facilitates grouping of tests for structured execution.
- Allows data-driven testing using @DataProvider.
- Offers built-in reporting capabilities.
- Supports parameterization of test cases using @Parameters.
🔹 Q87. What is the purpose of testng.xml in TestNG?
Answer:
testng.xml is the configuration file for TestNG. It is used to:
- Create test suites and test groups
- Configure parallel execution
- Attach listeners
- Pass parameters to test scripts
It acts as a central place to control the execution flow of your test suite.
🔹 Q88. How can you pass parameters to test scripts using TestNG?
Answer:
Parameters can be passed using:
- @Parameters annotation in the test class
- <parameter> tag in the testng.xml file
Example:
xml
<parameter name=”browser” value=”chrome” />
java
@Parameters(“browser”)
public void setup(String browser) {
// Use the passed parameter
}
🔹 Q89. How can you create a Data-Driven Framework in TestNG?
Answer:
You can use @DataProvider annotation to create a data-driven framework. It allows running the same test multiple times with different sets of data.
Example:
java
@DataProvider(name = “loginData”)
public Object[][] dataProviderMethod() {
return new Object[][] { { “user1”, “pass1” }, { “user2”, “pass2” } };
}
@Test(dataProvider = “loginData”)
public void loginTest(String username, String password) {
// Login logic here
}
🔹 Q90. What is the use of TestNG Listeners?
Answer:
Listeners in TestNG are used to perform actions based on test events (like success, failure, skip).
Key points:
- Implement ITestListener or use TestListenerAdapter
- Useful for logging and custom reports
- Common methods: onTestSuccess(), onTestFailure(), onTestSkipped()
🔹 Q91. What is the role of @Listeners annotation in TestNG?
Answer:
The @Listeners annotation is used to associate a listener class with a test class.
Steps:
- Create a listener class implementing ITestListener
- Annotate the test class with @Listeners(YourListener.class)
This enables automatic listener behavior during test execution.
🔹 Q92. How can you make one test method dependent on another in TestNG?
Answer:
Use the dependsOnMethods attribute in the @Test annotation.
Example:
java
@Test
public void login() {}
@Test(dependsOnMethods = { “login” })
public void dashboard() {}
The dashboard() test will run only after successful execution of login().
🔹 Q93. How to set test case priority in TestNG?
Answer:
Test case priority can be set using the priority attribute in the @Test annotation.
Example:
java
@Test(priority = 1)
public void testA() {}
@Test(priority = 2)
public void testB() {}
Tests will execute in the order of their assigned priority (lower value = higher priority). Default priority is 0.
🔹 Q94. What are the commonly used TestNG annotations?
Answer:
Below are frequently used annotations in TestNG:
Annotation | Purpose |
@Test | Marks a test method |
@BeforeMethod | Runs before each test method |
@AfterMethod | Runs after each test method |
@BeforeClass | Runs before the first method in class |
@AfterClass | Runs after the last method in class |
@BeforeTest | Runs before any test case starts |
@AfterTest | Runs after all test cases |
@BeforeSuite | Runs before the suite starts |
@AfterSuite | Runs after the suite ends |
🔹 Q95. What are some common assertions in TestNG?
Answer:
TestNG provides multiple assert methods to verify conditions:
- assertEquals(actual, expected, message)
- assertNotEquals(data1, data2, message)
- assertTrue(condition, message)
- assertFalse(condition, message)
- assertNotNull(object)
- fail(message) – to fail a test explicitly
These assertions help validate the behavior of the application under test.
Q96. How can we run test cases in parallel using TestNG?
Answer:
To run tests in parallel, use the following configuration in testng.xml:
xml
<suite name=”Suite” parallel=”methods” thread-count=”5″>
<test name=”Test1″>
<classes>
<class name=”TestClass” />
</classes>
</test>
</suite>
Here, parallel=”methods” can also be replaced with tests or classes, depending on how you want to parallelize the execution. thread-count defines the number of parallel threads.
🔹 Q97. Name an API used for reading and writing data to Excel files.
Answer:
Two widely used APIs for handling Excel files in Java are:
- Apache POI: A popular library for reading, writing, and updating Excel files in .xls and .xlsx formats.
- JXL (Java Excel API): Another API used for Excel file manipulation but limited to .xls format.
🔹 Q98. Name an API used for logging in Java.
Answer:
Log4j is an open-source logging API commonly used in Java for logging events. It supports multiple logging levels such as:
- ALL
- DEBUG
- INFO
- WARN
- ERROR
- TRACE
- FATAL
🔹 Q99. What is the use of logging in automation?
Answer:
Logging in automation helps:
- Debugging: Logs provide detailed information about the execution flow, making it easier to identify and fix issues.
- Test Reports: Logs capture the runtime behavior of tests, offering insights into test performance and outcomes.
- Failure Analysis: Logs are useful for analyzing failures and understanding the causes of test issues.
🔹 Q100. What is invocationCount in TestNG?
Answer:
The invocationCount attribute in TestNG defines how many times a test method should be invoked. It allows a test to run multiple times without changing the test code.
Example:
java
@Test(invocationCount = 5)
public void testMethod() {
// This method will be executed 5 times
}
🔹 Q101. How can we run a test method multiple times in a loop (without using a data provider)?
Answer:
You can use the invocationCount parameter in the @Test annotation to run a test method multiple times in a loop. Simply set the invocationCount to the desired number of iterations.
Example:
java
@Test(invocationCount = 10)
public void repeatTest() {
// The test will run 10 times
}
🔹 Q102. What is the default priority of test cases in TestNG?
Answer:
The default priority for TestNG tests, when not specified, is 0. If two tests have the same priority, they will execute in the order they are defined.
🔹 Q103. What is the difference between soft assertion and hard assertion in TestNG?
Answer:
- Soft Assertion (SoftAssert): Multiple assertions can be made in a test method. Even if one assertion fails, the test continues executing and all assertions are collected. The softAssert.assertAll() method collates the results at the end.
- Hard Assertion: The test stops execution as soon as a failure occurs. No further steps are executed within the test method after a failed assertion.
🔹 Q104. How to fail a TestNG test if it doesn’t get executed within a specified time?
Answer:
You can use the timeOut attribute in the @Test annotation to specify the maximum time a test method should take. If the test exceeds the timeout, it will fail with a timeout exception.
Example:
java
@Test(timeOut = 5000)
public void longRunningTest() {
// The test will fail if it takes more than 5000 ms
}
👉The Next 20 Questions-IV: SELENIUM
👉The Next 20 Questions-V: SELENIUM
👉The Next 20 Questions-VI: SELENIUM