[go: up one dir, main page]

0% found this document useful (0 votes)
84 views3 pages

Selenium Automation Q&A

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

Advanced Level Selenium Interview Questions:-

1. Is there a way to type in a textbox without using sendKeys()?


you can use Javascript Executer to input text into a text box without using sendKeys() method:
// Initialize JS object
JavascriptExecutor JS = (JavascriptExecutor)webdriver;
// Enter username
JS.executeScript("document.getElementById('User').value='Abha_Rathour'");
// Enter password
JS.executeScript("document.getElementById('Password').value='password123'");

2. How to select a value from a dropdown in Selenium WebDriver?


Select select = new Select(driver.findElement(By.id("abcd")));
select.selectByVisibleText()/deselectByVisibleText(); - selects/deselects an option by its displayed text
select.selectByValue()/deselectByValue(); - selects/deselects an option by the value of its "value" attribute
select.selectByIndex()/deselectByIndex(); - selects/deselects an option by its index
select.isMultiple(); - returns TRUE if the drop-down element allows multiple selection at a time; FALSE if
otherwise
select.deselectAll(); - deselects all previously selected options

3. What does the switchTo() command do?


It is used to switch to that browser window/frame/Alert
-driver.switchTo().window("windowName");
-driver.switchTo().frame("frameName");
-Alert alert = driver.switchTo().alert();

4. How to upload a file in Selenium WebDriver?


Uploading files in WebDriver is done by simply using the sendKeys() method on the file-select input field to enter
the path to the file to be uploaded.

WebElement uploadElement = driver.findElement(By.id("uploadfile_0"));


// enter the file path onto the file-selection input field
uploadElement.sendKeys("C:\\newhtml.html");
// click the "UploadFile" button
driver.findElement(By.name("send")).click();

5. How to set browser window size in Selenium?


Selenium WebDriver allows resizing and maximizing window natively from its API. We use 'Dimension' class to
resize the window.
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://google.co.in");
System.out.println(driver.manage().window().getSize());
Dimension d = new Dimension(420,600);
//Resize the current window to the given dimension
driver.manage().window().setSize(d);

6. When do we use findElement() and findElements()?


findElement():
syntax: WebElement elementName = driver.findElement(By.LocatorStrategy("LocatorValue"));
Returns the first most web element if there are multiple web elements found with the same locator
Throws exception NoSuchElementException if there are no elements matching the locator strategy
It will only find one web element
Indexing is not Applicable

findElements();
syntax: List<WebElement> elementName = driver.findElements(By.LocatorStrategy("LocatorValue"));
Returns a list of web elements
Returns an empty list if there are no web elements matching the locator strategy
It will find a collection of elements whose match the locator strategy.
Each Web element is indexed with a number starting from 0 just like an array

7. What is a pause on an exception in Selenium IDE?


pause(waitTime): Wait for the specified amount of time in milliseconds.
1. Thread.sleep(1000);
2. webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
3. WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ID")));

8. What is the difference between single and double slash in Xpath?


A double slash " // " means any descendant node of the current node in the HTML tree which matches the locator. //
->Selects nodes in the document from the current node that match the selection no matter where they are
A single slash " / " means a node which is a direct child of the current. / -> Selects from the root node

Eg:
1. /bookstore -> Selects the root element bookstore
2. bookstore/book -> Selects all book elements that are children of bookstore
3. //book -> Selects all book elements no matter where they are in the document
4. bookstore//book -> Selects all book elements that are descendant of the bookstore element, no matter where they
are under the bookstore element
5. //@lang -> Selects all attributes that are named lang

9. How do you find broken links in Selenium WebDriver?

Collect all the links in the web page based on <a> tag.
Send HTTP request for the link and read HTTP response code.
Find out whether the link is valid or broken based on HTTP response code.
Repeat this for all the links captured.
10. How to login to any site if it is showing an Authentication Pop-Up for Username and Password?

Approach 1: Handling Authentication/Login Popup Window using Selenium WebDriver


By passing user credentials in URL. Its simple, append your username and password with the URL.
e.g., http://Username:Password@SiteURL
http://rajkumar:myPassword@www.softwaretestingmaterial.com

here, Username is rajkumar


Password is myPassword
SiteURL is www.softwaretestingmaterial.com

Sample code:
String URL = "http://" + rajkumar + ":" + myPassword + "@" + www.softwaretestingmaterial.com;
driver.get(URL);
Alert alert = driver.switchTo().alert();
alert.accept();

Approach 2: Handling Authentication/Login Popup Window using Selenium WebDriver


By using AutoIt, we could handle authentication pop up.
Sample AutoIT Script:
WinWaitActivate("Authentication Required","")
Send("rajkumar{TAB}myPassword{ENTER}")

Sample Java Code:


public static void login(String email, String password) throws Exception{
driver.get(URL);
//Passing the AutoIt Script here
Runtime.getRuntime().exec("D:\\Selenium\\workspace\\AutoItFiles\\ExecutableFiles\\FirefoxBrowser.exe");
driver.findElement
loginpage.setEmail(email);
loginpage.setPassword(password);
loginpage.clickOnLogin();
}

Approach 3: Handling Authentication/Login Popup Window using Selenium WebDriver


By using Alerts in Selenium, we could handle authentication pop up.
driver.switchTo().alert();
//Selenium-WebDriver Java Code for entering Username & Password as below:
driver.findElement(By.id("userID")).sendKeys("userName");
driver.findElement(By.id("password")).sendKeys("myPassword");
driver.switchTo().alert().accept();
driver.switchTo().defaultContent();
}

You might also like