Selenium Frequently Asked Questions & Answers Part-1

Ques.1. What is Selenium?
Ans. Selenium is a robust test automation suite that is used for automating web based applications. It supports multiple browsers, programming languages and platforms.

Ques.2. What are different forms of selenium?
Ans. Selenium comes in four forms-
Selenium WebDriver - Selenium WebDriver is used to automate web applications using browser's native methods.
Selenium IDE - A Firefox plugin that works on record and play back principle.
Selenium RC - Selenium Remote Control(RC) is officially deprecated by selenium and it used to work on javascript to automate the web applications.
Selenium Grid - Allows selenium tests to run in parallel across multiple machines.

Ques.3. What are some advantages of selenium?
Selenium is open source and free to use without any licensing cost.
It supports multiple languages like Java, ruby, python etc.
It supports multi browser testing.
It has good amount of resources and helping community over the internet.
Using selenium IDE component, non-programmers can also write automation scripts
Using selenium grid component, distributed testing can be carried out on remote machines possible.

Ques.4. What are some limitations of selenium?
We cannot test desktop application using selenium.
We cannot test web services using selenium.
For creating robust scripts in selenium webdriver, programming langauge knowledge is required.
We have to rely on external libraries and tools for performing tasks like - logging(log4J), testing framework-(testNG, JUnit), reading from external files(POI for excels) etc.

Ques.5. Which all browsers/drivers are supported by Selenium Webdriver?
Ans. Some commonly used browsers supported by selenium are-
Google Chrome - ChromeDriver
Firefox - FireFoxDriver
Internet Explorer - InternetExplorerDriver
Safari - SafariDriver
HtmlUnit (Headless browser) - HtmlUnitDriver
Android - Selendroid/Appium
IOS - ios-driver/Appium

Ques.6. Can we test APIs or web services using Selenium webdriver?
Ans. No selenium webdriver uses browser's native method to automate the web applications. Since web services are headless, so we cannot automate web services using selenium webdriver.

Ques.7. What are the testing type supported by Selenium WebDriver?
Ans. Selenium webdriver can be used for performing automated functional and regression testing.

Ques.8. What are various ways of locating an element in selenium?
Ans. The different locators in selenium are-

Ques.9. What is an XPath?
Ans. Xpath or XML path is a query language for selecting nodes from XML documents. XPath is one of the locators supported by selenium webdriver.

Ques.10. What is an absolute XPath?
Ans. An absolute XPath is a way of locating an element using an XML expression beginning from root node i.e. html node in case of web pages. The main disadvantage of absolute xpath is that even with slightest change in the UI or any element the whole absolute XPath fails.
Example - html/body/div/div[2]/div/div/div/div[1]/div/input

Ques.11. What is a relative XPath?
Ans. A relative XPath is a way of locating an element using an XML expression beginning from anywhere in the HTML document. There are different ways of creating relative XPaths which are used for creating robust XPaths (unaffected by changes in other UI elements).
Example - //input[@id='username']

Ques.12. What is the difference between single slash(/) and double slash(//) in XPath?
Ans. In XPath a single slash is used for creating XPaths with absolute paths beginning from root node.
Whereas double slash is used for creating relative XPaths.

Ques.13. Which XPath you will prefer to use? Why?
Normally we prefer to use Relative XPath.
Ralative Xpath can identify element evethou some UI changes happed, but can’t identify by Absolute Xpath.

Ques.14. What is the difference between Absolute XPath and Relative XPath?
Absolute Xpath will traverse entire HTML from the root node /html. 
Relative Xpath directly jump to node based on attribute specified. 

Ques.15. How can we inspect the web element attributes in order to use them in different locators?
Ans. Using Chropath or developer tools we can inspect the specific web elements. Chropath is a plugin that provides xpaths and CSS Selectors. From automation perspective, “Right click on page inspect element” is used specifically for inspecting web-elements in order to use their attributes like id, class, name etc. in different locators.

Ques.16. How can we locate an element by only partially matching its attributes value in Xpath?
Ans. Using contains() method we can locate an element by partially matching its attribute's value. This is particularly helpful in the scenarios where the attributes have dynamic values with certain constant part.
xPath expression = //*[contains(@name,'user')]
The above statement will match the all the values of name attribute containing the word 'user' in them.

Ques.17. How can we locate elements using their text in XPath?
Ans. Using the text() method

Ques.18. How can we move to nth child element using XPath?
Ans. There are two ways of navigating to the nth element using XPath-

Using square brackets with index position
Example - div[2] will find the second div element.

Using position()
Example - div[position()=3] will find the third div element.

Ques.19. What is the syntax of finding elements by class using CSS Selector?
Ans. By .className we can select all the element belonging to a particluar class e.g. '.inputtext ' will select all elements having class ' inputtext '.

Ques.20. What is the syntax of finding elements by id using CSS Selector?
Ans. By #idValue we can select all the elements belonging to a particluar id e.g. ‘#u_0_n' will select the element having id - u_0_n.

Ques.21. How can we select elements by their attribute value using CSS Selector?
Ans. Using [attribute=value] we can select all the element belonging to a particluar attribute e.g. '[type=radio]' will select the element having attribute type of value ‘radio'.

Ques.22. What is fundamental difference between XPath and css selector?
Ans. The fundamental difference between XPath and css selector is using XPaths we can traverse up in the document i.e. we can move to parent elements. Whereas using CSS selector we can only move downwards in the document.

Ques.23. How can we launch different browsers in selenium webdriver?
Ans. By creating an instance of driver of a particular browser-

Ques.24. What is the use of driver.get("URL") and driver.navigate().to("URL") command? Is there any difference between the two?
Ans. Both driver.get("URL") and driver.navigate().to("URL") commands are used to navigate to a URL passed as parameter. There is no difference between the two commands.

Ques.25. How can we type text in a textbox element using selenium?

WebElement searchTextBox = driver.findElement("search")); searchTextBox.sendKeys("searchTerm");