Latest Selenium TestNG Interview Questions and Answers – 2017

Selenium Webdriver and TestNG together make a perfect automation framework. They compliment each other by filling the functionality gaps with their unique features. Like Selenium Webdriver doesn’t have a built-in reporting feature while TestNG does have it. Similarly, it lacks in browser testing which Selenium Webdriver has expertise. Hence, for this edition of Q&A series, we’ve prepared an entirely new set of Selenium TestNG interview questions and answers. Earlier, we’d published many posts covering the most important Selenium interview questions. Also, you might have seen one of our most read post on the TestNG interview questions and answers. If you’ve not already read then, then you would surely find them worth reading. Apart from the Q&A section, you can search for a no. of Selenium testing and Webdriver commands tutorials from this blog.

So, broadly speaking there are two main topics we covered in this blog post. One is Selenium Webdriver and the second is the TestNG. Also, we like to assure you that all the Selenium TestNG Interview Questions given here are practical and ready to use. However, we’ve further classified the questions into multiple sets. And each of them evaluates the candidates based on their experience level.

All of these Selenium TestNG interview questions would not only make you doing better in interviews but guide you in building efficient automation projects. We can say so because the answers of many of theses questions have quality tips hidden within. Also, we’ve tried to address some of the common problems that software testers face in their daily activities. So you can bookmark this post to refer it when you fall into similar problems we took up here.

Latest Selenium TestNG Interview Questions – 2017.

Here is the list of 20 latest Selenium TestNG interview questions for 2017. Please read them thoroughly and revise again if required.

Selenium TestNG Interview Questions and Answers.

Selenium TestNG Interview Questions for 2017

Q-1: Why should we prefer using WebDriver instead of Selenium IDE?

First of all, Selenium IDE is a known Record and Playback tool and also very easy to use. But at times, it could be quite deceptive as well. Also, we can’t scale up automation using the record/playback technique while testing a large enterprise application. Next, web applications go through frequent changes which make the IDE more difficult for the testers to adopt. So, it’s not an ideal solution to automate in a production testing environment.

Let’s consider the following example. Say, we recorded a test and found an element with a dynamic ID. Then, we imported it into Eclipse but think what would we do if the test starts failing after some time. It could be because the ID is no more on available on the web page. Might the developer would have changed it while adding a new feature. So it’s better being Agile and use something like Webdriver to create a test suite adaptive to such changes.

Also, to your note, the idea of IDE was to work as a quick automation tool and not an alternative to as the functional test suite.


Q-2: What is the easiest way to get the value from a text field in Selenium?

We can use the getText() method to fetch the value of a text field in Selenium. For illustration, let’s consider the below example.

<p id="title">Hello world!</p>

When we use the getText() method, it’ll return the text as “Hello world!”.

For retrieving the text from a <p> tag, we’ll call getText() in the following manner.


If we’ve to get the text from a Combobox, then something like the below code would work.

Select comboBox = new Select(findElement("title")));


Q-3: What is the best approach for reading a JavaScript variable from Selenium WebDriver?

We’ll create a JavaScript file and add a variable to read using the Selenium Webdriver code. Then, we’ll define a setter/getter function in the JavaScript. Probably you can give the following code example to make the interviewer understand your answer.

First code is the JavaScript file which contains a global variable and the get/set functions.

var selVar;

