Selenium Java Interview Questions and Answers Part-18
1) Explain the usage of different annotations available in TestNG?
@BeforeSuite – Method will run before all tests run in the suite
@AfterSuite – Method will run after all tests run in the suite
@BeforeTest – Method will run before any test method within the class is run
@AfterTest – Method will run after all test methods within the class have run
@BeforeClass – Method will run before first test method in the class is invoked
@AfterClass – Method will run after all test methods in the class have run
@BeforeMethod – Method will run before each test method
@AfterMethod – Method will run after each test method
2) How do you prioritize your test cases in Selenium?
We can use priority method to set the priority of test method to run.
@Test(priority=1) public void ClickA(){ System.out.println("Test will execute first"); } @Test(priority=2) public void ClickB(){ System.out.println("Test will execute second"); }
3) Suppose you want to skip one test method from execution, how do you skip it from execution?
We can use enabled method to enable or disable test in TestNG.
@Test(enabled=false) public void login(){ }
4) What is the hierarchy of testNG.xml tags?
Below is the hierarchy of TestNG.xml tags:
<suite> <test> <classes> <class> <methods>
5) Explain the structure of testng.xml file?
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="Test Suite" verbose="1"> <test name="Regression Tests"> <classes> <class name="com.example.TestRunner"> </class> </classes> </test> </suite>
6) What are the different methods of Assert?
– assertEqual(): Compares two strings are equal and fails if both are not equal
– assertTrue(): Checks boolean condition is true and fails if it is false
– assertFalse(): Checks boolean condition is false and falis if it is true
7) How do you store your TestNG reports?
TestNG default reports are stored in target output folder of the project.
8) How do you use Parameters in TestNG?
@Parameters({"username","password"}) @Test public void login(String username, String password){ System.out.println(username); System.out.println(password); } <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="Test Suite" verbose="1"> <test name="Regression Tests"> <parameter name="username" value="qascript"/> <parameter name="password" value="qascript123"/> <classes> <class name="com.example.Tests"> </class> </classes> </test> </suite>
9) What is the use of grouping in TestNG?
TestNG groups are used to include/exclude different tests from the execution
10) What is the difference between JUnit and TestNG?
– More annotations are present in TestNG compared to JUnit
– HTML Reporting is present in TestNG but it is not present in JUnit
– TestNG supports dependent tests but Junit does not
11) What is the difference between include and exclude in TestNG?
– All test methods which are defined in the group under include tags will be included in the execution
– All test methods which are defined in the group under exclude tags will be excluded from execution
12) How to execute the single selected method in TestNG?
We can define a group for the method and put the group within the include tags
13) How the packages and classes are structured in TestNG.xml?
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="Test Suite" verbose="1"> <test name="Regression Tests"> <packages> <package name="com.example"/> </packages> </test> </suite>
14) What are assertions and why we go for them in TestNG?
Assertions are used in TestNG for validating the result or outcome of a test.
It can be used to compare the expected test result with actual test result.
Test Execution status will pass or fail based on the assertion result.
15) Tell me a login page script in TestNG?
@Test public void login(){ driver.findElement(By.id("username")).sendKeys("qascript"); driver.findElement(By.id("password")).sendKeys("qascript123"); driver.findElement(By.id("submit")).click(); }
16) What is the difference between @Parameters and @DataProviders in TestNG?
– @DataProvider can handle complex parameters like objects read from a property file or a database.
– @Parameters can be used to handle simple parameter values.
17) What problems you have faced while working with TestNG?
Working with DataProviders, Parallel execution and Listeners was challenging in TestNG.
18) How to create Suites in TestNG?
TestNG suite tags can be added in the testng.xml file.
19) How to prioritize the tests in TestNG at Class level and Suite level?
Priority can be set for test methods and then we can use @BeforeSuite, @BeforeClass methods to prioritize the tests at class and suite level.
20) Suppose I want to check a particular exception in TestNG. How will you check?
@Test(expectedExceptions = ArithmeticException.class) public void exceptionTest() { System.out.println("Exception occurred"); }
21) What is the difference between Maven and TestNG?
– Maven is a build automation tool used to build and manage Java projects.
– TestNG is a testing framework for Java to manage test execution.
22) Do you know any third party reporting other than testng?
There are many third-party reporting like Extent Reports, Maven Cucumber Reporting plugin.
23) What is the difference between TestNG and Grid?
– TestNG is a testing framework for Java to manage test execution.
– Grid is a selenium component which is used to execute tests on remote machines.
24) How will you mark as method as a data provider using TestNG annotation?
@Test(dataProvider="TestData")
25) Can you tell me usage of TestNG Soft Assertion?
Soft Assertion doesn’t throw any exception when an assert fails and the test execution doesn’t stop.
Next Steps:
> More interview questions and answers on Selenium Java, continue to the next post (Click on Next Post link below)
> Check complete Selenium Java interview questions and answers here (Click here)
Please leave your questions/comments/feedback below.
Happy Learning ?
Connect to me on Linked In (Click here)
On a mission to contribute to the Software Testing Community in all possible ways.