Using Selenium WebDriver to Run Mobile Web Tests

Using WebDriver to Run Appium Selenium Tests

Dear Testdroiders,

The Basics of Mobile Web Testing blog sparked some serious interest and many of you have been curious how to build a very basic script for the mobile web test using Selenium/Appium. I’ll walk you through here with the brief example how to build a simple test for your website, using real devices and real browsers. The script below follows the video example so let’s start with that one.

Download FREE Guide to Learn How to Succeed in Mobile Web Testing.

I’m using Python here so if you don’t have Python installed and configured, check this out first.

The first thing to do is to do the imports and write a basic function for log outputting.

import os
import sys
import time
from time import sleep
from selenium import webdriver

##
## More help on writing tests:
## => http://selenium-python.readthedocs.org/en/latest/navigating.html
##

def log(msg):
    print (time.strftime("%H:%M:%S") + ": " + msg)

As a first real step, define the URL for device access. To access Testdroid Cloud’s devices (both Android and iOS) use the following URL:

appium_URL = 'http://appium.testdroid.com/wd/hub';

Then, define your local screenshot path where all taken screenshots will be stored:

screenshot_path = '/Users/vhelppi/Documents/Appium-Samples/Screenshots/Chrome';

Let’s take a look at Desired Capabilities. These describe your login credentials, used target (e.g. which web browser do you want to use for the session), project name and test run (by definition what you see under your Testdroid Cloud projects), what device(s) will be used for the session, platform and browser. Make sure you follow the instructions with setting proper desired capabilities – things can quickly go wrong here. Also, please make sure you use correct device names. These can be fetched from Testdroid Cloud device listing and name defined here must map identically to the one in cloud:

desired_caps = {}
desired_caps['testdroid_username'] = ‘ville-veikko.helppi@bitbar.com’
desired_caps['testdroid_password'] = 'xxxxxxxx'
desired_caps['testdroid_target'] = 'chrome'
desired_caps['testdroid_project'] = 'Appium Chrome'
desired_caps['testdroid_testrun'] = 'TestRun 1'
desired_caps['testdroid_device'] = 'Asus Google Nexus 7 (2013) ME571KL'
desired_caps['platformName'] = 'android'
desired_caps['deviceName'] = 'AndroidDevice'
desired_caps['browserName'] = 'chrome'

Now, let’s initiate the Selenium driver by calling Remote() from Selenium library. As a parameter the Appium URL (pointing to Testdroid Cloud) and Desired Capabilities are given. And naturally, it’s always a good idea to output some status information to logs/screen.

log ("WebDriver request initiated. Waiting for response, this may take a while.")
driver = webdriver.Remote(appium_URL, desired_caps)

It is also a good idea to take screenshots when ever appropriate. More is surely better, but try to keep number of those manageable. Before we start the session with any browser or URL we’ll take a screenshot of web browser (plain view, nothing shown yet in the browser):

log ("Taking screenshot of home page: '0_chromeLaunched.png'")
driver.save_screenshot(screenshot_path + "/0_chromeLaunched.png")

Now, specify your website here. I use our Testdroid Community page for this test and take a screenshot of it:

log ("Loading page")
driver.get("http://bitbar.com/testing")
log ("Taking screenshot of home page: '1_home.png'")
driver.save_screenshot(screenshot_path + "/1_home.png")

The next thing for the script is to find an element – “Products” button on Testdroid Community page. It’s defined as “Menu” and button number “1” (“0” is the “Why Testdroid?”). The syntax given to find_element_by_xpath function call looks like this:

log ("Finding 'Products'")
elem = driver.find_element_by_xpath('//*[@id="menu"]/ul/li[1]/a')
log ("Clicking 'Products'")
elem.click()

Remember that there are a great set of different find_element function calls available in Selenium. For example, you can search elements by id, name, xpath, and many others.

After clicking the “Products” menu item on top of the screen the script takes a screenshot and locates “Learn More” button from the page. As you see, there are several “Learn More” buttons on this page, so searching by name wouldn’t help here. For sake of example, I’ll use xpath again. If you look at the page source code (www.testdroid.com/products -> inspect element) you’ll find this:

Element Inspection on Testdroid.com

Now, based on the implementation, the script locates the right button with “Learn more” text and clicks it:

log ("Taking screenshot of 'Products' page: '2_products.png'")
driver.save_screenshot(screenshot_path + "/2_products.png")
log ("Finding 'Learn More'")
elem = driver.find_element_by_xpath('//*[@id="products"]/div[1]/div/div[1]/div[3]/a')
log ("Clicking 'Learn More'")
elem.click()

Allright. Now we’ve inspected some HTML elements, clicked those and some screenshots are taken. In the next few lines, we’ll find “topMenu” and identify “Supported Frameworks” button in that. The element inspection of the page looks like this:

Supported Frameworks by Testdroid link

And again, for sake of using xpath, the code for locating the right link looks like this:

log ("Taking screenshot of 'Learn More' page: '3_learnmore.png'")
driver.save_screenshot(screenshot_path + "/3_learnmore.png")
log ("Finding 'Supported Frameworks'")
elem = driver.find_element_by_xpath('//*[@id="topMenu"]/div[1]/div/a[2]')
log ("Clicking 'Supported Frameworks'")
elem.click()

Now we’ve landed to the page where supported test automation frameworks by different Testdroid products are shown.

log ("Taking screenshot of 'Supported Framworks' page: '4_supportedframeworks.png'")
driver.save_screenshot(screenshot_path + "/4_supportedframeworks.png")
log ("quitting")
driver.quit()
sys.exit()

If you want to try out this, you can download this Python script here.

Happy Web Testing!


The Ultimate Hands-on Guide to Successful Mobile Web Testing

Learn how to do mobile web testing more efficiently and effectively and reap the benefits of it.

Download

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close