50 Selenium Interview Question
50 Selenium Interview Question
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.
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
Listener Support Selenium IDE doesn't Selenium RC doesn't WebDriver supports the
support listeners support listeners implementation of
Listeners
Origin is a sequential combination of scheme, host, and port of the URL. For example, for a
URL 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.
FirefoxDriver
InternetExplorerDriver
ChromeDriver
SafariDriver
OperaDriver
AndroidDriver
IPhoneDriver
HtmlUnitDriver
Q #20) What are the different types of waits available in WebDriver?
There are two types of waits available in 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.
Syntax:
WebElement username = drv.findElement(By.id(“Email”));
// entering username
username.sendKeys(“sth”);
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();
Syntax:
String Text = driver.findElement(By.id(“Text”)).getText();
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);
The above-mentioned link can also be accessed by using the following command.
driver.findElement(By.partialLinkText(“Goo”)).click();
The above command finds 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.
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);
frame(Name of Frame)
driver.switchTo().frame(“name of the frame”);
frame(WebElement element)
Select Parent Window
driver.switchTo().defaultContent();
Q #28) When do we use findElement() and findElements()?
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:
WebElement element = driver.findElements(By.xpath(“//div[@id=’example’]//ul//li”));
findElements(): findElements() is used to find all the elements in the current web page
matching to the specified locator value. Take a note that all the matching elements would
be fetched and stored in the list of WebElements.
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
1 // Storing the list
List <WebElement> elementList =
2
driver.findElements(By.xpath("//div[@id='example']//ul//li"));
3 // Fetching the size of the list
4 int listSize = elementList.size();
5 for (int i=0; i<listSize; i++)
6{
7 // Clicking on each service provider link
8 serviceProviderLinks.get(i).click();
// Navigating back to the previous page that stores link to service
9
providers
10 driver.navigate().back();
11 }
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:
1 // Instantiating Action Interface
2 Actions actions=new Actions(driver);
3 // howering on the dropdown
actions.moveToElement(driver.findElement(By.id("id of the
4
dropdown"))).perform();
5 // Clicking on one of the items in the list options
6 WebElement subLinkOption=driver.findElement(By.id("id of the sub link"));
7 subLinkOption.click();
Q #36) How to retrieve CSS properties of an element?
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”);
Q #37) How to capture screenshot in WebDriver?
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
29 FileUtils.copyFile(scrFile, new File("C:\\CaptureScreenshot\\google.jpg"))
30 }
31 }
Q #38) What is Junit?
Junit is a unit testing framework introduced by Apache. Junit is based on Java.
Q #39) What are Junit annotations?
Following are the JUnit Annotations:
@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 methods.
@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 methods.
@AfterClass: Method annotated as @AfterClass lets the system know that this
method shall be executed once after any of the test methods.
@Ignore: Method annotated as @Ignore lets the system know that this method
shall not be executed.
1 JXL supports “.xls” format i.e. binary based format. POI jar supports all of these
JXL doesn’t support Excel 2007 and “.xlsx” format i.e. formats
XML based format
2 JXL API was last updated in the year 2009 POI is regularly updated and
released
3 The JXL documentation is not as comprehensive as POI has a well prepared and
that of POI highly comprehensive
documentation
4 JXL API doesn’t support rich text formatting POI API supports rich text
formatting
5 JXL API is faster than POI API POI API is slower than JXL API
Q #47) What is the difference between Selenium and QTP?
Feature Selenium Quick Test Professional (QTP)
Browser Selenium supports almost all the QTP supports Internet Explorer,
Compatibility popular browsers like Firefox, Chrome, Firefox and Chrome. QTP only
Safari, Internet Explorer, Opera etc supports Windows Operating
System
Application Selenium supports testing of only web QTP supports testing of both the
under Test based applications web based application and windows
based application
Vendor As Selenium is a free tool, user would Users can easily get the vendor’s
Support not get the vendor’s support in support in case of any issue
troubleshooting issues
In Selenium, objects can be stored in an excel sheet which can be populated inside the
script whenever required.