Selenium

Upload a File Using Selenium WebDriver

Web application testing is not only about the on-screen functionality but also involves interacting with external resources, such as images, documents and other media.

To upload a file using Selenium, the file path is needed to be set in the input element control before submitting the form.

On the form the control to inspect should look like the image below.

Browse... No files selected

When the folder button is clicked the folder dialog is displayed by the browser.

File Upload dialog

The HTML input element should look similar to the code below.
Take note of the type="file" attribute.

<input id="fileUpload" type="file">Browse.
• • •

Get the Parent Node of the Current Selenium WebElement

There are times that the parent is required from the child element.

There are two options JavaScript or XPath:

WebElement myElement = driver  
        .findElement(By.id("myDiv"));
WebElement parent = (WebElement) ((JavascriptExecutor) driver)  
        .executeScript(
        "return arguments[0].parentNode;", myElement);
WebElement myElement = driver.findElement(By.id("myDiv"));  
WebElement parent = myElement.findElement(By.xpath(".."));  

But what if the parent of the parent is required?

The JavaScript would be

return arguments[0].parentNode.parentNode;  

and the XPath will be

By.xpath("../..")  
• • •

Switch Windows Using Selenium WebDriver

Some web applications open a new window in order to section specific data entry or view.

Selenium can switch windows via the window handle.

The following is an example and must be configured for a specific website under test.

// Store the current window handle
String winHandleBefore = driver.getWindowHandle();

// Perform the click operation that opens new window
driver.findElement(By.LinkText("Open in New Window"));

// Switch to new window opened
for(String winHandle : driver.getWindowHandles()){  
    driver.switchTo().window(winHandle);
}

// Perform the actions on new window
// Do something here...

// Close the new window, if that window no more required
driver.close();

// Switch
• • •

Selenium ChromeDriver for Android

Selenium is well known with automation testing of web applications, but a lot of businesses are moving their attention away from the desktop and onto the mobile platform.

The Selenium ChromeDriver allows Selenium tests to be run in a Chrome browser. The best part, is that the Chrome browser on Android devices are compatible.

The following is a quick start guide to allow Selenium tests to be executed on an Android device with Chrome.

Requirements

1. On the

• • •