Top 25 selenium interview questions

Top 25 Selenium Interview Questions:

In this tutorial please find top 25 selenium interview questions.

1. What is selenium?

  • Selenium is an open source (no licensing costs) automation tool.
  • It is used for web application testing. It works with different browsers like Firefox, Chrome, IE, Safari and Opera.
  • We can choose different programming languages to write selenium scripts. Languages supported in selenium are Java, Python, C#, Ruby, Pearl and .Net.

2. Explain Asset and Verify commands in selenium?

Assert and Verify commands are used to verify the presence of an element like text, button, dropdown or link etc., in the UI.

Assert: If a particular element is not present in the UI, then the test will abort immediately.

Verify:  If a particular element is not present in the UI, then the test will not abort and it will proceed to the next steps, and it logs a failure.


3. What is selenese?

Selenese is a language used to write test scripts in Selenium IDE. Selenese contains below set of commands.

i. Actions: These are used to modify the web application state. Using actions, we can make selenium wait for a particular element, clickAndWait or hover on the dropdown.

Example for Actions in Selenium WebDriver:

 

ii. Accessors: These are used to analyze the web application state and are used to store the results in variables.

Ex:

storeAllWindowTitles (varName) – Stores all window titles opened in browser and returns in the form of an array.

storeText (Elemlocator, varName) – Stores the text of any web element which has text.

storeTitle (variableName) – Stores the title of the currently opened page.

iii. Assertions: Are of three types.

  • assert
  • verify
  • waitFor

4. What is the difference between final, finally and finalize keywords?

Ans:

Final is the keyword used in java to store a value which is constant (You cannot change the value).

Finally is a block in selenium to execute important test code. Finally block will always get executed even if there is an exception occurred. For example, In trycatchfinally block, exception has occurred in try block. Catch block will handle the exception and then the finally block will get executed.

Ex:

 

Output:

i/0 is not possible.

finally block excuted.

Finalize() method is used for garbage collection or clean up process.


5. Different types of locators in Selenium?

  1. Id
  2. Xpath
  3. Css Selector
  4. Link Text
  5. Partial Link Text
  6. Name
  7. Tag Name
  8. Attribute

Ans: Click here to see the different types of locators used in selenium


6. Write a code to select a value from the drop down?

Ans: Click here to know how to select a drop down in selenium webdriver.


7. Difference between relative and absolute xpath?**

Ans: “/” means absolute xpath and “//” is used for relative xpath.

In absolute xpath, xpath of an element is retrieved from the root of the element. So even if a small change in the path of the element can make the test script failure.

Ex: html/body/div[1]/header/nav/ul/li[5]/ul/li[1]/a

In relative xpath, nearby id or attribute is used to generate the xpath. So the chances of element id changes are very less. Due to this reason, relative xpath is preferred over absolute xpath.

Ex: .//*[@id=’menu-item-356′]/a

Note: Install firebug and firepath add-ons in firefox, to generate the xpath easily. These add-ons are available only in firefox browser.

8. What is the difference between setSpeed() and sleep() methods?

setSpeed() method delays the every selenium operation/step in the script.

Ex: setSpeed(2000): It delays the execution by 2 seconds for every step.

sleep() method is used to delay a particular selenium operation.

Ex: sleep(2000): It will wait for 2 seconds before proceeding to next step. So it is applicable for specific step where it is written.

9. FirefoxDriver is a class or an interface?

FirefoxDriver is a class that has been written specifically for the firefox browser. It has methods that are implemented and it can be instantiated. It can perform all methods on the firefox browser as defined in the interface WebDriver. Hence firefoxDriver is a java class and it implements the WebDriver interface.

10. What is the syntax of xpath in selenium?

//tagName[@attributeName = ‘attributeValue’]

Ex:

//input[@id = ‘username’]

//input[@id = ‘pwd’]

//input[@id = ‘submit’]

