Selenium WebDriver Interview Questions — part 1

Alex Siminiuc
Oct 14 · 6 min read
Photo by Nik MacMillan on Unsplash

Question

Why should a company implement test automation at all?

Answer

Most companies are interested these days in fast and better development, fast and better deployment and fast and better testing.
They would like to have not one application build per week but 10 per day.

It is obviously very hard to deploy and test 10 builds per day.

Unless the deployment and testing are done automatically in a continuous integration and deployment environment with a system like Jenkins and Teamcity.

The main benefit of test automation is that it allows implementing CI/CD processes in the project.

The second benefit is to automate the test cases that are executed over and over again. I am referring to the regression testing ones.

By automating these test cases and running them unattended, the testers will have more time for more creative, exploratory, edge-case testing.

They will also have more time for other types of testing such as whitebox testing, api testing. And they will have more time for improving their skills.

The third benefit is that, by adding test automation to CI/CD processes, the feedback for developers regarding regression issues is very fast.

After a new build is deployed, automated tests run and if there are any issues, the development team is notified right away.


Question

Describe page object model.

Answer

When writing automation code, it is incorrect to have all code in the test script. This leads to very long test scripts which are difficult to read and maintain.

If you have WebDriver APIs in your test methods, You’re Doing It Wrong.
Simon Stewart

Page Object Model explains the rules for making the automation code easier to maintain:

1. Create a page class for each page of the site

The purpose of the page class is to wrap an HTML page, or fragment, with an application-specific API, allowing you to manipulate page elements without digging around in the HTML (Martin Fowler).

Another way of looking at it is that the page class implements the user interaction with the page.

Each user action that is possible on the page should have a corresponding method in the class.

The class methods can either interact with the page or return information from the page.

The methods should not just interact with specific elements of the page (click a link, type in a textbox, get the text of a label). Instead, they should implement user actions (search for keyword, change sort order, go to next page).

2. If a page class gets too big, it should be broken down in page element classes

If a page class corresponds to a page of the site, a page element class corresponds to a component of a page. Objects of the page element classes should be used in the page class.

Let’s say that you have a ResultsPage class for the results page of the site and SortComponent, FilterComponent, PagingComponent classes for various components of the results page. The objects of the component classes should be used in the methods of the ResultsPage class.

3. Page methods should return new page objects if they lead to a new page being displayed in the browser

If the search() method of the HomePage class displays the results page, then it should return an object of the ResultsPage class:

ResultsPage resultsPage = homePage.search(“java”);

4. Page classes should not include assertions

It is the responsibility of the test scripts to make assertions on the methods of the page objects.


Question

What are all different ways of finding an element using Selenium WebDriver?

Answer

There are multiple ways of finding an element with Selenium WebDriver:

  1. using driver.findElement()
WebElement element = driver.findElement(locator);

2. using driver.findElements()

List list = driver.findElements(locator);
WebElement element = list.get(0);

3. using explicit wait and expected conditions

WebDriverWait wait = new WebDriverWait(driver, 30);WebElement list = wait.until(
ExpectedConditions.
visibilityOfElementLocatedBy(loc));

4. using Javascript

String getElementJS = “function getElement(){ “ +
“var t = document.getElementById(‘title’); return t; }; “ +
“return getElement()”;
WebElement title = (WebElement)jsExec.executeScript(getElementJS);

Question

How do you wait until the title and url of a page are both correct?

Answer

There are 2 ways of doing this:

  1. with no synchronization
String actualUrl = driver.getCurrentUrl();
String actualTitle = driver.getTitle();
assertEquals(actualUrl, expectedUrl, “url is incorrect!”);
assertEquals(actualTitle, expectedTitle, “title is incorrect!”);

This way if checking the page title and url will not work for slow pages.

2. with synchronization

WebDriverWait wait = new WebDriverWait(driver, 30);wait.until(ExpectedConditions.urlToBe(expectedUrl));
wait.until(ExpectedConditions.titleIs(expectedTitle));

This way works for slow pages as well.

The 2 waits can be also combined in 1:

wait.until(ExpectedConditions.and(
ExpectedConditions.urlToBe(expectedUrl), ExpectedConditions.titleIs(expectedTitle)));

Question

How would you improve the following method?

public void selectPage(int i) {
By l = getLc(i);
WebElement p = driver.findElement(l);
p.click();
}

Answer

There are multiple things to improve.

The parameter represents a page number.
Since page numbers can only be positive, the method should start with a check for invalid values for the parameter.

Some of the names are not good enough. l should be replaced by locator, p by page and getLc by getLocator.

Finding an element with driver.findElement() only works if the page is displayed fast and the element is on it. It is better to wait until the element is available before using it

The better code is below:

public void selectPage(int i) {
if (i <= 0)
throw new IllegalArgumentException(“invalid index — “ + i);
By locator = getLocator(i); WebDriverWait wait = new WebDriverWait(driver, 30);

WebElement page = wait.until(
ExpectedConditions
.elementToBeClickable(locator));
page.click();
}

Question

How can the catch clause be improved?

try {
int i = n / 0;
int[] numbers = new int[] {1, 2, 3, 4, 5};
numbers[10] = 9;
}
catch (Exception e) {
console.out(e.getMessage());
}

Answer

There are 2 exceptions generated in the try block:

  • dividing n to 0 throws an ArithmeticException
  • accessing the 11 array element throws an IndexOutOfBounds exception

Each exception should be handled separately in its own catch clause.

Also, since both exceptions are severe, instead of logging a message in the console, a new exception should be thrown with a user friendly error message and the initial exception:

try {
int i = n / 0;
int[] numbers = new int[] {1, 2, 3, 4, 5};
numbers[10] = 9;
}
catch (ArithmeticException e) {
throw new RuntimeException(“division to 0!”, e);
catch (IndexOutOfBoundsException e) {
throw new RuntimeException(“elements out of the array”, e);
}

Question

After opening the home page of a site, a popup is displayed randomly. How do you handle this popup?

Answer

The random popup is displayed over the home page.

The popup has a title, a cancel button, some text and a submit button.

Sometimes, the popup is displayed, sometimes it is not.

When it is displayed, the automation code should close it.
When it is not displayed, the automation code should not do anything:

public class Popup {
private final static By TITLE_ID = By.id(“title”);
private final static By CANCEL_ID = By.id(“cancel”);

private WebDriver driver;
private WebDriverWait wait;

public Popup() {
this.driver = driver;
this.wait = new WebDriverWait(this.driver, 5);
}

public void cancel() {
if (isDisplayed()) {
WebElement cancelButton = this.wait.until(
ExpectedConditions.elementToBeClickable(CANCEL_ID));

cancelButton.click();
this.wait.until(ExpectedConditions.
invisibilityOfElementLocated(TITLE_ID));
}
}

public boolean isDisplayed() {
try {
WebElement title = this.wait.until(
ExpectedConditions.
visibilityOfElementLocatedBy(TITLE_ID));
return title.isDisplayed();
} catch (TimeoutException e) {
return false;
}
}
}

isDisplayed() returns true if the title of the popup is visible in 3 seconds. Otherwise, it returns false.

cancel() will do something only if the popup is displayed (isDisplayed() returns true):

  • It finds the cancel button
  • It clicks the cancel button
  • It waits until the title of the popup is invisible

In the test, you need to call the cancel() method of the popup after HomePage is displayed:

HomePage homePage = new HomePage(driver);
homePage.open();
homePage.popup().cancel();

Do you like these questions?
Clap if you do :)

More questions coming up soon.

Alex Siminiuc

Written by

Blogs about Selenium and Java at https://seleniumjava.com.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade