Selenium Interview Questions

1. You are hired by a company as test automation engineer.

Your first task is to automate testing for a website using Selenium WebDriver and Java.

Please describe your approach for the project.

 

ANSWER

 

First, look into what types of testing are being executed manually.

Functional testing is the most popular.

Cross-browser testing is another, localization testing, reliability testing, etc.

The first one to be automated is functional testing. All other testing types have to be deferred until you have a good suite of automated tests for functional testing.

 

Have a look at the suite of regression functional test cases.

Select from them the test cases with high priority since they are the most important for the business.

Then, filter the list of high priority test cases and only keep the ones that

  • are executed frequently
  • do not have complex pre-requisites
  • do not have dependencies

For each remaining test case, since it may be testing more than one thing, break it down in smaller test cases that test each one thing only.

The smaller test cases will be the candidates for automation.

Each small test case will be implemented with an automation test.

 

 

The project should be designed with the following layers:

  1. test scripts (built with a unit testing library such as Junit or TestNG)
  2. page objects
  3. automation framework
  4. Selenium WebDriver

 

Each layer should use as much as possible only the layer under it.

The project may have more dependencies than Selenium WebDriver and the unit testing framework. All dependencies can be managed by Maven.
Maven also allows executing the tests in command prompt which is essential for running the automation tests in CI/CD (Jenkins).

 

 

For each test, create a page class that implements the interaction with each web page.

The class should have methods that either interact with the elements of the page or get information from the elements.

The tests will rely on the objects of the page classes.

The page classes may grown in time so when this happens it is a good idea to create also page element classes.

If a page class corresponds to a web page, a page element class corresponds to a component of a web page (example: search component,  sorting component, paging component).

Both page classes and page element classes should synchronize with the website so they wait until an element is available before using it.

 


The automation framework should provide common functionality such as

  1. reporting
  2. logging
  3. taking screenshot on error
  4. getting exceptions on error
  5. generating random data
  6. using proxies
  7. classes for all common html elements
  8. custom drivers
  9. custom locators
  10. custom expected conditions
  11. reading xml and json files

The page objects should use as much as possible the automation framework.

 


Tests should be organized in test classes.

This is done usually by the page that the tests work on or by the feature.

The tests should be independent of each other, of environment, browser and test data.

Try keeping the number of tests in a test class low (not more than 10).

 


Keep everything small in the project.

Small classes, small methods, small locators, small variable names.
It is much better to have many small classes than a few big ones.

 

 

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

 

 

 

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

 

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

 

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

 

 

 

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

 

ANSWER

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

  • using driver.findElement()

WebElement element = driver.findElement(locator);

 

  • using driver.findElements()

List<WebElement> list = driver.findElements(locator);

WebElement element = list.get(0);

 

  • using explicit wait and expected conditions

WebDriverWait wait = new WebDriverWait(driver, 30);

WebElement list = wait.until(

ExpectedConditions.visibilityOfElementLocatedBy(locator));

 

  • using Javascript

String getElementJS = "function getElement(){ ” +

"var t = document.getElementById('title'); return t; }; " + 

"return getElement()";

WebElement title = (WebElement)jsExec.executeScript(getElementJS);

 

 

 

5. 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)));

 

 

 

Do you like these questions?

Do you want to continue?

Read 10 more questions for USD 5 paid by Paypal transfer to alex@alexsiminiuc.com.

 

Advertisements