TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Latest Selenium TestNG Interview Questions and Answers – 2024
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Selenium Interview

Latest Selenium TestNG Interview Questions and Answers – 2024

Last updated: Feb 25, 2024 2:06 am
By Meenakshi Agarwal
Share
15 Min Read
Selenium TestNG Interview Questions for 2017
SHARE

Selenium Webdriver and TestNG together make a perfect automation framework. In this edition of the 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 essential Selenium interview questions.

Contents
Q-1: Why should we prefer using WebDriver instead of Selenium IDE?Q-2: What is the easiest way to get the value from a text field in Selenium?Q-3: What is the best approach for reading a JavaScript variable from Selenium WebDriver?Q-4: How will you get the HTML source of <WebElement> in Selenium WebDriver using Python/Java/C#?Q-5: How to initialize a list of web elements in Selenium Webdriver?Q-6: What is the best way to click screenshots when a test fails in Selenium 2.0?Q-7: How to fix the below chrome driver error in Selenium?Q-8: What is the right way to run Selenium WebDriver test cases in Chrome?Q-9: What to do for setting up the <InternetExplorerDriver> for Selenium 2.0?Q-10: What would you do to make the <WebDriver> wait for a page to refresh before executing the test?Q-11: How to get Selenium to wait until the document is ready?Q-12: How to get Selenium to wait until the element is present?Q-13: What is the fundamental difference between a wait() and sleep()?Q-14: How can we run a particular TestNG test group using a maven command?Q-15: Which command will you use to run a single test method in Maven?Q-16: How can we run all the tests of a TestNG class?Q-17: What will you do to run the Selenium tests without using the <testng.xml>?Q-18: How to disable an entire Selenium test in TestNG?Q-19: How can we attach a failure screenshot to the testNG report?Q-20: What would you do to configure the parallel execution of tests and classes in <testng.xml>?

Also, you might have seen one of our most-read posts on the TestNG interview questions and answers. If you’ve not already read it, then you would surely find it worth reading. Apart from the Q&A section, you can search for a no. of Selenium testing and Webdriver commands tutorials from this blog.

By combining functionalities, both these frameworks make an excellent Web automation testing tool. Software testers use It to speed up automation whereas developers for their unit testing tasks. It has become a key skill that hiring managers expect from candidates. Hence, you must read this tutorial as it opens up a path for you to excel in this automation skill.

Latest Selenium TestNG Interview Q&A- 2023.

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

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 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 from the link: <https://sites.google.com/a/chromium.org/chromedriver/downloads>.

Finally, 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 2.0?

First of all, we’ll download the IE driver module on our system. We can download it from the link: <http://www.seleniumhq.org/download/>. Then, unpack it and place it 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("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="http://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="http://www.google.com"/>
        <classes>
            <class name="com.mytest.tool.testing_01"/>
            <class name="com.mytest.tool.testing_02"/>
        </classes>
    </test>
</suite>

Summary – Selenium TestNG Interview Questions for 2023.

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.

You Might Also Like

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

TestNG Interview Questions for Sure Success in 2024

5 Most Asked Selenium Questions You Must Know

10 Selenium Technical Interview Questions and Answers.

How to: Selenium Webdriver Cucumber Interview

TAGGED:Hire Selenium Candidates
Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
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 MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Selenium Webdriver Fluent Wait Command with Examples Understand Webdriver Fluent Wait with Examples
Next Article How to Use TestNG Assertions in Selenium How to Use TestNG Assertions to Verify Tests in Selenium

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x