---Advertisement---

Automation Testing Interview Questions and Answers (Part-II)

By Manisha

Published On:

---Advertisement---

Q41. What is the syntax of a basic testng.xml file?

Answer:

A basic testng.xml file is used to configure and organize TestNG test execution. It allows you to define test suites, tests, and classes.

Syntax:

xml

<suite name=”MySuite”>

    <test name=”MyTest”>

        <classes>

            <class name=”com.project.tests.LoginTest”/>

            <class name=”com.project.tests.DashboardTest”/>

        </classes>

    </test>

</suite>

Key Tags:

  • <suite>: Defines the test suite.
  • <test>: Represents individual test configurations.
  • <classes>: Contains a list of classes to be executed.
  • <class>: Specifies the fully qualified class name of a test.


Q42. What is group execution in TestNG and how to achieve it?

Answer:

Group execution in TestNG allows you to organize and execute a subset of test methods using groups. This is helpful in categorizing tests like “smoke”, “regression”, “sanity”, etc.

Step-by-step Example:

java

@Test(groups = {“smoke”})

public void loginTest() {

    // code

}

@Test(groups = {“regression”})

public void dashboardTest() {

    // code

}

In testng.xml:

xml

<test name=”GroupExecution”>

    <groups>

        <run>

            <include name=”smoke”/>

        </run>

    </groups>

    <classes>

        <class name=”com.project.tests.LoginTest”/>

        <class name=”com.project.tests.DashboardTest”/>

    </classes>

</test>

Benefits:

  • Execute specific sets of tests
  • Efficient testing during CI/CD

Q43. What is parallel execution in TestNG and how to configure it?

Answer:

Parallel execution in TestNG enables running multiple tests or methods simultaneously, speeding up test execution time.

In testng.xml:

To run methods in parallel:

xml

<suite name=”ParallelTestSuite” parallel=”methods” thread-count=”3″>

    <test name=”ParallelMethods”>

        <classes>

            <class name=”com.project.tests.ParallelTest”/>

        </classes>

    </test>

</suite>

  • parallel=”methods” → Executes test methods in parallel.
  • thread-count=”3″ → Number of concurrent threads.

To run tests in parallel:

xml

<suite name=”Suite” parallel=”tests” thread-count=”2″>

    <test name=”Test1″>

        <classes>

            <class name=”com.project.tests.TestClass1″/>

        </classes>

    </test>

    <test name=”Test2″>

        <classes>

            <class name=”com.project.tests.TestClass2″/>

        </classes>

    </test>

</suite>


Q44. How to perform cross-browser testing in Selenium using TestNG?

Answer:

Cross-browser testing ensures your application works across different browsers. Using TestNG and the @Parameters annotation, we can pass browser types from testng.xml.

Step-by-step:

java

@Parameters(“browser”)

@BeforeMethod

public void setUp(String browser) {

    if (browser.equalsIgnoreCase(“chrome”)) {

        driver = new ChromeDriver();

    } else if (browser.equalsIgnoreCase(“firefox”)) {

        driver = new FirefoxDriver();

    }

}

In testng.xml:

xml

<test name=”ChromeTest”>

    <parameter name=”browser” value=”chrome”/>

    <classes>

        <class name=”com.project.tests.LoginTest”/>

    </classes>

</test>

<test name=”FirefoxTest”>

    <parameter name=”browser” value=”firefox”/>

    <classes>

        <class name=”com.project.tests.LoginTest”/>

    </classes>

</test>


Q45. How to disable a test method in TestNG?

Answer:

To disable or skip a test method temporarily in TestNG, use the enabled=false attribute in the @Test annotation.

Example:

java

@Test(enabled = false)

public void skipThisTest() {

    // This test will not run

}

Use Case:

  • Skipping tests temporarily for debugging
  • Avoid running unstable tests


Q46. How to execute the same test multiple times in TestNG?

Answer:

Use invocationCount attribute in @Test to execute the same test method multiple times.

Example:

java

@Test(invocationCount = 5)

public void repeatTest() {

    System.out.println(“Running multiple times”);

}

  • This test will execute 5 times.
  • Helpful for load testing or retry logic.


Q47. What is Assertion in TestNG? Provide examples used in real-time.

Answer:

Assertions in TestNG are used to validate expected vs actual outcomes. If the assertion fails, the test fails.

Common Assertions:

java

Assert.assertEquals(actualTitle, expectedTitle);

Assert.assertTrue(isElementVisible);

Assert.assertFalse(isErrorDisplayed);

Assert.assertNotNull(driver);

Real-time Example:

java

String actualTitle = driver.getTitle();

Assert.assertEquals(actualTitle, “Dashboard”);

boolean logoVisible = driver.findElement(By.id(“logo”)).isDisplayed();

Assert.assertTrue(logoVisible);


Q48. What is @Parameters in TestNG and how to use it?

Answer:

@Parameters allows you to pass values from testng.xml into test methods.

Usage:

java

@Parameters({“username”, “password”})

@Test

public void loginTest(String user, String pass) {

    System.out.println(“Username: ” + user);

}

In testng.xml:

xml

<parameter name=”username” value=”admin”/>

<parameter name=”password” value=”admin123″/>

Benefits:

  • Externalize test data
  • Useful for cross-browser or environment configurations

Q49. How to run the same test method with multiple data sets using DataProvider?

Answer:

Use @DataProvider to supply multiple sets of data to a test method.

Example:

java

@DataProvider(name = “loginData”)

public Object[][] dataSet() {

    return new Object[][] {

        {“admin”, “admin123”},

        {“user”, “user123”}

    };

}

@Test(dataProvider = “loginData”)

public void loginTest(String username, String password) {

    // Use data in test

}

Benefits:

  • Enables data-driven testing
  • Reusable for multiple test cases


Q50. What is @Listeners in TestNG and how is it used?

Answer:

@Listeners in TestNG allows you to implement custom code during test execution events (like test start, success, failure).

Steps:

  1. Create a listener class:

java

public class CustomListener implements ITestListener {

    public void onTestFailure(ITestResult result) {

        System.out.println(“Test Failed: ” + result.getName());

    }

}

  1. Register listener:

java

@Listeners(CustomListener.class)

public class MyTestClass {

    @Test

    public void testMethod() {

        Assert.fail(); // Triggers onTestFailure

    }

}

---Advertisement---

Leave a Comment