Selenium and Web Driver Interview Questions and Answers

Q1. What is Selenium?
A1. Selenium is a suite of tools for browser automation (i.e. automated Web testing). It is composed of

  • Selenium IDE: A tool for recording and playing back. This is a Fire-fox plugin.
  • WebDriver and RC provide the APIs for a variety of languages like Java, .NET, PHP, etc. A Java example is shown below. The WebDriver and RC work with most browsers.
  • Grid transparently distribute your tests on multiple machines so that you can run your tests in parallel, cutting down the time required for running in-browser test suites. 

Q2. How would you go about using selenium for your web testing?
A2. The example below shows the steps involved in testing a simple login page with selenium + Web Driver. The pages have HTML snippets as shown below

Login page:

Login response page

STEP 1: Configure your Maven to include the required jar file selenium-java-x.x.x.jar.

The selenium-java-x.x.x.jar will transitively bring in the other relevant driver jars for different browsers.

STEP 2: It is a best practice to have separate page objects as these page objects can be shared by multiple JUnit test cases. If a particular page element changes, you will have to change it only in one place, which is page object, and not in all JUnit test cases where a paricular element is used. The JUnit test cases will depend on the page objects and should not refer to the page elements directly.

You can have your page objects extend the above generic SystemTestPage class.

STEP 3: Write the JUnit class using the page objects defined above and assert the results. These JUnit test cases can be run to test your web pages.

Q3. What are selenium locators? What tools do you use to locate them?
A3. Selenium Locators are the way of finding the HTML element on the page to perform a Selenium action on. The example above has a line asshown below to extract the username element from the Login response page. This uses an XPath expression to locate the element.

The XPath expression will be something like //td[[text()=John] which looks for a td element with text value “John”.

The annotation in the above example is also a locator by name as shown below

This will match the HTML snippet

You could also find by tagName, id, css, etc.

There are handy tools to identify the HTML elements or locators.

  • Selenium IDE, which is a Firefox plugin useful in identifying the locators, debugging, etc.

  • The Fire-bug plugin, which allows you to inspect the elements by right clicking on the page and then selecting “Inspect Element”. Google chrome provides a similar functionality to inspect element out of the box. 

Q4. In your experience, what are some of the challenges with Selenium?
A4. In general, badly written test cases whether junit tests or web tests, the biggest complaint is about writing test cases that are not maintainable. Unmaintainable automated test cases can take more time than manual tests. So, it is important to write quality test cases by clearly separting the page objects from the test cases as demonstrated  in the Q&A above. The use of the locators need to be carefully thought through. For example, some frameworks like JSF dynamically generate HTML element IDs. So, if IDs are used in your tests, then the test cases may fail if the IDs have changed. The solution to this problem is to use XPath to find the relevant HTML elements. The ClickAndWait action will not work for AJAX calls, and use “waitForElement” instead.

300+ Java & Big Data Interview FAQs

16+ Java Key Areas Interview Q&As

800+ Java Interview Q&As

300+ Java & Big Data Tutorials