Selenium Interview Questions
Selenium Interview Questions
WebDriver and RC: It provide the APIs for a variety of languages like
Java, .NET, PHP, etc. With most of the browsers Webdriver and RC works.
Grid: With the help of Grid you can distribute tests on multiple machines
so that test can be run parallel which helps in cutting down the time required
for running in browser test suites
ID
Name
Tag
Attribute
CSS
Linktext
PartialLink Text
Xpath etc
Functional
b)
Regression
For post release validation with continuous integration automation tool could be used
a)
Jenkins
b)
Hudson
c)
Quick Build
d)
CruiseCont
Double Slash //
For any reporting related capabilities have to depend on third party tools
Test
Before
After
Ignore
BeforeClass
AfterClass
RunWith
12) While using click command can you use screen coordinate?
To click on specific part of element, you would need to use clickAT command. ClickAt
command accepts element locator and x, y co-ordinates as argumentsclickAt (locator, cordString)
13) What are the advantages of Selenium?
14) Why testers should opt for Selenium and not QTP?
Selenium is more popular than QTP as
Selenium is used specially for testing web based applications while QTP
can be used for testing client serverapplication also
15) What are the four parameter you have to pass in Selenium?
Four parameters that you have to pass in Selenium are
Host
Port Number
Browser
URL
SetSpeed () : For specific amount of time it will stop the execution for every selenium
command.
This command is useful for demonstration purpose or if you are using a slow web
application
17) What is same origin policy? How you can avoid same origin policy?
The Same Origin Policy is introduced for security reason, and it ensures that content
of your site will never be accessible by a script from another site. As per the policy, any
code loaded within the browser can only operate within that websites domain.
To avoid Same Origin Policy proxy injection method is used, in proxy injection mode the
Selenium Server acts as a client configured HTTP proxy , which sits between the browser
and application under test and then masks the AUT under a fictional URL
18) What is heightened privileges browsers?
The purpose of heightened privileges is similar to Proxy Injection, allows websites to do
something that are not commonly permitted. The key difference is that the browsers are
launced in a special mode called heightened privileges. By using these browser mode,
Selenium core can open the AUT directly and also read/write its content without passing the
whole AUT through the Selenium RC server.
19) How you can use submit a form using Selenium ?
You can use submit method on element to submit form-
element.submit () ;
Alternatively you can use click method on the element which does form submission
20) What are the features of TestNG and list some of the functionality
in TestNG which makes it more effective?
TestNG is a testing framework based on JUnit and NUnit to simplify a broad range of testing
needs, from unit testing to integration testing. And the functionality which makes it efficient
testing framework are
21) Mention what is the difference between Implicit wait and Explicit
wait?
Implicit Wait: Sets a timeout for all successive Web Element searches. For the specified
amount of time it will try looking for element again and again before throwing a
NoSuchElementException. It waits for elements to show up.
Explicit Wait : It is a one-timer, used for a particular search.
22) Which attribute you should consider throughout the script in frame
for if no frame Id as well as no frame name?
You can use..driver.findElements(By.xpath(//iframe)).
This will return list of frames.
You will ned to switch to each and every frame and search for locator which we want.
Then break the loop
23) Explain what is the difference between find elements () and find
element () ?
find element ():
It finds the first element within the current page using the given locating mechanism. It
returns a single WebElement
findElements () : Using the given locating mechanism find all the elements within the
current page. It returns a list of web elements.
24) Explain what are the JUnits annotation linked with Selenium?
The JUnits annotation linked with Selenium are
@Before public void method() It will perform the method () before each
test, this method can prepare the test
Syntax-http://username:password@url
ex- http://creyate:tom@www.gmail.com
create test scripts in a desired programming language. It also allows executing test
scripts within the large spectrum of browsers.
Selenium WebDriver WebDriver is a different tool altogether that has various
advantages over Selenium RC. WebDriver directly communicates with the web
browser and uses its native compatibility to automate.
Selenium Grid Selenium Grid is used to distribute your test execution on multiple
Q #7) What is the difference between Selenium IDE, Selenium RC and WebDriver?
Feature
Selenium IDE
Selenium RC
WebDriver
Browser
Compatibility
Selenium IDE
comes as a
Firefox plugin,
thus it supports
only Firefox
Selenium RC
supports a
varied range of
versions of
Mozilla
Firefox,
Google
Chrome,
Internet
Explorer and
Opera
WebDriver
supports a
varied range of
versions of
Mozilla
Firefox, Google
Chrome,
Internet
Explorer and
Opera.
Also supports
HtmlUnitDrive
r which is a
GUI less or
headless
browser.
Feature
Selenium IDE
Selenium RC
WebDriver
Record and
Playback
Selenium IDE
supports record
and playback
feature
Selenium RC
doesn't
supports record
and playback
feature
WebDriver
doesn't support
record and
playback
feature
Server
Requirement
Selenium IDE
doesn't require
any server to be
started before
executing the
test scripts
Selenium RC
requires server
to be started
before
executing the
test scripts
WebDriver
doesn't require
any server to be
started before
executing the
test scripts
Architecture
Selenium IDE
is a Javascript
based
framework
Selenium RC is
a JavaScript
based
Framework
WebDriver uses
the browser's
native
compatibility to
automation
Object Oriented
Selenium IDE
is not an object
oriented tool
Selenium RC is
semi object
oriented tool
WebDriver is a
purely object
oriented tool
Dynamic Finders
(for locating web
elements on a
webpage)
Selenium IDE
doesn't support
dynamic
finders
Selenium RC
doesn't support
dynamic
finders
WebDriver
supports
dynamic
finders
Handling Alerts,
Navigations,
Dropdowns
Selenium IDE
doesn't
explicitly
provides aids to
handle alerts,
navigations,
dropdowns
Selenium RC
doesn't
explicitly
provides aids to
handle alerts,
navigations,
dropdowns
WebDriver
offers a wide
range of
utilities and
classes that
helps in
handling alerts,
navigations,
and dropdowns
efficiently and
effectively.
WAP
(iPhone/Android
) Testing
Selenium IDE
doesn't support
testing of
iPhone/Andrio
Selenium RC
doesn't support
testing of
iPhone/Andrio
WebDriver is
designed in a
way to
efficiently
Feature
Selenium IDE
Selenium RC
WebDriver
d applications
d applications
support testing
of
iPhone/Android
applications.
The tool comes
with a large
range of drivers
for WAP based
testing.
For example,
AndroidDriver,
iPhoneDriver
Listener Support
Selenium IDE
doesn't support
listeners
Selenium RC
doesn't support
listeners
WebDriver
supports the
implementation
of Listeners
Speed
Selenium IDE
is fast as it is
plugged in with
the webbrowser that
launches the
test. Thus, the
IDE and
browser
communicates
directly
Selenium RC is
slower than
WebDriver as it
doesn't
communicates
directly with
the browser;
rather it sends
selenese
commands over
to Selenium
Core which in
turn
communicates
with the
browser.
WebDriver
communicates
directly with
the web
browsers. Thus
making it much
faster.
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.
Q #15) When should I use Selenium Grid?
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.
Q #16) What do we mean by Selenium 1 and Selenium 2?
Selenium RC and WebDriver, in a combination are popularly known as Selenium 2. Selenium
RC alone is also referred as Selenium 1.
Q #17) Which is the latest Selenium tool?
WebDriver
Q #18) How do I launch the browser using WebDriver?
The following syntax can be used to launch Browser:
WebDriver driver = new FirefoxDriver();
WebDriver driver = new ChromeDriver();
WebDriver driver = new InternetExplorerDriver();
Q #19) What are the different types of Drivers available in WebDriver?
The different drivers available in WebDriver are:
FirefoxDriver
InternetExplorerDriver
ChromeDriver
SafariDriver
OperaDriver
AndroidDriver
IPhoneDriver
HtmlUnitDriver
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.
Q #21) How to type in a textbox using Selenium?
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);
Q #22) How can you find if an element in displayed on the screen?
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();
Q #23) How can we get a text of a web element?
Get command is used to retrieve the inner text of the specified web element. The command
doesnt 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();
Q #24) How to select value in a dropdown?
Value in the drop down can be selected using WebDrivers Select class.
Syntax:
selectByValue:
Select selectByValue = newSelect(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 = newSelect(driver.findElement(By.id(SelectID_Three)));
selectByIndex.selectByIndex(2);
Q #25) What are the different types of navigation commands?
Following are the navigation commands:
navigate().back() The above command requires no parameters and takes back the user
to the previous webpage in the web browsers history.
Sample code:
driver.navigate().back();
navigate().forward() This command lets the user to navigate to the next web page with
reference to the browsers 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);
Q #26) How to click on a hyper link using linkText?
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.
Q #27) How to handle frame in WebDriver?
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);
Locating iframe using tagName
driver.switchTo().frame(driver.findElements(By.tagName(iframe).get(0));
Locating iframe using index
frame(index)
driver.switchTo().frame(0);
1
public static void setAttribute(WebElement element, String attributeName, String value)
2
3
{
4
5
WrapsDriver wrappedElement = (WrapsDriver) element;
6
7
JavascriptExecutor driver = (JavascriptExecutor)wrappedElement.getWrappedDriver();
8
9
driver.executeScript("arguments[0].setAttribute(arguments[1],arguments[2])", element,
1
attributeName, value);
0
1
}
1
call the above method in the test script and pass the text field attribute and pass the text you want to
enter.
Ques 2) There is a scenario whenever Assert.assertEquals() function fails automatically it
has to take screenshot. How can you achieve this ?
Ans- By using EventFiringWebDriver.
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3
1
4
1
5
Ques 4) How to login into any site if its showing any authetication popup for user name and
pass ?
Ans pass the username and password with url.
1 Syntax- http://username:password@url
2
3 ex- http://creyate:jamesbond007@alpha.creyate.com
Ques 6) Open a browser in memory means whenever it will try to open a browser the browser
page must not come and can perform the operation internally.
Ans- use HtmlUnitDriver.
ex-
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
h) An xml file can be generated to execute the entire test suite. In that xml file we can rearrange our
execution order and we can also skip the execution of particular test case.
Ques 8) How do you take screen shot without using EventFiringWebDriver ?
Ans-
1 act.sendKeys(Keys.RETURN);
For Tab-
1 act.sendKeys(Keys.ENTER);
1 Syntax driver.switchTo().defaultContent();
Ques 13) How to type text in a new line inside a text area ?
Ans- Use \n for new line.
Ans- 1st go through all the manual test cases and identify the steps which are repeating. Note down
such steps and make them as methods and write into ProjectSpecificLibrary.
Ques 20) What are different components of your framework ?
Ans- Library- Assertion, ConfigLibrary, GenericLibrary, ProjectSpecificLibrary, Modules.
Drivers folder, Jars folder, excel file.
Ques 21) What are the browsers supported by Selenium IDE ?
Ans- Mozilla FireFox only. Its an Firefox add on.
Ques 22) What are the limitations of Selenium IDE ?
Ansa) It does not supports looping or conditional statements. Tester has to use native languages to write
logic in the test case.
b) It does not supports test reporting, you have to use selenium RC with some external reporting
plugin like TestNG or JUint to get test execution report.
c) Error handling is also not supported depending on the native language for this.
d) Only support in Mozilla FireFox only. Its an Firefox add on.
Ques 23) How to check all checkboxes in a page ?
Ans-
Ques 25) How do you identify the Xpath of element on your browser ?
And- to find the xpath , we use Firebug addons on firefox browser and to identify the xpath written we
use Firepath addons.
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5
2
6
2
7
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
1 try{
2
3 Assert.assertEquals(expUserName, actUserName);
4
5 }catch(Exception e){
6
7 Syste.out.println(name is invalid);
8
9}
Question 2: On my machine port 4444 is not free. How do Use another port?
Answer:
3). If you want to know about the database or data source to which you are connected
then the Connection object gathers information about the database or data source by
the use of DatabaseMetaData.
4). It helps us to close the data source. The Connection.isClosed() method returns true
only if the Connection.close() has been called. This method is used to close all the
connection.
Firstly we need to establish the connection with the database. This is done by using the
method DriverManager.getConnection(). This method takes a string containing a URL.
The DriverManager class, attempts to locate a driver that can connect to the database
represented by the string URL. Whenever the getConnection() method is called the
DriverManager class checks the list of all registered Driver classes that can connect to
the database specified in the URL.
Syntax:
ID
Name
3.
4.
5.
Dom
Question 6: How do you verify an object present in multiple pages?
Answer:
Check on each page assertTrue(selenium.isElementPresent(locator));
Question 7: What is the difference between single and double slash in xpath?
Answer:
If xpath starts selection from the document node, itll allow you to create absolute path
expressions.
1 // sample
2 Selenium.prototype.doFunctionName = function(){
3
4}
The do in front of the function name tells Selenium that this function can be called as
a command for a step instead of an internal or private function.
Question 9: How do you verify the presence of an element after the successful
page loading?
Answer:
It can be achieved with the following line of code.
Just mention some time value to check the element (in seconds) like:
Java Sample
Java
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
run a Selenium test suite to a fraction of the time that a single instance of Selenium
instance would take to run.
Question 11: How to start the selenium server from your language class?
Answer:
1
2
3
4
5
6
7
8
9
1
0
1
1
try {
seleniumServer = new SeleniumServer();
seleniumServer.start();
} catch (Exception e) {
e.printStackTrace();
}
Question 13: What is XPath? When would I have to use XPath in Selenium?
Answer:
XPath is a way to navigate in xml document and this can be used to identify elements in
a web page. You may have to use XPath when there is no name/id associated with
element on page or only partial part of name/ide is constant.
Direct child is denoted with /
Relative child is denoted with //
Id, class, names can also be used with XPath
//input[@name=q]
//input[@id=lst-ib]
//input[@class= lst]
//input[contains(@id,lst-ib)]
css=input[name=q]
css=input[id=lst-ib] or input#lst-ib
css=input[class= lst] or input.lst
css=input[id*= lst-ib )]
Question 15: There is id, name, XPath, CSS locator, which one should I use?
Answer:
If there are unique names or identifier available then they should be used instead of
XPath and CSS locators. If not then css locators should be given preference as their
evaluation is faster than XPath in most modern browsers.
Question 16: What is the mechanism to handle multiple popups in selenium?
Answer:
Multiple popups can be handled by using the command getWindowHandles().
Then store all the window names into Set<String> variable and transform it into an
array.
Next by using the array index, you can navigate to specific window by using
driver.switchTo().window(ArrayIndex);
Question 17: How do you handle Ajax controls using selenium?
Answer:
Lets consider an example. Say the google test box which is an ajax control and when
we enter some text into it, then it displays the auto suggested values.
To work with such controls, you need to capture all the suggested values in a string after
entering the value in textbox. Then just split the string and take the values.
Go to Selenium interview questions and answers.
Get method will get a page to load or get page source or get text thats all. Whereas
navigate will guide through the history like refresh, back, forward. For example if we
want to move forward and do some functionality and back to the home page. This can
be achieved through navigate() method only. driver.get() will wait till the whole page
gets loaded and driver.navigate() will just redirect to that page and will not wait.
Question 20: How is the implicit Wait different from Explicit wait?
Implicit Wait sets internally a timeout that will be used for all consecutive Web Element
searches. It will try lookup the element again and again for the specified amount of time
before throwing a NoSuchElementException if the element could not have been found. It
does only this and cant be forced into anything else it waits for elements to show up.
Explicit Wait or just Wait is a one-timer used by you for a particular search. It is more
extendible in the means that you can set it up to wait for any condition you might like.
Usually, you can use some of the prebuilt Expected Conditions to wait for elements to
become clickable, visible, invisible, etc., or just write your own condition that suits your
needs.
Question 21: How to Handle Alerts/Popups in Selenium WebDriver?
Answer:
There are two types of alerts which are commonly referred
1.
Robot class is a java based utility which emulates the keyboard and mouse
actions and can be effectively used to handling window based pop up with the help
of keyboard events.
2.
The keyPress and keyRelease methods simulate the user pressing and releasing a
certain key on the keyboard respectively.
Question 22: How to take a screenshot with Selenium WebDriver?
Answer:
1
2
3
4
5
6
7
8
9
1
0
1
1
Import org.apache.commons.io.FileUtils;
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));
Question 23: How to address SSL Certificate issue in Firefox with WebDriver
(or) how do you manage the secured connection error in HTTPS?
Answer:
Java
ID,
Name,
3.
4.
CSS,
XPath,
5.
6.
Classname,
TagName,
7.
8.
LinkText, and
Partial Link Text.
With the actions object you should first move the menu title, and then move to the
popup menu item and click it. Dont forget to call actions.perform() at the end. Heres
some sample Java code:
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3
working as expected. TDD was introduced first by XP. I believe I have explained enough
in simple terms.
BDD Behavior Driven Development.
Behavior-driven development combines the general techniques and principles of TDD
with ideas from domain-driven design. Its purpose is to help the folks devising the
system (i.e. the developer) identify appropriate tests to writethat is, tests that reflect
the behavior desired by the stakeholders.
DDD-Domain Driven Development.
DDD is about mapping business domain concepts into software artifacts. A DDD
framework offers following benefits:
Helps the team to create a common model, between the business and IT
stakeholders
The model is modular, extensible and easy to maintain as the design reflects the
business model.
It improves the re-usability and testability of the business domain objects.
1.
2.
3.
4.
@Before/AfterTest and
@Before/AfterGroup
5.
In TestNG we can tell the test that one method is dependent on another method
whereas in Junit this is not possible.
6.
Grouping of test cases is available in TestNG whereas the same is not available in
Junit. Execution can be done based on Groups. For example, if you have defined
many cases and segregated them by defining 2 groups as Sanity and Regression.
And if you only want to execute the Sanity cases then just tell TestNG to execute
the Sanity. TestNG will automatically execute the cases belonging to the Sanity
group.
7.
Also TestNG supports parallel test case execution.
Question 31: What are the different Parameters for @Test annotation?
Answer:
Parameters are keywords that modify the annotations function.
Question 32: Can we run group of test cases using TestNG?
Answer:
Test cases in group using TestNG will be executed with the below options.
If you want to execute the test cases based on one of the group like regression test or
smoke test@Test(groups = {regressiontest, smoketest})
Question 33: Which web driver implementation is the fastest?
Answer:
HTMLUnitDriver. Simple reason is HTMLUnitDriver does not execute tests on browser but
use plain http request-response. It is far quicker than launching a browser and executing
tests.
Question 34: Is it possible to use Selenium RC API with Selenium 2.0?
Answer:
You can emulate Selenium 1.0 API with Selenium 2.0. But not all of the Selenium 1.0
methods are supported. To achieve this you need to get Selenium instance from
WebDriver and use Selenium methods. Method executions might also be slower while
simulating Selenium 1.0 with in Selenium 2.0.
Question 35: How do I use Selenium Grid while using Java, .Net or Ruby?
Answer:
With java you can take advantage of parallel testing capabilities of TestNG to