TechBeamersTechBeamers
  • Viral Tips 🔥
  • Free CoursesTOP
  • TutorialsNEW
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing Tutorial
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
  • Top Interviews & Quizzes
    • SQL Interview Questions
    • Testing Interview Questions
    • Python Interview Questions
    • Selenium Interview Questions
    • C Sharp Interview Questions
    • Java Interview Questions
    • Web Development Questions
    • PHP Interview Questions
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
  • ToolsHOT
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
TechBeamersTechBeamers
Search
  • Viral Tips 🔥
  • Free CoursesTOP
  • TutorialsNEW
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing Tutorial
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
  • Top Interviews & Quizzes
    • SQL Interview Questions
    • Testing Interview Questions
    • Python Interview Questions
    • Selenium Interview Questions
    • C Sharp Interview Questions
    • Java Interview Questions
    • Web Development Questions
    • PHP Interview Questions
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
  • ToolsHOT
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
Follow US
© TechBeamers. All Rights Reserved.
Selenium Interview

Top 20 Selenium TestNG Interview Questions (2025)

Last updated: Feb 23, 2025 11:58 am
Meenakshi Agarwal
By
Meenakshi Agarwal
Meenakshi Agarwal Avatar
ByMeenakshi Agarwal
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large...
Follow:
No Comments
3 months ago
Share
14 Min Read
SHARE

In this post, we’ve curated 20 essential Selenium TestNG interview questions and answers to help you prepare. Whether you’re brushing up on your skills or diving into automation for the first time, these questions will give you the confidence to tackle any interview.

Selenium TestNG Interview Questions for 2017

By the way, don’t forget to check out our other popular posts on Selenium interview questions and TestNG interview questions for even more practice. Let’s get started!”

Top 20 Must-Know Selenium TestNG Interview Questions

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

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 is 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. The developer might have changed it while adding a new feature. So it’s better to be Agile and use something like Webdriver to create a test suite adaptive to such changes.

Also, for your note, the idea of IDE was to work as a quick automation tool and not an alternative to 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.

findElement(By.id("title")).getAttribute("value");

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

Select comboBox = new Select(findElement(By.id("title")));
comboBox.getFirstSelectedOption().getText();

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.

The 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;

@Test
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 <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.

webElement.getAttribute("innerHTML");

Example-2: Get HTML source in Python.

webElement.get_attribute("innerHTML")

Example-3: Get HTML source in C#.

webElement.GetAttribute("innerHTML");

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

Since it’s a frequent 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) {
  System.out.println(testResult.getStatus());
  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 webdriver.chrome.driver system property.

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

java -jar selenium-server-standalone-2.53.1.jar -Dwebdriver.chrome.driver="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. After that, we can use the Java code given below to run Selenium webdriver tests in Chrome.

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

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

While Internet Explorer (IE) is no longer actively supported by Microsoft (as of June 15, 2022), some legacy systems may still require testing on IE. Here’s how you can set up the InternetExplorerDriver for Selenium:

First of all, download the IE driver compatible with your system. We can download it from the link: https://www.selenium.dev/downloads/. Then, unpack it and place it on the local drive.

Finally, copy it to some location on your system. And, use the following Java code to set up and initialize the InternetExplorerDriver:

File file = new File("C:\\Selenium\\iexploredriver.exe");
System.setProperty("webdriver.ie.driver", 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 {
  wait.until(expectation);
 } catch (Throwable error) {
  assertFalse("Timeout occurs.", true);
 }
}

Q-11: How to get Selenium to 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 until the lifetime of the Web Driver instance.

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

Since the <WebDriver> provides the 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);
.ignoring(NoSuchElementException.class);
}

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))
.until(ExpectedConditions.elementToBeClickable(By.id("button")));

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

The primary difference between a wait() and 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 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 issue 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 using Maven.

mvn -Dtest=TestClass#testMethod test

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

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 to the below Java code.

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

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

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

@Test(enabled=false)
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 of 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="https://www.google.com"/>
<classes>
<class name="com.mytest.tool.testing_01"/>
<class name="com.mytest.tool.testing_02"/>
</classes>
</test>
<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="https://www.google.com"/>
<classes>
<class name="com.mytest.tool.testing_01"/>
<class name="com.mytest.tool.testing_02"/>
</classes>
</test>
</suite>

Next Steps: Ace Your Selenium TestNG Interview

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,
TechBeamers

Related

TAGGED:Hire Selenium Candidates
Share This Article
Flipboard Copy Link
Subscribe
Notify of
guest

guest

0 Comments
Newest
Oldest
Inline Feedbacks
View all comments

List of Topics

Stay Connected

FacebookLike
XFollow
YoutubeSubscribe
LinkedInFollow

Subscribe to Blog via Email

Enter your email address to subscribe to latest knowledge sharing updates.

Join 1,011 other subscribers

Continue Reading

  • 10 Selenium Technical Interview Questions and Answers.Nov 26
  • 5 Most Asked Selenium Questions You Must KnowMar 17
  • Top 35 TestNG Interview Questions with AnswersApr 1
  • When Selenium Click() not Working – What to do?Dec 25
  • Top 100+ Selenium Interview Questions and Answers (2025) – PDF IncludedAug 27
  • Selenium Webdriver Interview QuestionsMar 3
  • Differences in Selenium IDE, RC, WebdriverApr 24
  • 35 Selenium Webdriver Questions for InterviewApr 30
  • The Best Sample Resume for Selenium Webdriver Interview (2025)May 20
  • How to Get Ready for Selenium Webdriver Cucumber InterviewJul 9
View all →

RELATED TUTORIALS

Top 20 Interview Questions for Automation Testing in Selenium

Selenium Webdriver Interview Questions

By Meenakshi Agarwal
3 months ago
When Selenium Click() Not Working

When Selenium Click() not Working – What to do?

By Meenakshi Agarwal
1 year ago
top five selenium questions for interview

5 Most Asked Selenium Questions You Must Know

By Meenakshi Agarwal
11 months ago
35 TestNG Interview Questions and Answers for Sure Success

Top 35 TestNG Interview Questions with Answers

By Meenakshi Agarwal
2 months ago
© TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
wpDiscuz