Top 50 Selenium Interview Questions and Answers
Top 50 Selenium Interview Questions and Answers
com/selenium-interview-questions-answers/#
Automation testing or Test Automation is a process of automating the manual process to test the
application/system under test. Automation testing involves use to a separate testing tool which
lets you create test scripts which can be executed repeatedly and doesn’t require any manual
intervention.
Selenium
Selenium is one of the most popular automated testing suites. Selenium is designed in a way to
support and encourage automation testing of functional aspects of web based applications and a
wide range of browsers and platforms. Due to its existence in the open source community, it has
become one of the most accepted tools amongst the testing professionals.
Selenium is not just a single tool or a utility, rather a package of several testing tools and for the
same reason it is referred to as a Suite. Each of these tools is designed to cater different testing
and test environment requirements.
Q #5) What are the testing types that can be supported by Selenium?
1. Functional Testing
2. Regression Testing
Q #7) What is the difference between Selenium IDE, Selenium RC and WebDriver?
Feature Selenium IDE Selenium RC WebDriver
Feature Selenium IDE Selenium RC WebDriver
WebDriver
supports a varied
range of versions
Selenium RC
of Mozilla
supports a varied
Selenium IDE Firefox, Google
range of versions
comes as a Chrome, Internet
Browser of Mozilla
Firefox plugin, Explorer and
Compatibility Firefox, Google
thus it supports Opera.
Chrome, Internet
only Firefox Also supports
Explorer and
HtmlUnitDriver
Opera
which is a GUI
less or headless
browser.
Selenium IDE Selenium RC WebDriver
Record and supports record doesn't supports doesn't support
Playback and playback record and record and
feature playback feature playback feature
Selenium IDE WebDriver
Selenium RC
doesn't require doesn't require
requires server to
Server any server to be any server to be
be started before
Requirement started before started before
executing the test
executing the executing the
scripts
test scripts test scripts
WebDriver uses
Selenium IDE is
Selenium RC is a the browser's
a Javascript
Architecture JavaScript based native
based
Framework compatibility to
framework
automation
Selenium IDE is Selenium RC is WebDriver is a
Object Oriented not an object semi object purely object
oriented tool oriented tool oriented tool
Dynamic Finders
Selenium IDE Selenium RC WebDriver
(for locating web
doesn't support doesn't support supports
elements on a
dynamic finders dynamic finders dynamic finders
webpage)
WebDriver
Selenium IDE Selenium RC offers a wide
doesn't explicitly doesn't explicitly range of utilities
Handling Alerts,
provides aids to provides aids to and classes that
Navigations,
handle alerts, handle alerts, helps in handling
Dropdowns
navigations, navigations, alerts,
dropdowns dropdowns navigations, and
dropdowns
Feature Selenium IDE Selenium RC WebDriver
Feature Selenium IDE Selenium RC WebDriver
efficiently and
effectively.
WebDriver is
designed in a
way to
efficiently
support testing
Selenium IDE Selenium RC of
WAP doesn't support doesn't support iPhone/Android
(iPhone/Android) testing of testing of applications. The
Testing iPhone/Andriod iPhone/Andriod tool comes with
applications applications a large range of
drivers for WAP
based testing.
For example,
AndroidDriver,
iPhoneDriver
WebDriver
Selenium IDE Selenium RC
supports the
Listener Support doesn't support doesn't support
implementation
listeners listeners
of Listeners
Selenium RC is
slower than
Selenium IDE is WebDriver as it
fast as it is doesn't
WebDriver
plugged in with communicates
communicates
the web-browser directly with the
directly with the
Speed that launches the browser; rather it
web browsers.
test. Thus, the sends selenese
Thus making it
IDE and browser commands over
much faster.
communicates to Selenium Core
directly which in turn
communicates
with the browser.
Selenium IDE is the simplest and easiest of all the tools within the Selenium Package. Its record
and playback feature makes it exceptionally easy to learn with minimal acquaintances to any
programming language. Selenium IDE is an ideal tool for a naïve user.
Locator can be termed as an address that identifies a web element uniquely within the webpage.
Thus, to identify web elements accurately and precisely we have different types of locators in
Selenium:
ID
ClassName
Name
TagName
LinkText
PartialLinkText
Xpath
CSS Selector
DOM
Assert: Assert command checks whether the given condition is true or false. Let’s say we assert
whether the given element is present on the web page or not. If the condition is true then the
program control will execute the next test step but if the condition is false, the execution would
stop and no further test would be executed.
Verify: Verify command also checks whether the given condition is true or false. Irrespective of
the condition being true or false, the program execution doesn’t halts i.e. any failure during
verification would not stop the execution and all the test steps would be executed.
Xpath is used to locate a web element based on its XML path. XML stands for Extensible
Markup Language and is used to store, organize and transport arbitrary data. It stores data in a
key-value pair which is very much similar to HTML tags. Both being markup languages and
since they fall under the same umbrella, Xpath can be used to locate HTML elements.
The fundamental behind locating elements using Xpath is the traversing between various
elements across the entire page and thus enabling a user to find an element with the reference of
another element.
Single Slash “/” – Single slash is used to create Xpath with absolute path i.e. the xpath would be
created to start selection from the document node/start node.
Double Slash “//” – Double slash is used to create Xpath with relative path i.e. the xpath would
be created to start selection from anywhere within the document.
The problem of same origin policy disallows to access the DOM of a document from an origin
that is different from the origin we are trying to access the document.
Origin is a sequential combination of scheme, host and port of the URL. For example, for a URL
http:// http://www.softwaretestinghelp.com/resources/, the origin is a combination of http,
softwaretestinghelp.com, 80 correspondingly.
Thus the Selenium Core (JavaScript Program) cannot access the elements from an origin that is
different from where it was launched. For Example, if I have launched the JavaScript Program
from “http://www.softwaretestinghelp.com”, then I would be able to access the pages within the
same domain such as “http://www.softwaretestinghelp.com/resources” or
“http://www.softwaretestinghelp.com/istqb-free-updates/”. The other domains like google.com,
seleniumhq.org would no more be accessible.
So, In order to handle same origin policy, Selenium Remote Control was introduced.
Selenium Grid can be used to execute same or different test scripts on multiple platforms and
browsers concurrently so as to achieve distributed test execution, testing under different
environments and saving execution time remarkably.
WebDriver
1. Implicit Wait
2. Explicit Wait
Implicit Wait: Implicit waits are used to provide a default waiting time (say 30 seconds)
between each consecutive test step/command across the entire test script. Thus, subsequent test
step would only execute when the 30 seconds have elapsed after executing the previous test
step/command.
Explicit Wait: Explicit waits are used to halt the execution till the time a particular condition is
met or the maximum time has elapsed. Unlike Implicit waits, explicit waits are applied for a
particular instance only.
User can use sendKeys(“String to be entered”) to enter the string in the textbox.
Syntax:
WebElement username = drv.findElement(By.id(“Email”));
// entering username
username.sendKeys(“sth”);
WebDriver facilitates the user with the following methods to check the visibility of the web
elements. These web elements can be buttons, drop boxes, checkboxes, radio buttons, labels etc.
1. isDisplayed()
2. isSelected()
3. isEnabled()
Syntax:
isDisplayed():
boolean buttonPresence = driver.findElement(By.id(“gbqfba”)).isDisplayed();
isSelected():
boolean buttonSelected = driver.findElement(By.id(“gbqfba”)).isDisplayed();
isEnabled():
boolean searchIconEnabled = driver.findElement(By.id(“gbqfb”)).isEnabled();
Get command is used to retrieve the inner text of the specified web element. The command
doesn’t require any parameter but returns a string value. It is also one of the extensively used
commands for verification of messages, labels, errors etc displayed on the web pages.
Syntax:
String Text = driver.findElement(By.id(“Text”)).getText();
Value in the drop down can be selected using WebDriver’s Select class.
Syntax:
selectByValue:
Select selectByValue = new Select(driver.findElement(By.id(“SelectID_One”)));
selectByValue.selectByValue(“greenvalue”);
selectByVisibleText:
Select selectByVisibleText = new Select (driver.findElement(By.id(“SelectID_Two”)));
selectByVisibleText.selectByVisibleText(“Lime”);
selectByIndex:
Select selectByIndex = new Select(driver.findElement(By.id(“SelectID_Three”)));
selectByIndex.selectByIndex(2);
Sample code:
driver.navigate().back();
navigate().forward() – This command lets the user to navigate to the next web page with
reference to the browser’s history.
Sample code:
driver.navigate().forward();
navigate().refresh() – This command lets the user to refresh the current web page there by
reloading all the web elements.
Sample code:
driver.navigate().refresh();
navigate().to() – This command lets the user to launch a new web browser window and navigate
to the specified URL.
Sample code:
driver.navigate().to(“https://google.com”);
driver.findElement(By.linkText(“Google”)).click();
The command finds the element using link text and then click on that element and thus the user
would be re-directed to the corresponding page.
The above mentioned link can also be accessed by using the following command.
driver.findElement(By.partialLinkText(“Goo”)).click();
The above command find the element based on the substring of the link provided in the
parenthesis and thus partialLinkText() finds the web element with the specified substring and
then clicks on it.
An inline frame acronym as iframe is used to insert another document with in the current HTML
document or simply a web page into a web page by enabling nesting.
Select iframe by id
driver.switchTo().frame(“ID of the frame“);
frame(Name of Frame)
driver.switchTo().frame(“name of the frame”);
------------
frame(WebElement element)
Select Parent Window
driver.switchTo().defaultContent();
findElement(): findElement() is used to find the first element in the current web page matching
to the specified locator value. Take a note that only first matching element would be fetched.
Syntax:
Syntax:
List <WebElement> elementList =
driver.findElements(By.xpath(“//div[@id=’example’]//ul//li”));
Q #29) How to find more than one web element in the list?
At times, we may come across elements of same type like multiple hyperlinks, images etc
arranged in an ordered or unordered list. Thus, it makes absolute sense to deal with such
elements by a single piece of code and this can be done using WebElement List.
Sample Code
close(): WebDriver’s close() method closes the web browser window that the user is currently
working on or we can also say the window that is being currently accessed by the WebDriver.
The command neither requires any parameter nor does is return any value.
quit(): Unlike close() method, quit() method closes down all the windows that the program has
opened. Same as close() method, the command neither requires any parameter nor does is return
any value.
Selenium is an automation testing tool which supports only web application testing. Therefore,
windows pop up cannot be handled using Selenium.
WebDriver offers the users with a very efficient way to handle these pop ups using Alert
interface. There are the four methods that we would be using along with the Alert interface.
void dismiss() – The accept() method clicks on the “Cancel” button as soon as the pop up
window appears.
void accept() – The accept() method clicks on the “Ok” button as soon as the pop up
window appears.
String getText() – The getText() method returns the text displayed on the alert box.
void sendKeys(String stringToSend) – The sendKeys() method enters the specified string
pattern into the alert box.
Syntax:
// accepting javascript alert
Alert alert = driver.switchTo().alert();
alert.accept();
Selenium is an automation testing tool which supports only web application testing, that means,
it doesn’t support testing of windows based applications. However Selenium alone can’t help the
situation but along with some third party intervention, this problem can be overcome. There are
several third party tools available for handling window based pop ups along with the selenium
like AutoIT, Robot class etc.
Q #34) How to assert title of the web page?
WebDriver offers a wide range of interaction utilities that the user can exploit to automate mouse
and keyboard events. Action Interface is one such utility which simulates the single user
interactions.
Thus, In the following scenario, we have used Action Interface to mouse hover on a drop down
which then opens a list of options.
Sample Code:
The values of the css properties can be retrieved using a get() method:
Syntax:
driver.findElement(By.id(“id“)).getCssValue(“name of css attribute”);
driver.findElement(By.id(“id“)).getCssValue(“font-size”);
1 import org.junit.After;
2 import org.junit.Before;
3 import org.junit.Test;
4 import java.io.File;
5 import java.io.IOException;
6 import org.apache.commons.io.FileUtils;
7 import org.openqa.selenium.OutputType;
8 import org.openqa.selenium.TakesScreenshot;
9 import org.openqa.selenium.WebDriver;
10 import org.openqa.selenium.firefox.FirefoxDriver;
11
12 public class CaptureScreenshot {
13 WebDriver driver;
14 @Before
15 public void setUp() throws Exception {
16 driver = new FirefoxDriver();
17 driver.get("https://google.com");
18 }
19 @After
20 public void tearDown() throws Exception {
21 driver.quit();
22 }
23
24 @Test
25 public void test() throws IOException {
26 // Code to capture the screenshot
27 File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
28 // Code to copy the screenshot in the desired location
FileUtils.copyFile(scrFile, new
29
File("C:\\CaptureScreenshot\\google.jpg"));
30 }
31 }
@Test: Annotation lets the system know that the method annotated as @Test is a test
method. There can be multiple test methods in a single test script.
@Before: Method annotated as @Before lets the system know that this method shall be
executed every time before each of the test method.
@After: Method annotated as @After lets the system know that this method shall be
executed every time after each of the test method.
@BeforeClass: Method annotated as @BeforeClass lets the system know that this
method shall be executed once before any of the test method.
@AfterClass: Method annotated as @AfterClass lets the system know that this method
shall be executed once after any of the test method.
@Ignore: Method annotated as @Ignore lets the system know that this method shall not
be executed.
Q #40) What is TestNG and how is it better than Junit?
TestNG is an advance framework designed in a way to leverage the benefits by both the
developers and testers. With the commencement of the frameworks, JUnit gained an enormous
popularity across the Java applications, Java developers and Java testers with remarkably
increasing the code quality. Despite being easy to use and straightforward, JUnit has its own
limitations which give rise to the need of bringing TestNG into the picture. TestNG is an open
source framework which is distributed under the Apache software License and is readily
available for download.
TestNG with WebDriver provides an efficient and effective test result format that can in turn be
shared with the stake holders to have a glimpse on the product’s/application’s health thereby
eliminating the drawback of WebDriver’s incapability to generate test reports. TestNG has an
inbuilt exception handling mechanism which lets the program to run without terminating
unexpectedly.
There are various advantages that make TestNG superior to JUnit. Some of them are:
Code Snippet
1 package TestNG;
2 import org.testng.annotations.*;
3 public class SettingPriority {
4 @Test(priority=0)
5 public void method1() {
6 }
7 @Test(priority=1)
8 public void method2() {
9 }
10 @Test(priority=2)
11 public void method3() {
12 }
13 }
Reusability of code
Maximum coverage
Recovery scenario
Low cost maintenance
Minimal manual intervention
Easy Reporting
1. Module Based Testing Framework: The framework divides the entire “Application
Under Test” into number of logical and isolated modules. For each module, we create a
separate and independent test script. Thus, when these test scripts taken together builds a
larger test script representing more than one module.
2. Library Architecture Testing Framework: The basic fundamental behind the
framework is to determine the common steps and group them into functions under a
library and call those functions in the test scripts whenever required.
3. Data Driven Testing Framework: Data Driven Testing Framework helps the user
segregate the test script logic and the test data from each other. It lets the user store the
test data into an external database. The data is conventionally stored in “Key-Value”
pairs. Thus, the key can be used to access and populate the data within the test scripts.
4. Keyword Driven Testing Framework: The Keyword driven testing framework is an
extension to Data driven Testing Framework in a sense that it not only segregates the test
data from the scripts, it also keeps the certain set of code belonging to the test script into
an external data file.
5. Hybrid Testing Framework: Hybrid Testing Framework is a combination of more than
one above mentioned frameworks. The best thing about such a setup is that it leverages
the benefits of all kinds of associated frameworks.
6. Behavior Driven Development Framework: Behavior Driven Development framework
allows automation of functional validations in easily readable and understandable format
to Business Analysts, Developers, Testers, etc.
Q #45) How can I read test data from excels?
Test data can efficiently be read from excel using JXL or POI API. See detailed tutorial here.
WebDriver cannot test Mobile applications. WebDriver is a web based testing tool, therefore
applications on the mobile browsers can be tested.
Q #49) Can captcha be automated?
Q #50) What is Object Repository? How can we create Object Repository in Selenium?
Object Repository is a term used to refer to the collection of web elements belonging to
Application Under Test (AUT) along with their locator values. Thus, whenever the element is
required within the script, the locator value can be populated from the Object Repository. Object
Repository is used to store locators in a centralized location instead of hard coding them within
the scripts.
In Selenium, objects can be stored in an excel sheet which can be populated inside the script
whenever required.
Hope in this article you will find answers to most frequently asked Selenium and WebDriver
Interview questions. The answers provided here are also helpful for understanding the Selenium
basics and advanced WebDriver topics.