Selenium Interview Question and Answers Part 2
Q 16)When should I use Selenium Grid?
Selenium Grid can be used to execute same or different test scripts on multiple platforms and browsers concurrently so as to achieve distributed test execution, testing under different environments and saving execution time remarkably.
Q 17) What do we mean by Selenium 1 and Selenium 2?
Selenium RC and WebDriver, in a combination are popularly known as Selenium 2. Selenium RC alone is also referred as Selenium 1.
Q 18) Which is the latest Selenium tool?
WebDriver
Q 19) How do I launch the browser using WebDriver?
The following syntax can be used to launch Browser:
WebDriver driver =newFirefoxDriver();
WebDriver driver =newChromeDriver();
WebDriver driver =newInternetExplorerDriver();
Q 20) What are the different types of Drivers available in WebDriver?
The different drivers available in WebDriver are:
- FirefoxDriver
- InternetExplorerDriver
- ChromeDriver
- SafariDriver
- OperaDriver
- AndroidDriver
- IPhoneDriver
- HtmlUnitDriver
Q 21) What are the different types of waits available in WebDriver?
There are two types of waits available in WebDriver:
- Implicit Wait
- Explicit Wait
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.
Q 22) How to type in a textbox using Selenium?
User can use sendKeys(“String to be entered”) to enter the string in the textbox.
Syntax:
WebElement username =drv.findElement(By.id(“Email”));
// entering username
username.sendKeys(“sth”);
Q 23)How can you find if an element in displayed on the screen?
WebDriver facilitates the user with the following methods to check the visibility of the web elements. These web elements can be buttons, drop boxes, checkboxes, radio buttons, labels etc.
- isDisplayed()
- isSelected()
- isEnabled()
Syntax:
isDisplayed():
booleanbuttonPresence = driver.findElement(By.id(“gbqfba”)).isDisplayed();
isSelected():
booleanbuttonSelected = driver.findElement(By.id(“gbqfba”)).isDisplayed();
isEnabled():
booleansearchIconEnabled = driver.findElement(By.id(“gbqfb”)).isEnabled();
Q 24)How can we get a text of a web element?
Get command is used to retrieve the inner text of the specified web element. The command doesn’t require any parameter but returns a string value. It is also one of the extensively used commands for verification of messages, labels, errors etc displayed on the web pages.
Syntax:
String Text = driver.findElement(By.id(“Text”)).getText();
Q 25) How to select value in a dropdown?
Value in the drop down can be selected using WebDriver’s Select class.
Syntax:
selectByValue:
Select selectByValue =newSelect(driver.findElement(By.id(“SelectID_One”)));
selectByValue.selectByValue(“greenvalue”);
selectByVisibleText:
Select selectByVisibleText =newSelect (driver.findElement(By.id(“SelectID_Two”)));
selectByVisibleText.selectByVisibleText(“Lime”);
selectByIndex:
Select selectByIndex =newSelect(driver.findElement(By.id(“SelectID_Three”)));
selectByIndex.selectByIndex(2);
Q 26) What are the different types of navigation commands?
Following are the navigation commands:
navigate().back()– The above command requires no parameters and takes back the user to the previous webpage in the web browser’s history.
Sample code:
driver.navigate().back();
navigate().forward()– This command lets the user to navigate to the next web page with reference to the browser’s history.
Sample code:
driver.navigate().forward();
navigate().refresh()– This command lets the user to refresh the current web page there by reloading all the web elements.
Sample code:
driver.navigate().refresh();
navigate().to()– This command lets the user to launch a new web browser window and navigate to the specified URL.
Sample code:
driver.navigate().to(“https://google.com”);
Q 27) How to click on a hyper link using linkText?
driver.findElement(By.linkText(“Google”)).click();
The command finds the element using link text and then click on that element and thus the user would be re-directed to the corresponding page.
The above mentioned link can also be accessed by using the following command.
driver.findElement(By.partialLinkText(“Goo”)).click();
The above command find 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.
Q 28) How to handle frame in WebDriver?
An inline frame acronym as iframe is used to insert another document with in the current HTML document or simply a web page into a web page by enabling nesting.
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 29) 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 30)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
2 List <WebElement> elementList = driver.findElements(By.xpath("//div[@id='example']//ul//li"));
3 // Fetching the size of the list
4 intlistSize = elementList.size();
5 for(inti=0; i<listSize; i++)
6 {
7 // Clicking on each service provider link
8 serviceProviderLinks.get(i).click();
9 // Navigating back to the previous page that stores link to service providers
10 driver.navigate().back();
11 }
Q #30) What is the difference between driver.close() and driver.quit command?
No comments:
Post a Comment