HomeAutomation

Selenium Java Interview Questions And Answers – Part 5

Selenium Java Interview Questions And Answers – Part 5

Selenium Java Interview Questions and Answers Part-5

1) What are the advantages of using TestNG?

– It provides parallel execution of test methods
– It allows to define dependency of one test method over other method
– It allows to assign priority to test methods
– It allows grouping of test methods into test groups
– It has support for parameterizing test cases using @Parameters annotation
– It allows data driven testing using @DataProvider annotation
– It has different assertions that helps in checking the expected and actual results
– Detailed (HTML) reports

2) Can you arrange the below testng.xml tags from parent to child?
<test>
<suite>
<class>
<methods>
<classes>

<suite><test><classes><class><methods>

 

3) What is the importance of testng.xml file?

TestNG.xml file is an XML file which contains all the Test configuration and this XML file can be used to run and organize our test.

4) What is TestNG Assert and list out common TestNG Assertions?

Asserts helps us to verify the conditions of the test and decide whether test has failed or passed. Some of the common assertions are:
– assertEqual(String actual,String expected)
– assertTrue(condition)
– assertFalse(condition)

5) What is Soft Assert in TestNG?

SoftAssert don’t throw an exception when an assert fails. The test execution will continue with the next step after the assert statement.

6) What is Hard Assert in TestNG?

Hard Assert throws an AssertException immediately when an assert statement fails and test suite continues with next @Test.

7) How to create Group of Groups in TestNG?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestNG Sample Test Suite">

  <test name="TestNG Sample Test">
    <groups>
      <define name="include-groups">
        <include name="sanity" />
        <include name="integration" />
      </define>
      <define name="exclude-groups">
        <include name="pass" />
        <include name="fail" />
      </define>
      <run>
        <include name="include-groups" />
        <exclude name="exclude-groups" />
      </run>
    </groups>
    <classes>
      <class name="com.groups.TestNGGroupsExample" />
    </classes>
  </test>
</suite>

 

8) How to exclude a particular test method from a test case execution using TestNG?

<suite name="Sample Test Suite" verbose="1" >
  <test name="Sample Tests" >
    <classes>
       <class name="com.test.sample">
        <methods>
        <exclude name="TestA" />
      </methods>
       </class>
    </classes>
  </test>
</suite>

 

9) How to exclude a particular group from a test case execution using TestNG?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Test Suite">
  <groups>
    <run>
      <exclude name="integration"></exclude>
    </run>
  </groups>
  <test name="Test">
    <classes>
      <class name="TestNGGroupsExample" />
    </classes>
  </test>
</suite>

 

10) How to disable a test case in TestNG?

@Test(enabled = false)
  public void test() {
    System.out.println("This test is disabled");
  }

 

11) How to ignore a test case in TestNG?

@Test(enabled = false)
public void test() {
System.out.println("This test is ignored");
}

 

12) What are the different ways to produce reports for TestNG results?

There are two ways to generate a report with TestNG −

Listeners − For implementing a listener class, the class has to implement the org.testng.ITestListener interface. These classes are notified at runtime by TestNG when the test starts, finishes, fails, skips, or passes.

Reporters − For implementing a reporting class, the class has to implement an org.testng.IReporter interface. These classes are called when the whole suite run ends. The object containing the information of the whole test run is passed to this class when called.

13) How to write regular expressions in testng.xml file to search @Test methods containing “smoke” keyword?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="testSuite">
<test name="test"> <classes>
<class name="sample">
<methods>
<exclude name="smoke.*"/>
</methods>
</class>
</classes>
</test>
</suite>

 

14) What is the time unit we specify in test suites and test cases?

Time unit is Milliseconds

15) List out various ways in which TestNG can be invoked?

– Command Line
– Maven
– IDE

16) How to run TestNG using command prompt?

java -cp C:\Selenium\SampleTest\lib\*;C:\Selenium\SampleTest\bin org.testng.TestNG testng.xml

 

17) What is the use of @Test(invocationCount=x)?

The invocationcount attribute tells how many times TestNG should run a test method. In this example, the method testCase will be invoked ten times:

@Test(invocationCount = 10)
public void testCase(){

System.out.println("Invocation method");

}

 

18) What is the use of @Test(threadPoolSize=x)?

The threadPoolSize attribute tells TestNG to create a thread pool to run the test method via multiple threads. With thread pool, it will greatly decrease the running time of the test method.

Example: Start a thread pool, which contains 3 threads, and run the test method 3 times

@Test(invocationCount = 3, threadPoolSize = 3)
public void testThreadPools() {

System.out.printf("Thread Id : %s%n", Thread.currentThread().getId());
}

 

19) What does the test timeout mean in TestNG?

While running test methods there can be cases where certain test methods get struck or may take longer time than to complete the execution than the expected. We need to handle these type of cases by specifying Timeout and proceed to execute further test cases / methods

@Test(timeOut=5000)
public void executeTimeOut() throws InterruptedException{
Thread.sleep(3000);
}

 

20) What is JUnit?

JUnit is an open source Unit Testing Framework for JAVA. It is useful for Java Developers to write and run repeatable tests.

21) What are JUnit annotations?

– @Test
– @ParameterizedTest
– @RepeatedTest
– @TestFactory
– @TestTemplate
– @Disabled
– @Tag
– @AfterAll
– @BeforeAll
– @BeforeEach
– @AfterEach
– @TestInstance

22) What is TestNG and what is its use?

TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use. It is used for:
– Annotations
– Run your tests in arbitrarily big thread pools with various policies available
– Test that your code is multithread safe
– Flexible test configuration
– Support for data-driven testing
– Support for parameters
– Powerful execution model
– Supported by a variety of tools and plug-ins
– Embeds BeanShell for further flexibility
– Default JDK functions for runtime and logging
– Dependent methods for application server testing

23) How is TestNG better than JUnit?

– TestNG supports more annotations than Junit
– TestNG supports ordering of tests but Junit doesn’t
– TestNG supports various types of listeners using annotations but Junit doesn’t
– TestNG reports are better than Junit

24) How to set test case priority in TestNG?

@Test (priority=1)
public void openBrowser() {
driver = new FirefoxDriver();
}

@Test (priority=2)
public void launchGoogle() {
driver.get("http://www.google.co.in");
}

 

25) How to pass parameters through testng.xml to a test case?

public class ParameterizedTest {
@Test
@Parameters("name")
public void parameterTest(String name) {
System.out.println("Parameterized value is : " + name);
}
}

 

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "Suite1">
<test name = "test1">

<parameter name = "name" value="bijan"/>

<classes>
<class name = "ParameterizedTest" />
</classes>

</test>
</suite>

 

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 ?

About Me > Bijan Patel

Connect to me on Linked In (Click here)

On a mission to contribute to the Software Testing Community in all possible ways.

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *