Skip to content

Java example how to run Appium tests on Sauce real devices and virtual devices

Notifications You must be signed in to change notification settings

eyaly/sauce-java-appium-cross-platform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mobile testing w/ Appium + Java

Mobile automated testing using Appium and Java

Setup

Sauce Labs setup

  1. Free Sauce account
  2. Make sure you know how to find your Sauce Labs Username and Access Key by going to the Sauce Labs user settings page

Appium setup

  1. We will run our automated tests on Sauce Labs devices; therefore, there is no need to install Appium Server.

Demo app(s)

  1. The Android demo apps that has been used for the Android tests can be found here.
  2. The iOS demo apps that has been used for the iOS tests can be found here.
    Be aware of the fact that and iOS simulator uses a different build then a iOS real device. So please check the file you download.

The advice is to download the files to an apps folder in the root of this folder.

Make sure that when you downloaded the files from the releases page, that you rename the apps to the following:

  • mda-{#.#.#-#}.apk => my-demo-app-android.apk
  • SauceLabs-Demo-App.ipa => SauceLabs-Demo-App.ipa
  • Saucelabs-demo-app.simulator.zip => SauceLabs-Demo-App.Simulator.zip

If you don't do that then the scripts can't find the apps!

Upload apps to Sauce Storage

If you want to use Android emulators, Android real devices, iOS simulators or iOS real devices in the Sauce Labs platform, you need to upload the apps to the Sauce Storage.

Manual upload

Execute the following steps to manually upload the apps:

  • Login to the Sauce Labs platform
  • Go to LIVE > Mobile App
  • Click on App Upload and OR select the folder, OR drag the apps to the screen to upload them

Automated upload

You can find a script to upload them to, OR the US, OR EU DC in this-file. You can push the files to the storage by doing the following from the folder appium-app-examples:

cd src/test/java/sauce/demo/helpers/
./push_apps_to_storage.sh

Gitpod setup

ℹ Gitpod lets you run an entire Dev environment from a browser! You can use this approach if you don't have time or you don't know how to setup a local Java environment.

  1. Sign up for a free GitHub account
  2. Fork this repository
  1. Once the Gitpod.io URL is loaded, you will need to sign in with the GitHub account you created earlier
  2. Once the development environment is loaded, you should see 'Ready to test!' in the Terminal window in the lower portion of the window, run the following commands in that Terminal to set your SAUCE_USERNAME, SAUCE_ACCESS_KEY:
eval $(gp env -e SAUCE_USERNAME=<sauce_username>)
eval $(gp env -e SAUCE_ACCESS_KEY=<sauce_access_key>)

Replace <sauce_username>, <sauce_access_key> with your credentials

Once you have run those 2 commands, you can run the following commands to test your environment variables:

echo $SAUCE_USERNAME
echo $SAUCE_ACCESS_KEY

Run the tests

        // If using the US DC
        mvn clean test -Dregion=us

        // If using the EU DC
        mvn clean test -Dregion=eu

Click here to see an example console output.
  [INFO]
  [INFO] -------------------------------------------------------
  [INFO]  T E S T S
  [INFO] -------------------------------------------------------
  [INFO] Running TestSuite
  Sauce Android Native App  - Before hook
  *** BeforeMethod hook. Running method addProductToCart ***
  Sauce Android Native App  - Before hook
  *** BeforeMethod hook. Running method addProductToCart ***
  region is eu
  region is eu
  region is eu
  region is eu
  Sauce - Start selectProduct test
  Sauce - Start selectProduct test
  Sauce - AfterMethod hook
  Sauce - AfterMethod hook
  Sauce - Start selectProduct test
  Sauce - release driver
  Sauce - Start selectProduct test
  Sauce - AfterMethod hook
  Sauce - release driver
  Sauce - AfterMethod hook
  [INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 31.102 s - in TestSuite
  [INFO]
  [INFO] Results:
  [INFO]
  [INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
  [INFO]

In the next video I demo how to fork a repo and connect to Gitpod:

mobile_workshop_prep.mp4

Local environment setup

  1. Sign up for a free GitHub account
  2. Fork this repository
  • Make sure you are logged into GitHub
  • Click the Fork in the upper right of the GitHub.
  • Give the repo a ⭐ while you're here 🤩
  1. Clone your fork of the repository to your machine. Must have Git installed
git clone URL_OF_YOUR_FORK

Setup environment variables on your system

Run the tests

        // If using the US DC
        mvn clean test -Dregion=us

        // If using the EU DC
        mvn clean test -Dregion=eu

Click here to see an example console output.
  [INFO]
  [INFO] -------------------------------------------------------
  [INFO]  T E S T S
  [INFO] -------------------------------------------------------
  [INFO] Running TestSuite
  Sauce Android Native App  - Before hook
  *** BeforeMethod hook. Running method addProductToCart ***
  Sauce Android Native App  - Before hook
  *** BeforeMethod hook. Running method addProductToCart ***
  region is eu
  region is eu
  region is eu
  region is eu
  Sauce - Start selectProduct test
  Sauce - Start selectProduct test
  Sauce - AfterMethod hook
  Sauce - AfterMethod hook
  Sauce - Start selectProduct test
  Sauce - release driver
  Sauce - Start selectProduct test
  Sauce - AfterMethod hook
  Sauce - release driver
  Sauce - AfterMethod hook
  [INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 31.102 s - in TestSuite
  [INFO]
  [INFO] Results:
  [INFO]
  [INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
  [INFO]

Running GitHub Actions

You can run the Appium tests directly from GitHub using GitHub Actions

  1. Add two new variables: SAUCE_USERNAME and SAUCE_ACCESS_KEY to your GitHub Repository Secret, as shown in the snapshot
    GitHubSecret

More info can be found here.
2. Select (1) Actions tab -> (2) Click on "Real Devices" (3) Click "Run workflow" and (4) Run workflow runGitHubActions
3. GitHub Actions starts to run
startRunGitHubActions
4. You should see four test executions running in Sauce Labs platform runningGitHubActions
5. Wait till the tests passed passedRunGitHubActions

Running the tests using Sauce Orchestrate (SO)

You can run the Appium tests using SO directly from GitHub using GitHub Actions

  1. Add two new variables: SAUCE_USERNAME and SAUCE_ACCESS_KEY to your GitHub Repository Secret, as shown in the snapshot
    GitHubSecret

  2. Add also the two variables: DOCKERHUB_USERNAME and DOCKERHUB_TOKEN to your GitHub Repository Secret with your Docker username and password.

    More info can be found here.

  3. Select (1) Actions tab -> (2) Click on "native app workflow using ImageRunner" (3) Click "Run workflow" and (4) Run workflow

  4. GitHub Actions starts to run
    startRunGitHubActionsSO

The steps to create the image, run a local container and run it using SO

  1. Run the tests locally by using the command:
        mvn clean test
  1. Make sure tests run as expected.
  2. Check docker is up and running by using the command:
        docker info
  1. Create the docker file
  2. build the image from the docker file
        // The format
        $ docker build -t <docker username>/<project-name>-<container-name>:<tag>  <path-to-dockerfile>
       // Example
        $ docker build -t eyalyovelsauce/sauce-java-appium-cross-platform-docker:0.0.1 .
  1. The image should be in the local docker local_docker
  2. Run the image container locally
        $ docker run --rm -e SAUCE_USERNAME=${SAUCE_USERNAME}  -e SAUCE_ACCESS_KEY=${SAUCE_ACCESS_KEY}  eyalyovelsauce/sauce-java-appium-cross-platform-docker:0.0.1
  1. Push Image to a Docker Registry
        $ docker login
        $ docker push eyalyovelsauce/sauce-java-appium-cross-platform-docker:0.0.1
  1. The image should be in the docker Hub hub_docker
  2. Run SO by using the saucectl:
        $ saucectl run

The saucectl config file is here

Extra resources

About

Java example how to run Appium tests on Sauce real devices and virtual devices

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published