- Created by Phil Gochenour, last modified by Kim Pohas on Feb 14, 2021
PAGE DEPRECATED: updated content available here.
These Appium scripts for Android mobile application tests can help streamline your testing process.
For Example Purposes Only
The code in these scripts is provided on an "AS-IS” basis without warranty of any kind, either express or implied, including without limitation any implied warranties of condition, uninterrupted use, merchantability, fitness for a particular purpose, or non-infringement. Your tests and testing environments may require you to modify these scripts. Issues regarding these scripts should be submitted through GitHub. These scripts are not maintained by Sauce Labs Support.
These examples employ the page object model to run tests on emulators and simulators. Feel free to clone these scripts directly from GitHub, and follow the instructions in the README file.
Page Objects
This object represents a single view/page in the sample application. Click below to see the script:
Not found
Could not read the file appium-example/src/test/java/example/android/Pages/GuineaPigPage.java
Test Objects
These object represent the individual tests, as well as the prerequisite and postrequisite test tasks (TestBase.java). Click on any of the text below to see the scripts:
Not found
Could not read the file appium-example/src/test/java/example/android/Tests/TestBase.java
Not found
Could not read the file appium-example/src/test/java/example/android/Tests/FollowLinkTest.java
Not found
Could not read the file appium-example/src/test/java/example/android/Tests/TextInputTest.java
The example below employs the page object model using WebDriverIO test automation framework to run tests on emulators and simulators. For more Webdriver examples, see Sauce Labs Training on GitHub: Node.js examples repository (be sure to read the follow the README file first).
WebDriverIO Config
This script represents the prerequisite and postrequisite test tasks. Click below to see the script:
const {config} = require('./wdio.shared.local.appium.conf'); // ================== // Specify Test Files // ================== config.specs= [ './test/specs/emu-sim/*.js' ]; // ============ // Capabilities // ============ // For all capabilities please check // http://appium.io/docs/en/writing-running-appium/caps/#general-capabilities config.capabilities = [ { // The defaults you need to have in your config automationName: 'UiAutomator2', deviceName: 'Pixel_3_10.0', platformName: 'Android', platformVersion: '10.0', orientation: 'PORTRAIT', // Provide the Google Photos package here but don't start it appPackage: 'com.google.android.apps.photos', appActivity: '.home.HomeActivity', autoLaunch: false, // Read the reset strategies very well, they differ per platform, see // http://appium.io/docs/en/writing-running-appium/other/reset-strategies/ noReset: true, newCommandTimeout: 240, maxInstances: 1, // Always default the language to a language you prefer so you know the app language is always as expected language: 'en', locale: 'en', }, ]; exports.config = config;
Use these Python test frameworks to run tests on real devices, and emulators and simulators.These examples use the pytest test framework to run tests on real devices. Feel free to clone these scripts directly from GitHub, and follow the instructions in the README file.
Python Config
This script initializes the test fixtures, as well as the prerequisite and post-requisite test tasks.
import pytest import os import json import re from appium import webdriver def pytest_addoption(parser): parser.addoption("--dc", action="store", default='us', help="Set Sauce Labs Data Center (US or EU)") @pytest.fixture def data_center(request): return request.config.getoption('--dc') @pytest.fixture def ios_up_driver(request, data_center): caps = { 'username': os.environ['SAUCE_USERNAME'], 'accessKey': os.environ['SAUCE_ACCESS_KEY'], 'deviceName': 'iPhone.*', 'platformName': 'iOS', 'name': request.node.name, 'app': 'storage:filename=iOS.RealDevice.SauceLabs.Mobile.Sample.app.2.2.1.ipa' } if data_center and data_center.lower() == 'eu': sauce_url = "http://ondemand.eu-central-1.saucelabs.com/wd/hub" else: sauce_url = "http://ondemand.us-west-1.saucelabs.com/wd/hub" driver = webdriver.Remote(sauce_url, desired_capabilities=caps) yield driver sauce_result = "failed" if request.node.rep_call.failed else "passed" driver.execute_script("sauce:job-result={}".format(sauce_result)) driver.quit() @pytest.fixture def ios_simulator(request, data_center): caps = { 'username': os.environ['SAUCE_USERNAME'], 'accessKey': os.environ['SAUCE_ACCESS_KEY'], 'deviceName': 'iPhone XS Simulator', 'appiumVersion': '1.17.1', 'platformName': 'iOS', 'platformVersion': "13.4", 'deviceOrientation': "portrait", 'name': request.node.name, 'app': 'storage:filename=iOS.Simulator.SauceLabs.Mobile.Sample.app.2.7.0.zip' } if data_center and data_center.lower() == 'eu': sauce_url = "http://ondemand.eu-central-1.saucelabs.com/wd/hub" else: sauce_url = "http://ondemand.us-west-1.saucelabs.com/wd/hub" driver = webdriver.Remote(sauce_url, desired_capabilities=caps) yield driver sauce_result = "failed" if request.node.rep_call.failed else "passed" driver.execute_script("sauce:job-result={}".format(sauce_result)) driver.quit() @pytest.fixture def ios_to_driver(request, data_center): caps = { 'platformName': 'iOS', 'deviceOrientation':'portrait', 'privateDevicesOnly': False, 'phoneOnly': True } rdc_key = os.environ['TESTOBJECT_SAMPLE_IOS'] caps['testobject_api_key'] = rdc_key test_name = request.node.name caps['name'] = test_name if data_center and data_center.lower() == 'eu': sauce_url = "http://appium.testobject.com/wd/hub" else: sauce_url = "http://us1.appium.testobject.com/wd/hub" driver = webdriver.Remote(sauce_url, desired_capabilities=caps) # This is specifically for SauceLabs plugin. # In case test fails after selenium session creation having this here will help track it down. # creates one file per test non ideal but xdist is awful if driver: print("SauceOnDemandSessionID={} job-name={}\n".format(driver.session_id, test_name)) else: raise WebDriverException("Never created!") yield driver driver.quit() @pytest.fixture def android_to_driver(request, data_center): caps = { 'deviceName': 'Google.*', 'platformName': 'Android', 'deviceOrientation':'portrait', 'privateDevicesOnly': False } rdc_key = os.environ['TESTOBJECT_SAMPLE_ANDROID'] caps['testobject_api_key'] = rdc_key test_name = request.node.name caps['name'] = test_name if data_center and data_center().lower() == 'eu': sauce_url = "http://appium.testobject.com/wd/hub" else: sauce_url = "http://us1.appium.testobject.com/wd/hub" driver = webdriver.Remote(sauce_url, desired_capabilities=caps) # This is specifically for SauceLabs plugin. # In case test fails after selenium session creation having this here will help track it down. # creates one file per test non ideal but xdist is awful if driver: print("SauceOnDemandSessionID={} job-name={}\n".format(driver.session_id, test_name)) else: raise WebDriverException("Never created!") yield driver driver.quit() @pytest.fixture def android_up_driver(request, data_center): caps = { 'username': os.environ['SAUCE_USERNAME'], 'accessKey': os.environ['SAUCE_ACCESS_KEY'], 'deviceName': 'Google.*', 'platformName': 'Android', 'name': request.node.name, 'app': 'storage:filename=Android.SauceLabs.Mobile.Sample.app.2.3.0.apk' } if data_center and data_center.lower() == 'eu': sauce_url = 'http://ondemand.eu-central-1.saucelabs.com/wd/hub' else: sauce_url = 'http://ondemand.us-west-1.saucelabs.com/wd/hub' driver = webdriver.Remote(sauce_url, desired_capabilities=caps) yield driver sauce_result = "failed" if request.node.rep_call.failed else "passed" driver.execute_script("sauce:job-result={}".format(sauce_result)) driver.quit() @pytest.fixture def android_emulator(request, data_center): caps = { 'username': os.environ['SAUCE_USERNAME'], 'accessKey': os.environ['SAUCE_ACCESS_KEY'], 'deviceName': 'Android GoogleAPI Emulator', 'platformName': 'Android', 'platformVersion': '10.0', 'deviceOrientation': 'portrait', 'name': request.node.name, 'appiumVersion': '1.17.1', 'appWaitActivity': 'com.swaglabsmobileapp.MainActivity', 'app': 'storage:filename=Android.SauceLabs.Mobile.Sample.app.2.3.0.apk' } if data_center and data_center.lower() == 'eu': sauce_url = 'http://ondemand.eu-central-1.saucelabs.com/wd/hub' else: sauce_url = 'http://ondemand.us-west-1.saucelabs.com/wd/hub' driver = webdriver.Remote(sauce_url, desired_capabilities=caps) yield driver sauce_result = "failed" if request.node.rep_call.failed else "passed" driver.execute_script("sauce:job-result={}".format(sauce_result)) driver.quit() @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_runtest_makereport(item, call): # this sets the result as a test attribute for Sauce Labs reporting. # execute all other hooks to obtain the report object outcome = yield rep = outcome.get_result() # set an report attribute for each phase of a call, which can # be "setup", "call", "teardown" setattr(item, "rep_" + rep.when, rep)
Test Objects
These scripts represents the individual test. Click below to see the script(s):
import pytest def test_add_to_cart(emusim_driver): emusim_driver.get('https://www.saucedemo.com/v1/inventory.html') emusim_driver.find_element_by_class_name('btn_primary').click() assert emusim_driver.find_element_by_class_name('shopping_cart_badge').text == '1' emusim_driver.get('https://www.saucedemo.com/v1/cart.html') expected = emusim_driver.find_elements_by_class_name('inventory_item_name') assert len(expected) == 1 def test_add_two_to_cart(emusim_driver): emusim_driver.get('https://www.saucedemo.com/v1/inventory.html') emusim_driver.find_element_by_class_name('btn_primary').click() emusim_driver.find_element_by_class_name('btn_primary').click() assert emusim_driver.find_element_by_class_name('shopping_cart_badge').text == '2' emusim_driver.get('https://www.saucedemo.com/v1/cart.html') expected = emusim_driver.find_elements_by_class_name('inventory_item_name') assert len(expected) == 2
import pytest def test_valid_crentials_login(emusim_driver): emusim_driver.get('https://www.saucedemo.com/v1') emusim_driver.find_element_by_id('user-name').send_keys('locked_out_user') emusim_driver.find_element_by_id('password').send_keys('secret_sauce') emusim_driver.find_element_by_css_selector('.btn_action').click() assert emusim_driver.find_element_by_css_selector('.error-button').is_displayed()
import pytest def test_valid_crentials_login(emusim_driver): emusim_driver.get('https://www.saucedemo.com/v1') emusim_driver.find_element_by_id('user-name').send_keys('standard_user') emusim_driver.find_element_by_id('password').send_keys('secret_sauce') emusim_driver.find_element_by_css_selector('.btn_action').click() assert "/inventory.html" in emusim_driver.current_url
Python Config
This script initializes the test fixtures, as well as the prerequisite and post-requisite test tasks.
import pytest import os import json import re from appium import webdriver def pytest_addoption(parser): parser.addoption("--dc", action="store", default='us', help="Set Sauce Labs Data Center (US or EU)") @pytest.fixture def data_center(request): return request.config.getoption('--dc') @pytest.fixture def ios_up_driver(request, data_center): caps = { 'username': os.environ['SAUCE_USERNAME'], 'accessKey': os.environ['SAUCE_ACCESS_KEY'], 'deviceName': 'iPhone.*', 'platformName': 'iOS', 'name': request.node.name, 'app': 'storage:filename=iOS.RealDevice.SauceLabs.Mobile.Sample.app.2.2.1.ipa' } if data_center and data_center.lower() == 'eu': sauce_url = "http://ondemand.eu-central-1.saucelabs.com/wd/hub" else: sauce_url = "http://ondemand.us-west-1.saucelabs.com/wd/hub" driver = webdriver.Remote(sauce_url, desired_capabilities=caps) yield driver sauce_result = "failed" if request.node.rep_call.failed else "passed" driver.execute_script("sauce:job-result={}".format(sauce_result)) driver.quit() @pytest.fixture def ios_simulator(request, data_center): caps = { 'username': os.environ['SAUCE_USERNAME'], 'accessKey': os.environ['SAUCE_ACCESS_KEY'], 'deviceName': 'iPhone XS Simulator', 'appiumVersion': '1.17.1', 'platformName': 'iOS', 'platformVersion': "13.4", 'deviceOrientation': "portrait", 'name': request.node.name, 'app': 'storage:filename=iOS.Simulator.SauceLabs.Mobile.Sample.app.2.7.0.zip' } if data_center and data_center.lower() == 'eu': sauce_url = "http://ondemand.eu-central-1.saucelabs.com/wd/hub" else: sauce_url = "http://ondemand.us-west-1.saucelabs.com/wd/hub" driver = webdriver.Remote(sauce_url, desired_capabilities=caps) yield driver sauce_result = "failed" if request.node.rep_call.failed else "passed" driver.execute_script("sauce:job-result={}".format(sauce_result)) driver.quit() @pytest.fixture def ios_to_driver(request, data_center): caps = { 'platformName': 'iOS', 'deviceOrientation':'portrait', 'privateDevicesOnly': False, 'phoneOnly': True } rdc_key = os.environ['TESTOBJECT_SAMPLE_IOS'] caps['testobject_api_key'] = rdc_key test_name = request.node.name caps['name'] = test_name if data_center and data_center.lower() == 'eu': sauce_url = "http://appium.testobject.com/wd/hub" else: sauce_url = "http://us1.appium.testobject.com/wd/hub" driver = webdriver.Remote(sauce_url, desired_capabilities=caps) # This is specifically for SauceLabs plugin. # In case test fails after selenium session creation having this here will help track it down. # creates one file per test non ideal but xdist is awful if driver: print("SauceOnDemandSessionID={} job-name={}\n".format(driver.session_id, test_name)) else: raise WebDriverException("Never created!") yield driver driver.quit() @pytest.fixture def android_to_driver(request, data_center): caps = { 'deviceName': 'Google.*', 'platformName': 'Android', 'deviceOrientation':'portrait', 'privateDevicesOnly': False } rdc_key = os.environ['TESTOBJECT_SAMPLE_ANDROID'] caps['testobject_api_key'] = rdc_key test_name = request.node.name caps['name'] = test_name if data_center and data_center().lower() == 'eu': sauce_url = "http://appium.testobject.com/wd/hub" else: sauce_url = "http://us1.appium.testobject.com/wd/hub" driver = webdriver.Remote(sauce_url, desired_capabilities=caps) # This is specifically for SauceLabs plugin. # In case test fails after selenium session creation having this here will help track it down. # creates one file per test non ideal but xdist is awful if driver: print("SauceOnDemandSessionID={} job-name={}\n".format(driver.session_id, test_name)) else: raise WebDriverException("Never created!") yield driver driver.quit() @pytest.fixture def android_up_driver(request, data_center): caps = { 'username': os.environ['SAUCE_USERNAME'], 'accessKey': os.environ['SAUCE_ACCESS_KEY'], 'deviceName': 'Google.*', 'platformName': 'Android', 'name': request.node.name, 'app': 'storage:filename=Android.SauceLabs.Mobile.Sample.app.2.3.0.apk' } if data_center and data_center.lower() == 'eu': sauce_url = 'http://ondemand.eu-central-1.saucelabs.com/wd/hub' else: sauce_url = 'http://ondemand.us-west-1.saucelabs.com/wd/hub' driver = webdriver.Remote(sauce_url, desired_capabilities=caps) yield driver sauce_result = "failed" if request.node.rep_call.failed else "passed" driver.execute_script("sauce:job-result={}".format(sauce_result)) driver.quit() @pytest.fixture def android_emulator(request, data_center): caps = { 'username': os.environ['SAUCE_USERNAME'], 'accessKey': os.environ['SAUCE_ACCESS_KEY'], 'deviceName': 'Android GoogleAPI Emulator', 'platformName': 'Android', 'platformVersion': '10.0', 'deviceOrientation': 'portrait', 'name': request.node.name, 'appiumVersion': '1.17.1', 'appWaitActivity': 'com.swaglabsmobileapp.MainActivity', 'app': 'storage:filename=Android.SauceLabs.Mobile.Sample.app.2.3.0.apk' } if data_center and data_center.lower() == 'eu': sauce_url = 'http://ondemand.eu-central-1.saucelabs.com/wd/hub' else: sauce_url = 'http://ondemand.us-west-1.saucelabs.com/wd/hub' driver = webdriver.Remote(sauce_url, desired_capabilities=caps) yield driver sauce_result = "failed" if request.node.rep_call.failed else "passed" driver.execute_script("sauce:job-result={}".format(sauce_result)) driver.quit() @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_runtest_makereport(item, call): # this sets the result as a test attribute for Sauce Labs reporting. # execute all other hooks to obtain the report object outcome = yield rep = outcome.get_result() # set an report attribute for each phase of a call, which can # be "setup", "call", "teardown" setattr(item, "rep_" + rep.when, rep)
Test Objects
These scripts represents the individual tests. Click below to see the script:
def test_blank_credentials(android_up_driver): android_up_driver.find_element_by_accessibility_id("test-Username").send_keys("") android_up_driver.find_element_by_accessibility_id("test-Password").send_keys("") android_up_driver.find_element_by_accessibility_id("test-LOGIN").click() assert android_up_driver.find_element_by_accessibility_id("test-Error message").is_displayed()
def test_standard_user(android_up_driver): android_up_driver.find_element_by_accessibility_id("test-Username").send_keys("standard_user") android_up_driver.find_element_by_accessibility_id("test-Password").send_keys("secret_sauce") android_up_driver.find_element_by_accessibility_id("test-LOGIN").click() assert android_up_driver.find_element_by_accessibility_id("test-PRODUCTS").is_displayed()
These examples employ the page object model and use either the RSpec or Cucumber test frameworks to run tests on emulators and simulators. Feel free to clone these scripts directly from GitHub, and follow the instructions in the README file.
Rakefile
This file initializes the test capabilities, as well as the prerequisite and postrequisite test tasks:
def run_tests(deviceName, platformName, platformVersion, app) system("deviceName=\"#{deviceName}\" platformName=\"#{platformName}\" platformVersion=\"#{platformVersion}\" app=\"#{app}\" parallel_split_test spec") end task :Andoid_Emulator_Phone_5_1 do run_tests('Android Emulator', 'Android', '5.1', 'https://github.com/saucelabs-sample-test-frameworks/Java-Junit-Appium-Android/blob/master/resources/GuineaPigApp-debug.apk?raw=true') end task :Andoid_Emulator_Tablet_5_1 do run_tests('Android Emulator', 'Android', '5.1', 'https://github.com/saucelabs-sample-test-frameworks/Java-Junit-Appium-Android/blob/master/resources/GuineaPigApp-debug.apk?raw=true') end task :Galaxy_S8_Emulator do run_tests('Samsung Galaxy S8 HD GoogleAPI Emulator', 'Android', '7.0', 'https://github.com/saucelabs-sample-test-frameworks/Java-Junit-Appium-Android/blob/master/resources/GuineaPigApp-debug.apk?raw=true') end task :Galaxy_S6_Emulator do run_tests('Samsung Galaxy S6 GoogleAPI Emulator', 'Android', '7.0', 'https://github.com/saucelabs-sample-test-frameworks/Java-Junit-Appium-Android/blob/master/resources/GuineaPigApp-debug.apk?raw=true') end task :iPhone_6_Simulator do run_tests('iPhone 6 Simulator', 'iOS', '10.3', 'https://github.com/saucelabs-training/demo-java/blob/master/appium-example/resources/ios/SauceGuineaPig-sim-debug.app.zip?raw=true') end task :iPhone_7_Simulator do run_tests('iPhone 7 Simulator', 'iOS', '12.0', 'https://github.com/saucelabs-training/demo-java/blob/master/appium-example/resources/ios/SauceGuineaPig-sim-debug.app.zip?raw=true') end task :iPad_Air_Simulator do run_tests('iPad Air Simulator', 'iOS', '11.2', 'https://github.com/saucelabs-training/demo-java/blob/master/appium-example/resources/ios/SauceGuineaPig-sim-debug.app.zip?raw=true') end task :iPad_Simulator do run_tests('iPad (6th generation) Simulator', 'iOS', '12.0', 'https://github.com/saucelabs-training/demo-java/blob/master/appium-example/resources/ios/SauceGuineaPig-sim-debug.app.zip?raw=true') end multitask :test_sauce => [ :Andoid_Emulator_Phone_5_1, :Galaxy_S8_Emulator, :Andoid_Emulator_Tablet_5_1, :Galaxy_S6_Emulator, :iPhone_6_Simulator, :iPhone_7_Simulator, :iPad_Air_Simulator, :iPad_Simulator, ] do puts 'Running automation' end
Page Objects
These scripts represent the individual views/pages of the sample application:
require_relative "../spec/spec_helper" class GuineaPigAppPage attr_accessor :driver def initialize(driver) @driver = driver end # Elements def textInput @driver.find_element(:id, "i_am_a_textbox") end def emailTextInput @driver.find_element(:id, "fbemail") end end
Spec Objects
These scripts represents the individual tests, as well as a Sauce Labs utility helper:
require_relative "../spec/spec_helper" class GuineaPigAppPage attr_accessor :driver def initialize(driver) @driver = driver end # Elements def textInput @driver.find_element(:id, "i_am_a_textbox") end def emailTextInput @driver.find_element(:id, "fbemail") end end
require_relative "../spec/spec_helper" class GuineaPigAppPage attr_accessor :driver def initialize(driver) @driver = driver end # Elements def textInput @driver.find_element(:id, "i_am_a_textbox") end def emailTextInput @driver.find_element(:id, "fbemail") end end
Rakefile
This file initializes the prerequisite and postrequisite test tasks:
def run_tests(deviceName, platformName, platformVersion, app, junit) system("deviceName=\"#{deviceName}\" platformName=\"#{platformName}\" platformVersion=\"#{platformVersion}\" app=\"#{app}\" parallel_cucumber features -n 20") end task :Andoid_Emulator_Phone_5_1 do run_tests('Android Emulator', 'Android', '5.1', 'https://github.com/saucelabs-sample-test-frameworks/Java-Junit-Appium-Android/blob/master/resources/GuineaPigApp-debug.apk?raw=true', 'junit_reports/Andoid_Emulator_Phone_5_1') end task :Andoid_Emulator_Tablet_5_1 do run_tests('Android Emulator', 'Android', '5.1', 'https://github.com/saucelabs-sample-test-frameworks/Java-Junit-Appium-Android/blob/master/resources/GuineaPigApp-debug.apk?raw=true', 'junit_reports/Andoid_Emulator_Tablet_5_1') end task :Galaxy_S8_Emulator do run_tests('Samsung Galaxy S8 HD GoogleAPI Emulator', 'Android', '7.0', 'https://github.com/saucelabs-sample-test-frameworks/Java-Junit-Appium-Android/blob/master/resources/GuineaPigApp-debug.apk?raw=true', 'junit_reports/Galaxy_S8_Emulator') end task :Galaxy_S6_Emulator do run_tests('Samsung Galaxy S6 GoogleAPI Emulator', 'Android', '7.0', 'https://github.com/saucelabs-sample-test-frameworks/Java-Junit-Appium-Android/blob/master/resources/GuineaPigApp-debug.apk?raw=true', 'junit_reports/Galaxy_S6_Emulator') end task :iPhone_6_Simulator do run_tests('iPhone 6 Simulator', 'iOS', '10.3', 'https://github.com/saucelabs-training/demo-java/blob/master/appium-example/resources/ios/SauceGuineaPig-sim-debug.app.zip?raw=true', 'junit_reports/iPhone_6_Simulator') end task :iPhone_7_Simulator do run_tests('iPhone 7 Simulator', 'iOS', '12.0', 'https://github.com/saucelabs-training/demo-java/blob/master/appium-example/resources/ios/SauceGuineaPig-sim-debug.app.zip?raw=true', 'junit_reports/iPhone_5s_Simulator') end task :iPad_Air_Simulator do run_tests('iPad Air Simulator', 'iOS', '11.2', 'https://github.com/saucelabs-training/demo-java/blob/master/appium-example/resources/ios/SauceGuineaPig-sim-debug.app.zip?raw=true', 'junit_reports/iPad_Air_Simulator') end task :iPad_Simulator do run_tests('iPad (6th generation) Simulator', 'iOS', '12.0', 'https://github.com/saucelabs-training/demo-java/blob/master/appium-example/resources/ios/SauceGuineaPig-sim-debug.app.zip?raw=true', 'junit_reports/iPad_Simulator') end multitask :test_sauce => [ :Andoid_Emulator_Phone_5_1, :Galaxy_S8_Emulator, :Andoid_Emulator_Tablet_5_1, :Galaxy_S6_Emulator, :iPhone_6_Simulator, :iPhone_7_Simulator, :iPad_Air_Simulator, :iPad_Simulator, ] do puts 'Running automation' end
Environment Object
This script pulls the test capabilities for each device combo from the rake tasks; it also initializes the remote web driver object:
require 'appium_lib' require 'sauce_whisk' require 'rspec' Before do | scenario | # need to configure env variables for browser caps = { caps: { platformVersion: "#{ENV['platformVersion']}", deviceName: "#{ENV['deviceName']}", platformName: "#{ENV['platformName']}", app: "#{ENV['app']}", deviceOrientation: 'portrait', name: "#{scenario.feature.name} - #{scenario.name}", appiumVersion: '1.9.1', browserName: '', build: 'Appium-Ruby-Cucumber EmuSim Examples' } } @driver = Appium::Driver.new(caps, true) @driver.start_driver end # "after all" After do | scenario | sessionid = @driver.session_id jobname = "#{scenario.feature.name} - #{scenario.name}" puts "SauceOnDemandSessionID=#{sessionid} job-name=#{jobname}" @driver.driver_quit if scenario.passed? SauceWhisk::Jobs.pass_job sessionid else SauceWhisk::Jobs.fail_job sessionid end end
Test Features
These scripts represents the individual behavior scenarios that we define tests for in our step definitions:
Feature: Sample Ruby Cucumber Comment Test Scenario: Add a Comment Given I click on the comment box When I enter a comment Then I click the send button
Feature: Sample Ruby Cucumber Email Test Scenario: Enter an Email Address Given I click on the email box When I enter my email address Then I click the submit button
Test Step Definitions
These scripts define the specific steps our tests run in order to achieve the desired results from the test features:
Given /^I click on the comment box$/ do comment_input = @driver.find_element(:id, "comments") comment_input.click() end When /^I enter a comment$/ do comment_text = @driver.find_element(:id, "comments") comment_text.send_keys("My Exceptionally Eloquent Comment") end Then /^I click the send button$/ do submit_button = @driver.find_element(:id, "submit") submit_button.click() end
Given /^I click on the email box$/ do email_input = @driver.find_element(:id, "fbemail") email_input.click() end When /^I enter my email address$/ do email_text = @driver.find_element(:id, "fbemail") email_text.send_keys("example@email.com") end Then /^I click the submit button$/ do submit_button = @driver.find_element(:id, "submit") submit_button.click() end