Selenium Java Interview Questions and Answers Part-21
1) What is the difference between WebDriver Listeners and TestNG Listeners?
TestNG work on test related events while WebDriver Listeners work on automation related events.
2) TestNG- Write sample code to select browser depending on parameter given in testing.xml?
We can define the browser parameter value in testing.xml:
<suite name="Test Suite"> <test name="Cross Browser Tests"> <parameter name="browser" value="Chrome"/> <classes> <class name="com.qascript.FirstTest"/> </classes> </test> </suite>
We can use the parameter value in our test:
@Parameters({ "browser" }) @Test public void browserTest(String browser) { if(browser=="Chrome"){ driver = new ChromeDriver(); } }
3) How will you analyze the failures after giving a run in the TestNG framework?
We need to add logging into our TestNG framework and open the HTML Report after test run to
analyze the logs in failed tests.
4) Can we run a test without testng?
Yes we can run test using other test frameworks like Junit or Cucumber
5) Why to use TestNG with Selenium WebDriver?
TestNG is used in Selenium WebDriver due to the following:
– To control the flow of test execution with the help of annotations
– To perform cross browser testing
– To generate HTML reports
– To perform parallel testing and multithreading
6) Explain DataProviders in TestNG using an example
DataProviders in TestNG is used to provide different sets of data to the tests.
Ex-
public class dataProviderExample{ @DataProvider(name="test-data") public Object[][] dataMethod(){ return new Object[][]{{"x"},{"y"}}; } @Test(dataProvider="test-data") public void test1(String value){ System.out.println(value); } }
7) Explain How does TestNG allow you to state dependencies with an example?
TestNG allows us to state dependencies using annotation @dependsOnMethods.
@Test(dependsOnMethods={"test2"}) public void test1(){ } @Test public void test2(){ }
8) List two different annotations present in TestNG but not in JUnit?
@BeforeSuite and @BeforeGroups are not present in Junit.
9) Have you conducted cross browser testing in a parallel way using TestNG?
Yes. We can perform cross browser testing in parallel with TestNG using parallel and thread-count attributes.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Regression" parallel="methods" thread-count="2"> <test name="ParallelTest"> <classes> <class name="com.qascript.example"/> </classes> </test> </suite>
10) What is parameterization in TestNG?
Parameterization can be done in 2 ways in TestNG:
– By using Parameters annotation and testng xml file
– By using DataProvider annotation
11) What are the types of assertion and what are assertion in junit?
Assertion is used to validate the test cases and Pass/Fail the test based on the result. Types of assertions are assertEquals, assertFalse, assertTrue.
12) Explain the different JUnit annotations mostly used while writing the Selenium scripts?
– @BeforeClass
– @Before
– @Test
– @After
– @AfterClass
– @Ignore
13) There are 300 test cases, I want to execute test cases in some custom order, how to change the order of execution without doing changes in testng.XML and in code(.class files). If we can do, tell the logic and if we cannot do, justify with reason?
We can execute the tests in custom order by setting priority of different tests.
Ex –
@Test(priority=0) public void test1(){ } @Test(priority=1) public void test2(){ }
14) Is it possible to pass test data through testng.xml file, if yes how?
Yes. We can pass parameter and their values from testng.xml file.
15) How to run specific kind of Test cases using TestNG?
We can use groups in TestNG to run specific kind of test cases.
16) Can we execute test cases in order without using TestNG?
No. It is not possible to execute test cases in specific order without using TestNG or other test frameworks.
17) What is the testng.xml file used for?
It is the configuration file which can be used to organize tests, control flow of execution and group different tests.
18) How the testng class’s execution happen?
TestNG class execution happens as follows:
– Firstly BeforeSuite() method is executed only once
– Lastly AfterSuite() method executes only once
– BeforeTest(), BeforeClass(), AfterClass(), and AfterTest() methods are executed only once
– BeforeMethod() and AfterMethod() method executes for each test case but before/after executing the test case
– Each @Test method is executed between BeforeMethod() and AfterMethod()
19) How to parameterized your junit?
The custom runner Parameterized implements parameterized tests.
@RunWith(Parameterized.class)
20) What is an assertion? What is its drawback? How to overcome it?
Assertion is a way of validating the expected outcome of any test case.
Drawback of assertion is whenever any test case fails it will throw an exception and stop the execution.
We can use soft assertion if we want to continue execution even if there are test failures.
21) How can you prepare customized HTML report using TestNG in hybrid framework?
We can prepare customized HTML report with the helpd of different listeners:
– org.testng.ITestListener
– org.testng.IReporter
22) How do you manage re-running only failed test cases?
We can rerun failed test cases in TestNG by implementing the org.testng.IRetryAnalyzer interface.
23) How will you install ReportNG in your project?
– Include ReportNG maven dependencies in the pom.xml
<dependency> <groupId>org.uncommons</groupId> <artifactId>reportng</artifactId> <version>1.1.4</version> <scope>test</scope> </dependency>
– Add listeners to testng.xml
<listeners> <listener class-name="org.uncommons.reportng.HTMLReporter"/> <listener class-name="org.uncommons.reportng.JUnitXMLReporter"/> </listeners>
24) What is the difference between @BeforeMethod and @BeforeTest method?
@BeforeMethod – Annotated method is run after each every test method
@BeforeTest – Annotated method is run before any test method belonging to the class
25) What is the difference between @BeforeMethod and @BeforeClass ?
@BeforeMethod – Annotated method is run after each every test method
@BeforeClass – Annotated method is run before the first test method in the current class.
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.