Running Mobile Web Tests with Selenium WebDriver
Many of you have been interested in learning how to create a very simple Selenium/Appium script for the mobile web test since the Basics of Mobile Web Testing blog attracted a lot of attention. Here, I'll give you a quick tutorial on how to create a straightforward test for your website using actual hardware and browsers. Let's begin with the script that follows the example below.
I will be using Python, If you don't already have Python installed and configured, get it done as we will be using it only.
The imports must be completed first, followed by the creation of a simple function for log output.
import os
import sys
import time
from time import sleep
from selenium import webdriver
##
## More help on writing tests:
## => http://selenium-python.readthedocs.org/latest/navigating.html
##
def log(msg):
print (time.strftime("%H:%M:%S") + ": " + msg)
Establish the URL for device access as the first actual step. Use the following URL to access Bitbar Testing's mobile (Android and iOS) devices:
appium_URL = 'http://appium.testdroid.com/wd/hub'
Next, specify the local folder where you want to save all of your screenshots:
screenshot_path = '/Users/vhelppi/Documents/Appium-Samples/Screenshots/Chrome';
Examining the desired capabilities now. Your login information, used target (such as the web browser you want to use for the session), project name and test run (i.e., what you see under your Bitbar Testing projects), the device(s) that will be utilised during the session, platform, and browser are all described in these. Follow the instructions carefully when setting the desired capabilities because things can go wrong here very rapidly. Additionally, if possible, please use proper device names. These can be retrieved from our Device listing, and the names defined here and in the cloud must correspond exactly:
These can be retrieved from our Device listing, and the names defined here and in the cloud must correspond exactly:
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'
Let's now start the Selenium driver by utilising the library's Remote() method. The Appium URL (referring to Bitbar Testing) and Desired Capabilities are provided as parameters. Of course, it's a good idea to send some status data to the logs/screen.
log ("WebDriver request initiated. Waiting for response, this may take a while.")
driver = webdriver.Remote(appium_URL, desired_caps)
Whenever possible, it is also a good idea to capture screenshots. Though more is undoubtedly better, attempt to limit the quantity of screenshots you use. We will snap a screenshot of the web browser (plain view, nothing has yet showed in the browser) before we begin the session with any browser or URL.
log ("Taking screenshot of home page: '0_chromeLaunched.png'")
driver.save_screenshot(screenshot_path + "/0_chromeLaunched.png")
Describe your website now. I conduct this test using our Bitbar Testing page and capture a screenshot of it as follows:
log ("Loading page")
driver.get("https://bitbar.com/testing")
log ("Taking screenshot of home page: '1_home.png'")
driver.save_screenshot(screenshot_path + "/1_home.png")
Finding the "Products" button on the website is the next task for the script. Its definition is "Menu" and "1" (the "Why Bitbar?" button is button 0). The syntax for calling the find element by xpath function is as follows:
log ("Finding 'Products'")
elem = driver.find_element_by_xpath('//*[@id="menu"]/ul/li[1]/a')
log ("Clicking 'Products'")
elem.click()
Keep in mind that Selenium offers a wide variety of find element function calls. You can look up elements, for instance, using their names, XPaths, or IDs.
The script locates the "Learn More" button from the page after hitting the "Products" menu item in the top-left corner of the screen. Searching by name wouldn't be helpful in this case because there are numerous "Learn More" buttons on this page, as you can see. Again, I'll use XPath as an example. You may find this by looking at the page source code at www.bitbar.com/products -> inspect element:
Now, based on the implementation, the script finds and clicks the appropriate button that says "Learn more":
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()
Cool. Now that we've examined a few HTML components, we've clicked on them to snap some screenshots. We'll locate "topMenu" and recognise the "Supported Frameworks" button in it in the following few lines. The page's element inspection appears as follows:
Again, in the interest of using XPath, the code for finding the appropriate link is as follows:
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()
driver.save_screenshot(screenshot_path + "/4_supportedframeworks.png")
log ("quitting")
driver.quit()
sys.exit()


Comments
Post a Comment