Selenium WebDriver Interview questions

Sunandip
7 min readApr 22, 2019
  1. What is automation testing?
    Automation testing is the process of enabling a tool/machine to perform manual tasks without manual intervention. The expected results of a test are fed into the tool to be compared with the actual output of the test.
  2. What are the components of Selenium suite ?
    The Selenium suite package consists of the following components:
    Selenium IDE (Integrated Development Environment)
    Selenium RC
    Selenium Webdriver
    Selenium Grid
  3. What are the testing types that can be supported by Selenium?
    Selenium supports the following types of testing:
    Functional Testing
    Regression Testing
  4. What is the difference between Selenium 1.0 and Selenium 2.0.
    Selenium 1.0 consists of only Selenium RC where as Selenium RC and WebDriver combined together to form Selenium 2.0.
  5. What is the difference between single slash (/) and double slash (//) in Xpath?
    Single slash (/) is used to create Xpath with absolute path. In this case, the Xpath starts selection from the document root node.
    Double slash (//) is used to create Xpath with relative path. In this case, the Xpath starts selection from anywhere within the document.
  6. What are the different types of locators in Selenium?
    ID, ClassName, Name, TagName, LinkText, PartialLinkText, XPath, CSS Selector, by java script.
  7. What are the differences between Selenium 2.0 and Selenium 3.0?

The biggest difference between selenium 3.0 and selenium 2.x is that now, in Selenium 3, all the major browser vendors will ship their own WebDriver implementations (Apple, Google, Microsoft, and Mozilla) instead of being developed by Selenium project (Selenium 2.x) and because they know their browsers better than anyone else.

Support for Firefox browser provided by Mozilla’s gecko driver.

No need to maximize the browser using driver.manage().window().maximize(); By default the window will be maximized.

Selenium 3 has bug fixes from selenium 2 also it is more mobile automation focused.One of the major differences introduced in Selenium 3 was the introduction of the Appium project. The mobile-testing features that were part of Selenium 2 are now moved into a separate project named Appium.

More than 9+ versions of IE are supported in Selenium 3.0.

Apple has come up with its own SafariDriver to run your tests in Safari on Mac

Slenium WebDriver has become the W3C Standard. The W3C standard will encourage compatibility across different software implementations of WebDriver API.

Minimum Java version required is now 8+

Selenium 3.0 doesn’t support Firefox 47.0.0 version at all. Firefox is fully supported at version 47.0.1 or more.

8.Does Selenium have limitations? If so, what are some of these limitations?
Yes, It has. Below are some:

10.What is the purpose of Selenium Grid?
It allows us to run multiple functional tests in parallel, on multiple machines, against different browsers, in a heterogeneous environment.

11.What is Selenese?
Selenese is name given for set of commands used for in Selenium IDE.

13. What is the difference between driver.close and driver.quit command?
driver.close() will close the current instance of browser. driver.quit() closes all the browser instances opened so far as a part of program execution.

14. How does Verify and Assert differs?
Verify : Verify checks whether given conditions are met or not. If condition is not met it will not abort or terminate the execution. It will continue with the next step.
Assert : Assert checks whether given conditions are met or not. If condition is not met it will abort/terminate the execution. It will mark it as a failure, will not continue further.

16.What are different types of waits in selenium? Why do you require waits?
When tests are running, the application may not always respond with the same speed.
To handle these anticipated timing problems by synchronizing your test to ensure that
Selenium WebDriver waits until your application is ready before performing the next step.
There are two types of waits in selenium.
1. Implicit wait:
In implicit wait, program will wait for an element for given amount of time. It will search the element by polling(again and again) with in the time limit.
If element is not found, it will throw No such Element Found exception. It is applicable to entire script.
Need not to re write.
Syntax:

 driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);

2. Explicit wait:
This is for specific instance or condition or particular search. Can be called/used in that instance only.
we have to provide ExpectedCondtions for wait to stop.

 visibilityOfAllElements(),alertIsPresent(),elementToBeClickable(),visibilityOf() etc..

3. Fluent wait:
It is also kind of explicit wait. Unlike explicit wait, FluentWait uses a maximum timeout value and polling frequency. For example, if we set the maximum timeout value as 20 seconds and polling frequency as 2 seconds, WebDriver will check for an element every 2 seconds until the maximum value. In addition to this, we can confgure FluentWait to ignore specifc types of exceptions while waiting for the condition.
The FluentWait class is helpful in automating AJAX applications or scenarios where element load time varies often.

17.What are some exceptions in Selenium?
1.NoSuchElementException: When an element is not found on the web page.
2.NoAlertPresentException: Happens when you switch to no presented alert.
3.NoSuchWindowException: If a browser window is not present when code is expecting a new window.
4.StaleElementException: If an element is either deleted or no longer attached to the DOM.
5.NoSuchFrameException: Takes place if frame target to be switch does not exist.
6.SessionNotFoundException: The WebDriver is performing the action right after you quit the browser.
7.UnhandledAlertException: It happens when there is an alert, but WebDriver is unable to perform Alert operation.
8.WebDriverException: This takes place when the WebDriver is performing the action right after you close the browse.
9.ElementNotVisibleException: Thrown when an element is present on the DOM, but it is not visible, and so is not able to be interacted with.
Most commonly encountered when trying to click or read text of an element that is hidden from view.
10.ElementNotSelectableException: Thrown when trying to select an unselectable element. For example, selecting a ‘script’ element

18.How to clear and enter text in field?
Commands to do this are:

element.clear();

element.sendkeys(“learnmodeon”);

19. How to get text entered in field?
Commands to do this are:

String text=element.gettext()

20. What is the difference between findElements() and findElement() ?
Let us consider a case where a webpage contain multiple elements having same id.
findElement() : Returns the instance of first element.
findElements() : Returns list containing all elements.

If there is no element at all.
findElement() — will Throw NoSuchElementException.
findElements()- Returns empty list(i.e., length of list is 0)

21. How to select and element from dropdown?
Selecting/Deselecting an element from dropdown can be achieved in 3 ways. It will work only when html code of page contains select tag.
Example: Drop down of colors containing black and white.
a.Selection/deselection by visible text:
We can select an option by its visible text.
color.selectByVisibleText(“Black”); # Selects black color.

  1. b.Selection/deselection by value:
    We can select an option by its value attribute.
    color.selectByValue(“bl”);
  2. b.Selection/deselection by index:
    We can select an option by its index.
    color.selectByIndex(0); # Selects black color.

22. How to get list of available options in a dropdown?
Using getOptions() method we can retrieve all the options present.

23. How to get list of selected options in a dropdown?
Using getAllSelectedOptions() method we can retrieve all the options selected.

24. Explain different navigation commands on browser?
driver.navigate().back(); — Takes back the user to the previous webpage.
driver.navigate().forward(); — It will navigate to the next web page depends on browser’s history.
driver.navigate().refresh() — Refresh the current web page.
driver.navigate().to(“url”) — Navigate to the specified URL.

25. How to handle Pop-ups/alerts?
Using alert interface alerts can be handled. Methods available are:
To switch to alert-
Syntax : Alert alert = driver.switchTo().alert();

After switching to alert-
To dismiss the alert:
Syntax : alert.dismiss();
To accept the alert:
Syntax : alert.accept();
To get the content/message of the alert:
Syntax : alert.gettext();

26. How to handle frames?
Check for iframe tag in html code.
Mutiple ways we can switch to frame and execute tests-
a. Switch to frame by id
Syntax : driver.switchTo().frame(“id”);
b. Switch to frame by name
Syntax : driver.switchTo().frame(“Name”);
c. switch to frame by tagName
Syntax : driver.switchTo().frame(driver.findElements(By.tagName(“iframe”).get(0));

27. How to perform submit operation?
It can be done in two ways.
1. By clicking on the respective button.
ele.click();
2. By using submit method on the respective button.
ele.submit();

29. How to verify whether radio button is selected or not?
We can check whether a radio button is selected or deselected using the isSelected() method. This method return a boolean value.

30. How to verify text color in selenium webdriver?
Using CSS attribute value we can validate color. You could use .getCssValue to get the value of color.

31. How to verify whether radio button is selected or not?
We can check whether a radio button is selected or deselected using the isSelected() method. This method return a boolean value.

WebElement simpleTable = driver.findElement(By.id("items"));
Number of rows of this table can be retrieved through as
List WebElement rows = simpleTable.findElements(By.tagName("tr"));

36. How to do drag and drop operation?
We will use the Actions class to perform drag and drop operation.

37. How to click on an item in hover menu?
We will use the Actions class to achieve this.

38. How to check whether page is fully loaded or not without using wait for an element?
Using JavascriptExecutor we can achieve this: When page is fully loaded document ready state will be in completed status.

JavascriptExecutor jexec = (JavascriptExecutor)driver;
jexec.executeScript("return document.readyState").toString().equals("complete");

39. How to switch to another tab in same window?
Selenium does not provide ready made method for this. Need to follow same steps as of manual.
This can be done by entering ctrl+tab using keys actions.
Syntax:
From left to right tabs:

Actions act= new Actions(driver);
act.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).build().perform();

From right to left tabs:

Actions act= new Actions(driver);
act.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys(Keys.TAB).build().perform();

40. How to capture screenshot in Selenium?
Using TakesScreenshot interface capturing a screenshot is possible(TakesScreenshot relies on the browser API to capture
the screenshots. The HtmlUnit driver does not support the TakesScreenshot interface):
Syntax:

File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("report/menu.png"))

Originally published at https://medium.com on April 22, 2019.

--

--