Selenium TestNG Interview Questions and Answers Part2

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.

Selenium TestNG Interview Questions and Answers Part2.

First of all, we’ll see some of the basic interview questions that are easy to learn. But these are also on the priority list of interviewers.

1- Basic Level: Selenium TestNG Interview Questions and Answers.

First of all, let’s find out what’s in the first set of Selenium TestNG interview questions and answers?

1.1- Basic set#1.

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

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.

 

1.2- Basic set#2.

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.

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

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#.

 

1.3- Basic set#3.

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.

 

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.

 

2- Intermediate Level: Selenium TestNG Interview Questions and Answers.

In this section of the post, you’ll find the intermediary level of Selenium TestNG interview questions and answers.

2.1- Intermediary set#1.

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.

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.

 

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

 

2.2- Intermediary set#2.

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.

 

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

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

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.

 

2.3- Intermediary set#3.

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.

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.

 

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.

 

3- Advanced Level: Selenium TestNG Interview Questions and Answers.

In this part of the post, we’ll mostly cover the Selenium TestNG interview questions and answers related to TestNG concept.

3.1- Advanced set#1.

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.

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

 

Q-15: How to run a single test method with 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.

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

 

3.2- Advanced set#2.

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.

 

Q-17: How is it possible to eliminate the need for the <testng.xml> for running Selenium tests in TestNG?

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.

 

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.

 

3.3- Advanced set#3.

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 for configuring 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.

 

Final Thought.

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.

2 Comments

  1. Harshal Reply

Leave a Reply


*