Selenium Webdriver Interview Questions & Answers
Selenium Webdriver Interview Questions & Answers
Selenium Webdriver Interview Questions & Answers
com
Q 1: What is Selenium?
Ans: Selenium is a suite of software tools to automate web browsers across many platforms
(Different Operation Systems like MS Windows, Linux Macintosh etc.). It was launched in
2004, and it is open source Test Tool suite.
Ans: Web testing tools Selenium RC and WebDriver are consolidated in single tool in Selenium
2.0
Ans: Selenium WebDriver is a tool for writing automated tests of websites. It is an API name
and aims to mimic the behavior of a real user, and as such interacts with the HTML of the
application. Selenium WebDriver is the successor of Selenium Remote Control which has been
officially deprecated.
AndroidDriver,
ChromeDriver,
EventFiringWebDriver,
FirefoxDriver,
HtmlUnitDriver,
InternetExplorerDriver,
1
WWW.learnunwired.com
IPhoneDriver,
IPhoneSimulatorDriver,
RemoteWebDriver
Ans: In case of Selenium 1.0 you need Selenium jar file pertaining to one library for example in
case of java you need java client driver and also Selenium server jar file. While with Selenium
2.0 you need language binding (i.e. java, C# etc) and Selenium server jar if you are using
Remote Control or Remote WebDriver.
driver.get("https://www.google.co.in/");
Ans: HTMLUnitDriver. Simple reason is HTMLUnitDriver does not execute tests on browser
but plain http request – response which is far quick than launching a browser and executing tests.
But then you may like to execute tests on a real browser than something running behind the
scenes
Q 9: What all different element locators are available with Selenium 2.0?
Ans: Selenium 2.0 uses same set of locators which are used by Selenium 1.0 – id, name, css,
XPath but how Selenium 2.0 accesses them is different. In case of Selenium 1.0 you don’t have
to specify a different method for each locator while in case of Selenium 2.0 there is a different
method available to use a different element locator. Selenium 2.0 uses following method to
access elements with id, name, css and XPath locator –
driver.findElement(By.id("HTMLid"));
2
WWW.learnunwired.com
driver.findElement(By.name("HTMLname"));
driver.findElement(By.cssSelector("cssLocator"));
driver.findElement(By.xpath("XPathLocator));
el.submit();
Ans:
Ans:
WebElement el = driver.findElement(By.id("ElementID"));
el.clear();
Ans:
Ans: I am going to show you how to capture clip of page element using WebDriver.
3
WWW.learnunwired.com
Below I have written a “CaptureElementClip.java“java webdriver test script of a google
application where I capture google menu clip and save into project.
package com.webdriver.test;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
4
WWW.learnunwired.com
private WebDriver driver;
@BeforeSuite
baseUrl = "http://google.com";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
@Test
driver.get(baseUrl);
.getScreenshotAs(OutputType.FILE);
5
WWW.learnunwired.com
//get element dimension
height);
FileUtils.copyFile(screen, file);
@AfterSuite
driver.quit();
Ans:
el.click()
6
WWW.learnunwired.com
//verfiy to radio button is check it return true if selected else false
el.isSelected()
Q 17: How to count total number of rows of a table using Selenium 2.0?
Ans:
Ans:
Ans:
Ans:
Q 21: How to assert text assert text of webpage using selenium 2.0?
Ans:
WebElement el = driver.findElement(By.id("ElementID"));
7
WWW.learnunwired.com
Assert.assertEquals("Element Text", text);
Ans:
WebElement el = driver.findElement(By.id("ElementID"));
Ans:
WebElement el = driver.findElement(By.id("ElementID"));
builder.doubleClick(el).build().perform();
Ans:
Ans:
driver.manage().window().maximize();
Ans: I will explain the procedure to verify PDF file content using java WebDriver. As some time
we need to verify content of web application PDF file, opened in browser.
Use below code in your test scripts to get PDF file content.
8
WWW.learnunwired.com
PDFParserPDFParser = newPDFParser(fileToParse);
PDFParser.parse();
StringPDFtxt = newPDFTextStripper().getText(pdfParser.getPDDocument());
//closePDFParser object
PDFParser.getPDDocument().close();
After applying above code, you can store allPDF file content into “pdftxt” string variable. Now
you can verify string by giving input. As if you want to verify “Selenium or WebDiver” text. Use
below code.
Assert.assertTrue(pdftxt.contains(“Selenium or WebDiver”))
Ans: I will explain you to verify HTTP response code 200 of web application using java
webdriver. As webdriver does not support direct any function to verify page response code. But
using "WebClient" of HtmlUnit API we can achieve this.
9
WWW.learnunwired.com
Html unit API is GUI less browser for java developer, using WebClent class we can send request
to application server and verify response header status.
Below code, I used in my webdriver script to verify response 200 of web application
//verify response
Assert.assertEquals(200,htmlPage.getWebResponse().getStatusCode());
Assert.assertEquals("OK",htmlPage.getWebResponse().getStatusMessage());
credential.addCredentials("UserName", "Passeord");
webClient.setCredentialsProvider(credential);
10
WWW.learnunwired.com
//verify response
Assert.assertEquals(200,htmlPage.getWebResponse().getStatusCode());
Assert.assertEquals("OK",htmlPage.getWebResponse().getStatusMessage());
Ans: I have explained that how to verify images in webdriver using java. As webdriver does not
provide direct any function to image verification, but we can verify images by taking two screen
shots of whole web page using “TakesScreenshot” webdriver function, one at script creation time
and another at execution time,
In below example I have created a sample script in which first I captured a Google home page
screen shot and saved (GoogleInput.jpg) into my project, Another screen shot
“GoogleOutput.jpg” captured of same page at test executing time and saved into project. I
compared both images if they are not same then test will script fail.
package com.test;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
11
WWW.learnunwired.com
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
@BeforeSuite
baseUrl = "https://www.google.co.in/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
@AfterSuite
driver.quit();
12
WWW.learnunwired.com
@Test
driver.navigate().to(baseUrl);
getScreenshotAs(OutputType.FILE);
Thread.sleep(3000);
if(sizefileInput == sizefileOutPut) {
if(dafileInput.getElem(j) != dafileOutPut.getElem(j)) {
13
WWW.learnunwired.com
matchFlag = false;
break;
else
matchFlag = false;
One of my applications had Http authentication for security purpose and I had need to automate
using webdriver. As we know http authentication is not a part of DOM object so we cannot
handle it using webdriver. Here is approach to handle such type situation.
We need to pass http credential with URL to skip http popup authentication window. Below is
URL format.
http://username:passwork@applicationURL
14
WWW.learnunwired.com
driver.get(“http://username:passwork@applicationURL”)
After using above approach, http authentication popup window disappear. But in Internet
explorer, it raise error with message “wrong format url”. To accept same type url in internet
explorer browser we need to add a DWORD value named exploere.exe and iexplore.exe with
value data 0 in below registry.
For all users of the program, set the value in the following registry key:
HKEY_LOCAL_MACHINE\Software\Microsoft\Internet
Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE
For the current user of the program only, set the value in the following registry key:
HKEY_CURRENT_USER\Software\Microsoft\Internet
Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE
If you are using 64 bit machine, set the value in the following registry key.
HKEY_CURRENT_USER\Software\WOW6432Node\Microsoft\Internet
Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE
15