Selenium Interview Questions Flashcards
What is Selenium? Why do you prefer the Selenium Automation Tool? What are the testing types that can be supported by Selenium? Limitations of Selenium?
Selenium is a suite of software tools used to automate Web Browsers.
Prefer to Use Selenium Tool for the following:
● It is an Open source suite of tools mainly used for Functional and Regression Test Automation.
● Selenium supports various Operating environments:
MS Windows,Linux,Macintosh etc…
● Selenium supports various Browsers:
Mozilla Firefox,IE,Google Chrome,Safari,Opera etc…
● Selenium supports various programming environments to write programs (Test scripts):
Java,C#,Python,Perl,Ruby,PHP, JS
Scenarios we cannot automate using Selenium WebDriver:
● Automating Captcha is not possible
● We can not read barcode using Selenium WebDriver
● Windows based pop ups
● Image validation and comparison
● PDF validation and comparison
What is Selenium IDE? What is Selenium RC? What is Selenium Grid? When do you use Selenium Grid? What are the advantages of Selenium Grid? Which version of Selenium Webdriver are you using?
Selenium IDE AKA Selenium Integrated Development Environment is a Firefox plugin.
Selenium RC AKA Selenium Remote control / Selenium 1.
Selenium WebDriver AKA Selenium 2 is a browser automation framework that accepts commands and sends them to a browser. It is implemented through a browser-specific driver. It controls the browser by directly communicating with it.
Selenium Grid is a tool used to distribute your test execution on multiple platforms and environments concurrently.
Usage or Advantage of Selenium Grid:
● It allows running test cases in parallel thereby saving test execution time.
● It allows multi-browser testing
● It allows us to execute test cases on multi-platform
We are using the Selenium 3.0 because of compatibility issues which can support most of the latest browsers any way we are creating maven project any dependency we can easily update in pom.xml file.
What are the types of WebDriver APIs available in Selenium? What is the super interface of WebDriver? What is a headless browser?
Different webdriver API: ● Gecko Driver ● InternetExplorer Driver ● Chrome Driver ● Safari Driver ● HTMLUnit Driver
The Super interface of WebDriver is SearchContext Interface.
HTMLUnitDriver, because it is headless browser which have less User Interface.
What are the Locators available in Selenium? Which locator do you prefer?
There are eight locators in selenium to identify the webelements on the webpage:
ID, ClassName, Name, TagName, LinkText, PartialLinkText, XPath, CSS Selector.
The preference of the locator depends on the project.
What is an XPath? What is the difference between Absolute Path and Relative Path? What is the difference between “/” and “//”? In which situations are you going to use Xpath?
XPath is used to locate the elements. Using XPath, we could navigate through elements and attributes in an XML document to locate web elements.
● Single Slash “/” – Single slash is used to create XPath with absolute path i.e. the XPath would be created to start selection from the document node/start node and it is called Absolute XPath.
Example : /html/body/td/tr/div[1]/div[2]
● Double Slash “//” – Double slash is used to create XPath with relative path i.e. the XPath would be created to start selection from anywhere within the document. And it is called Relative XPath.
Example: //input[@id=’username’]
Write a complex xpath or css expression? Which one is better? What is the difference?
xpath:
“//label[@for=’personal_txtLicExpDate’]/following-sibling::img”
css:
“input[name^=’pass’]”
Both CSS and XPATH are great in Selenium Automation
When we want to automate the test Case on Internet Explorer sometimes CssSelector is better and for the rest (Browser i.e. Firefox, Chrome and Safari) xpath is better
Difference between both Xpath and CSS:
● Generally CSS is easy to use and readable over XPATH.
● CSS works only in forward direction while with xpath, we can search elements backward or forward
● Xpath can work with text while CSS cannot
Speed wise CSS and XPATH can be equal, or either one would be a bit speedier than the other. Therefore speed comparison can be ignored. However, off the record, I have encountered that CSS is much faster than XPATH in IE Browser.
How to launch a browser using Selenium WebDriver? Explain the line of code
// set the property for the driver, specify its location via the webdriver.chrome.driver System.setProperty("webdriver.chrome.driver", driverPath+"chromedriver.exe");
// instantiate an instance of ChromeDriver, which will be driving our browser: public static WebDriver driver = new ChromeDriver();
// navigate to the specific url driver.get("http://google.com");
What is the alternative to driver.get() method to open a URL using Selenium WebDriver? What is the difference?
driver.navigate().to(“http://www.example.com”);
The navigate interface also exposes the ability to move backwards and forwards in our browser’s history:
driver. navigate().forward();
driver. navigate().back();
What is the difference between driver.close() and driver.quit() methods?
close() - It is used to close the browser or page currently which is in focus.
quit() - It is used to shut down the webdriver instance or destroy the web driver
instance (close all the windows).
How to get the text of a web element? How to get an attribute value using Selenium WebDriver?
String buttonText = driver.findElement(By.cssSelector(“div.success”)).getText();
String innerText = driver.findElement(By.cssSelector(“div.success”)).getAttribute(“type”);
What is the difference between driver.findElement() and driver.findElements() commands? What is the return type of findElements?
findElement() will return only a single WebElement and if that element is not located or we use some wrong selector then it will throw NoSuchElementexception.
findElements() will return List of WebElements – It returns an empty list, when
element is not found on the current page as per the given element locator mechanism. It doesn’t throws NoSuchElementException.
How do you work with radio buttons which do not have an id attribute?
We can use click() ,if “id” is not present we can use xpath (i.e you can use relative or absolute), CSS or other locator type.
Also when there is a group of Radio Buttons/Check Boxes on the page then, it is possible that their names are the same, but values are different. In that case we can use the Webdriver findElements command to get the list of web elements and then loop through them.
List radioBtn=driver.findElements(By.name(“sex”));
for(WebElement radio: radioBtn) {
String value=radio.getAttribute(“value”);
if(radio.isEnabled() && value.equals(“Female”)) {
radio.click();
break;
}
}
How to select a value in a dropdown? How to check the multiple selected value in a dropdown?
We can use Select class
Select dropdown = new Select(driver.findElement(By.id(“designation”)));
● dropdown.selectByVisibleText(“Value”);
● dropdown.selectByIndex(1);
● dropdown.selectByValue(“value”);
isMultiple() Method
isMultiple() method is useful to verify if the targeted select box is multiple select box or not means we can select multiple options from that select box or not. It will return boolean (true or false) value. We can use it with if condition before working with select box.
Select listbox = new Select(driver.findElement(By.xpath(“//select[@id=’FromLB’]”)));
if (blistbox.isMultiple()){
listbox.selectByVisibleText(“Value”);
listbox.selectByVisibleText(“Value”);
}
How do you handle an element in different windows? Getwindowhandle vs Getwindowhandles and the return types. Write a method to switch to the window based on title?
We can handle multiple windows in selenium webdriver using Switch To methods which will allow us to switch control from one window to another window. When we open the browser window and then 3 tabs pop open, selenium is focused on the first window.
getWindowHandle() will get the handle of the page the webDriver is currently controlling. Everytime Selenium opens a browser, it’s going to give an index ID for the page called handle. This handle is a unique identifier for the web page. This is different every time you open a page even if it is the same URL.
String handle= driver.getWindowHandle();
getWindowHandles() method or commands returns all handles from all opened browsers by Selenium WebDriver during execution
Set handle= driver.getWindowHandles();//Return a set of window handle
You can use SwitchTo().Window(“handle”) to switch to the window you desire.
You can use SwitchTo().Window(“mywindowID”), if you know the window ID.
SwitchTo().Window(“”) will always go back to the base/main window.
Method to switch to window based on title
public switchToWindow(String titleName) { driver.switchTo().window("titleName"); //titleName =windowName }
How would you handle elements that are in a different frame? How to identify the frame which does not have Id as well as name?
Using switch commands we can switch to different frame to handle elements:
We can switch to the frame 3 different ways:
- Switch to Frame by Name or ID
driver. switchTo().frame(“iframe1”) - Switch to Frame by WebElement
WebElement iframeElement = driver.findElement(By.id(“IF1”));
driver.switchTo().frame(iframeElement); - Switch to Frames by Index
driver. switchTo().frame(0)
How can we handle web based pop-up? How can you handle JavaScript Alerts? Can we inspect an alert? How many ways can you handle alerts? How do you get text from alert? How do you send text to alert? How can we handle windows based pop up?
We can handle web based pop-ups using Alert Interface.
There are four methods that we would be using along with the Alert interface.
Alert alert = driver.switchTo().alert(); or driver.switchTo().alert()
1) void dismiss() – The dismiss() method clicks on the “Cancel” button as soon as the pop up window appears.
alert. dismiss(); or driver.switchTo().alert().dismiss();
2) void accept() – The accept() method clicks on the “Ok” button as soon as the pop up window appears.
alert. accept(); or driver.switchTo().alert().accept();
3) String getText() – The getText() method returns the text displayed on the alert box.
alert. getText(); or driver.switchTo().alert().getText();
4) void sendKeys(String stringToSend) – The sendKeys() method enters the specified string pattern into the alert box.
alert. sendkeys(“text”); or driver.switchTo().alert().sendkeys(“text”);
Selenium WebDriver cannot handle window based pop ups, for that we can use Robot Class or third-party tools like AutoIT.