$(document).ready(function() {

function setVar(new) {
    selVar = new;

function getVar() {
    return selVar;

Next, we can use the below Selenium Webdriver code in Java to read the JavaScript variable.

// Do all the necessary imports.

public class JSReaderTest {
    WebDriver driver = new FirefoxDriver();
    JavascriptExecutor js = (JavascriptExecutor) driver;

    public void testSelVar() {
        String selVar = (String) js.executeScript("alert(getVar());");
        Assert.assertEquals("new value for selVar is ", selVar);

So we can call the getVar() method from the Selenium test to read the Javascript variable and set asserts to verify it.


Q-4: How will you get the HTML source of a <WebElement> in Selenium WebDriver using Python/Java/C#?

We can call the API to extract the <innerHTML> attribute of a web element. Similarly, we can use the <outerHTML> attribute to get the source of the selected element.

Example-1: Get HTML source in Java.


Example-2: Get HTML source in Python.


Example-3: Get HTML source in C#.



Q-5: How to initialize a list of web elements in Selenium Webdriver?

Since it’s a common use case, so we had a chance to handle it in our project. We used the following approach in our test suite.

private List<WebElement> items;

public List<WebElement> getSelects() {
      items = getDriver().findElements(By.xpath("..."));
      return items;


Q-6: What is the best way to click screenshots when a test fails in Selenium 2.0?

Probably, for taking screenshots, the best we can do in Selenium 2.0 is as follows.

@AfterMethod public void takeScreenShotOnFailure(ITestResult testResult) throws IOException {
 if (testResult.getStatus() == ITestResult.FAILURE) {
  File errorImage = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
  FileUtils.copyFile(errorImage, new File("C:\\errorImage.jpg"));


Q-7: How to fix the below chrome driver error in Selenium?

The path to the driver executable must be set by the system property.

We can use the <selenium-server-standalone-*.jar> and pass the <> property while launching it.

java -jar selenium-server-standalone-2.53.1.jar"C:\test\chromedriver.exe"

So, the above command would fix the error. Java command-line option <-Dproperty=value> sets a system property value as expected.


Q-8: What is the right way to run Selenium WebDriver test cases in Chrome?

First of all, we’ve to download the chrome driver from the link: <>.

Finally, we can use the Java code given below to run Selenium webdriver tests in Chrome.

System.setProperty("", "C:\\path\\to\\chromedriver.exe");
WebDriver driver = new ChromeDriver();


Q-9: What to do for setting up the <InternetExplorerDriver> for Selenium 2.0?

First of all, we’ll download the IE driver module on our system. We can download it from the link: <>. Then, unpack it and place on the local drive.

Finally, copy it to some <C:\\Selenium\\iexploredriver.exe> directory. Next, we need to set it up in the system. Therefore, we’ll use the following Java code to run Selenium tests in IE.

File file = new File("C:\\Selenium\\iexploredriver.exe");
System.setProperty("", file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver();


Q-10: What would you do to make the <WebDriver> wait for a page to refresh before executing the test?

It seems like the following Java code will make the <WebDriver> wait for a page to refresh. It’ll also ensure the tests should run later.

public void waitForPageToRefresh(WebDriver driver) {
 ExpectedCondition < Boolean > expectation = new
 ExpectedCondition < Boolean > () {
  public Boolean apply(WebDriver driver) {
   return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
 Wait < WebDriver > wait = new WebDriverWait(driver, 30);
 try {
 } catch (Throwable error) {
  assertFalse("Timeout occurs.", true);


Q-11: How to get the Selenium wait until the document is ready?

In Webdriver, we can write something like the code given below.

driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);

You can check that the code has set a 10-second timeout for page loading. If the page takes more time to load, then the Webdriver will throw a <TimeoutException>. We can catch the exception and take action accordingly.

Also, just for clarity, it’s an implicit wait that we set up above. So if you define this once, it’ll remain active till the lifetime of the Web Driver instance.


Q-12: How to get Selenium wait until the element is present?

Since the <WebDriver> provides Fluent Wait concept, so we’ll use it in this case. We can apply Fluent Wait to ignore the <NoSuchElementException> exception. It’ll also make the <WebDriver> wait for the element.

Probably we can use the below Java code.

FluentWait fluentWait = new FluentWait<>(WebDriver) {
  .withTimeout(30, TimeUnit.SECONDS)
  .pollingEvery(200, TimeUnit.MILLISECONDS);

Next, we’ll check whether the element is present on the page or not. It seems like the below code would do the job for us.

WebElement item = (new WebDriverWait(driver, 10))


Q-13: What is the fundamental difference between a wait() and the sleep()?

The primary difference between a wait() and the sleep() is as follows.

1- First of all, we can end a wait from another thread by using the notify method. But it’s not at all possible to interrupt a sleep call.

2- Wait always occurs in a synchronized block whereas the sleep isn’t.


Q-14: How can we run a particular TestNG test group using a maven command?

If we’ve several TestNG test groups like <group1>, <group2>, <group3> etc. Then, first of all, we’ll need to get them added to our project’s <testng.xml>. Also, if want all of them to run at once, then we need to execute the below <mvn> command.

mvn test

Consequently, we can run an another command to run a particular group.

mvn test -Dgroups=group3,group2


Q-15: Which command will you use to run a single test method in maven?

To run a single test method in Maven, we can use the same approach as we did in the previous question. See the below command to run a single test method suing maven.

mvn -Dtest=TestClass#testMethod test

Just for a note, we can use wildcard characters with both method name and class name.


Q-16: How can we run all the tests of a TestNG class?

It is easy to achieve using maven. We can issue the below command to run tests of a class.

mvn test -Dtest=classname


Q-17: What will you do to run the Selenium tests without using the <testng.xml>?

It’s one of the Selenium TestNG interview questions that interviewers love to ask. Please check out the answer below.

Yes, we can do it by using listeners in TestNG. Please refer the below Java code.

TestListenerAdapter listener = new TestListenerAdapter();
TestNG ng = new TestNG();
ng.setTestClasses(new Class[] { MyTestClass.class });


Q-18: How to disable an entire Selenium test in TestNG?

TestNG provides a granular level control to manage test cases. So, it’s relatively easy to turn of a test or a full test suite in TestNG. Hence, please check out the below code.

public class MyFirstTest {


Q-19: How can we attach a failure screenshot to the testNG report?

Though, we can’t attach an error snapshot. But we can add a link to the screenshot in the test report.

There is a TestNG class as <org.testng.Reporter.log> which gives a method to add the link to the TestNG report.

Here are a few steps to do it.

1- First of all, create a Listener class and add it to the TestNG project.
2- Write a method to override the <ontestfailure()> method. It’s the default error handler for failures in TestNG.
3- Take a screenshot inside the above method, and save it to a file.
4- Use the Reporter.log() method to put the hyperlink of the screenshot file in the report.

Finally, you’ll see the failure screenshot linked to the TestNG report.


Q-20: What would you do to configure the parallel execution of tests and classes in <testng.xml>?

It’s simple to understand the solution by looking at the below <testng.xml> file. It’ll run both the tests and classes in parallel.

<suite name="My Test Suite" allow-return-values="true" verbose="1" parallel="tests" thread-count="2">
    <test name="Login Test case 01" parallel="classes" thread-count="2">
        <parameter name="Operating_System" value="Windows 7"/>
        <parameter name="Browser_Name" value="Internet Explorer"/>
        <parameter name="Browser_Version" value="11"/>
        <parameter name="Base_URL" value=""/>
            <class name="com.mytest.tool.testing_01"/>
            <class name="com.mytest.tool.testing_02"/>
    <test name="Login Test case 02" parallel="classes" thread-count="2">
        <parameter name="Operating_System" value="Windows 7"/>
        <parameter name="Browser_Name" value="Mozilla Firefox"/>
        <parameter name="Browser_Version" value="47.0.1"/>
        <parameter name="Base_URL" value=""/>
            <class name="com.mytest.tool.testing_01"/>
            <class name="com.mytest.tool.testing_02"/>


Summary – Selenium TestNG Interview Questions for 2017.

We intended to help you in interview planning by publishing the post on some of the essential Selenium TestNG interview questions and answers. We wish that this post could serve our mission and will get you the due benefit.

There has been quite an effort in researching, discussing and finally coming out with the above post. So we request you to share it with your friends and on social media.

Please do so only if you think it’s worthy of being shared.


All the Best,



  1. jameel Reply
    • Meenakshi Agarwal Reply
  2. Rajeshwari Reply
    • Meenakshi Agarwal Reply
  3. Sebastian Reply
    • Meenakshi Agarwal Reply
      • Sebastian Reply
  4. Harshal Reply
    • Meenakshi Agarwal Reply