We're Improving Our Documentation!

Sauce Labs is in the process of updating our documentation site! Take a sneak peek at the new site!

The Sauce Labs Cookbook

Sauce Headless

Front End Performance Testing

Insights

External Resources

More Info


Page tree
Skip to end of metadata
Go to start of metadata

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:

 GuineaPigPage.java

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:

 TestBase.java

Not found

Could not read the file appium-example/src/test/java/example/android/Tests/TestBase.java

 FollowLinkTest.java

Not found

Could not read the file appium-example/src/test/java/example/android/Tests/FollowLinkTest.java

 TextInputTest.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:

 wdio.conf.js

Not found

Could not find the given branch refs/remotes/origin/master

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.

 conftest.py

Not found

Could not find the given branch refs/remotes/origin/master

Test Objects

These scripts represents the individual test. Click below to see the script(s):

 test_add_to_cart.py

Not found

Could not find the given branch refs/remotes/origin/master

 test_login_fail.py

Not found

Could not find the given branch refs/remotes/origin/master

 test_login_success.py

Not found

Could not find the given branch refs/remotes/origin/master

Python Config

This script initializes the test fixtures, as well as the prerequisite and post-requisite test tasks.

 conftest.py

Not found

Could not find the given branch refs/remotes/origin/master

Test Objects

These scripts represents the individual tests. Click below to see the script:

 test_invalid_login.py

Not found

Could not find the given branch refs/remotes/origin/master

 test_valid_login.py

Not found

Could not find the given branch refs/remotes/origin/master



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.

 Click here for the RSpec example...

Rakefile

This file initializes the test capabilities, as well as the prerequisite and postrequisite test tasks:

 Rakefile

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:

 app_object.rb

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:

 comment_test_spec.rb

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_helper.rb

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

 Click here for the Cucumber example...

Rakefile

This file initializes the prerequisite and postrequisite test tasks:

 Rakefile

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:

 env.rb

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:

 comment.feature

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

 email.feature

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:

 comment_steps.rb

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

 email_steps.rb

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