---Advertisement---

Selenium Interview Questions and Answers (Level-2)

By Manisha

Updated On:

---Advertisement---

86. What are the advantages of using TestNG in Selenium?

Answer:
TestNG offers several powerful features that enhance Selenium automation:

  1. Provides various assertions to compare actual and expected outcomes.
  2. Allows parallel execution of test methods.
  3. Enables method dependency using dependsOnMethods.
  4. Supports prioritizing test cases using the priority attribute.
  5. Facilitates grouping of tests for structured execution.
  6. Allows data-driven testing using @DataProvider.
  7. Offers built-in reporting capabilities.
  8. 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:

  1. Create a listener class implementing ITestListener
  2. 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:

AnnotationPurpose
@TestMarks a test method
@BeforeMethodRuns before each test method
@AfterMethodRuns after each test method
@BeforeClassRuns before the first method in class
@AfterClassRuns after the last method in class
@BeforeTestRuns before any test case starts
@AfterTestRuns after all test cases
@BeforeSuiteRuns before the suite starts
@AfterSuiteRuns after the suite ends

๐Ÿ”น Q95. What are some common assertions in TestNG?

Answer:
TestNG provides multiple assert methods to verify conditions:

  1. assertEquals(actual, expected, message)
  2. assertNotEquals(data1, data2, message)
  3. assertTrue(condition, message)
  4. assertFalse(condition, message)
  5. assertNotNull(object)
  6. 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:

  1. Apache POI: A popular library for reading, writing, and updating Excel files in .xls and .xlsx formats.
  2. 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

---Advertisement---

Leave a Comment