11. Is there any super interface for WebDriver?

SearchContext is the super interface of WebDriver.

12. What is the difference between findElements() and findElement()?

findElements() method returns list of web elements present in the DOM. If it doesn’t find any element then it returns an empty list. So it won’t throw any error if it was not able to find any element.

findElement() method return only one web element. If it finds more than 1 web elements then it returns the first element in DOM. If there are no matching elements then it will throw ElementNotFound exception.

13. What is Fluent Wait in Selenium? How it is different from explicit wait?

FluentWait is different from explicit wait. Fluent wait is used to define the maximum amount of time for
webdriver to wait for a specific element to appear or display and with polling frequency to check for the condition. If the element appears in 2nd sec itself then it won’t check further and continues with further steps. Where as in explicit wait, webdriver waits till 10 sec are completed to check for the condition.

For example, if we give fluent wait as 10 sec and polling frequency as 1 sec, it checks the condition for every 1 sec for 10 times.

14. What are the common exception you have faced in selenium?

  1. NoSuchElementException
  2. StaleElementReferenceException
    1. A stale element reference exception is thrown in one of two cases, the first being more common than the second:
      • The element has been deleted entirely.
      • The element is no longer attached to the DOM.
  3. SessionNotFoundException
  4. NoSuchWindowException.
  5. NoSuchFrameException.
  6. NoAlertPresentException.
  7. InvalidSelectorException.
  8. ElementNotVisibleException.
  9. ElementNotSelectableException.
  10. TimeoutException

15. Can we mix both implicit wait and explict wait together? If not, why?

Ans: It is not a good practice to mix both implicit and explicit waits together as per selenium official website documentation. Because it may give unpredictable wait times.

Explicit wait can be used when an element takes more than expected time to appear. Explicit wait is applicable for this element only. Where as implicit wait is defined only once in the code it is applicable for whole cycle.

Explicit wait is a conditional wait and it takes priority over implicit wait.

16. What are the different ways to refresh a browser?

navigate.refresh();

navigate.to(“url”);

sendKeys(Keys.F5);

17. How to press ENTER key in selenium.

18. What are the different ways to select a value in dropdown?

selectByValue()

selectByIndex()

selectByVisibleText()

 

19. How can you submit a form in selenium?

Using submit() method. 

webElement.submit();

20. A website is showing an authentication popup asking username and password. How can you login?

We can pass username and password in the url like below.

http://username:password@url

21. Can you write a code to double click an element?

Actions class is used to double click the element.

Actions action = new Actions(driver);

action.doubleClick(webElement);

22. How to navigate back and forth of the page in selenium webdriver?

driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
driver.navigate().to(“URL“);

23. How to get the text of a web element?

getText() method is used to get the text of a web element.

Ex:

String text = driver.findElement(By.id(“userid”)).getText();

24. Is Webdriver class or interface?

WebDriver is an interface and it extends “SearchContext” interface. That means SearchContext is the super interface for WebDriver interface.

25. How to switch to a frame in selenium?

driver.switchTo().frame(By.tagName(“frameid”));

 

Recommended Posts:

Real-time selenium interview questions

How to check if string contains only digits in Java

How to check if string contains only alphabets in Java

Difference between flush and close in Java

How to capitalize the first letter of a String in Java

How to capitalize each word in Java

How to swap two numbers without using third variable in Java

Generate random between 1 to 10 in Java

Generate random between 1 to 5 in Java

.
.
.
Tags: Top 25 selenium interview questions, selenium tutorials, top 25 selenium interview questions for experienced.
Selenium automation testing, mostly asked top 25 selenium interview questions, handle untrusted ssl certificate in selenium.
Most commonly asked top 25 selenium interview questions.
Other links: TestNG
arrays tostring in java | top 25 selenium interview questions
Note: If you find anything incorrect related to “top 25 selenium interview questions”, please reply in the comments section so that we will correct it. Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *