DEV Community

Cover image for Step-by-Step Guide to Setting Up Sauce Labs CI/CD with GitHub Actions for mobile App testing
QAProEngineer
QAProEngineer

Posted on

Step-by-Step Guide to Setting Up Sauce Labs CI/CD with GitHub Actions for mobile App testing

Introduction

Sauce Labs is a cloud-based platform for automated testing of web and mobile applications. In this guide, we'll walk through the steps to set up a CI/CD (Continuous Integration/Continuous Deployment) pipeline using GitHub Actions to perform end-to-end (E2E) testing of a mobile app with Sauce Labs. The GitHub Actions workflow is defined in a YAML file (.github/workflows/saucelabs.yml), and it consists of several steps.

Prerequisites

Before getting started, make sure you have the following prerequisites in place:

  1. A GitHub repository containing your mobile app source code.
  2. A Sauce Labs account with SAUCE_USERNAME and SAUCE_ACCESS_KEY credentials.
  3. The mobile app file (APK in this example) you want to test.

GitHub Repository Setup

  1. Create a .github/workflows directory in your GitHub repository if it doesn't already exist.

  2. Inside the .github/workflows directory, create a YAML file (e.g., saucelabs.yml) to define your CI/CD workflow.

  3. Copy and paste the following YAML code into your saucelabs.yml file:

name: Sauce CI:  app E2E testing

on:
  - push

env:
  SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }}
  SAUCE_ACCESS_KEY: ${{ secrets.SAUCE_ACCESS_KEY }}

jobs:
  build:
    runs-on: macos-latest

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v2

      - name: Set Up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: 16.13.0

      - name: Upload App to Sauce Labs
        run: |
          SAUCE_USERNAME=$SAUCE_USERNAME
          SAUCE_ACCESS_KEY=$SAUCE_ACCESS_KEY
          APP_FILE_PATH=$(pwd)/app-upload/app-release-2.0.5.apk

          response=$(curl -u "$SAUCE_USERNAME:$SAUCE_ACCESS_KEY" --location \
          --request POST 'https://api.us-west-1.saucelabs.com/v1/storage/upload' \
          --form "payload=@$APP_FILE_PATH" \
          --form 'name="app-x86-release.apk"' \
          --form 'description="Android Test App v3"')

          # Extract the file ID from the response
          file_id=$(echo "$response" | jq -r '.item.id')

          # Print the file_id value
          echo "File ID to be deleted: $file_id"

          # Store the file_id in a file
          echo "$file_id" > file_id.txt

      - name: Change to e2e/testing directory
        run: cd e2e/testing

      - name: Install Dependencies
        run: |
          cd e2e/testing
          npm install
          npm run test:android:sauce:emu 
        continue-on-error: true  # Continue even if tests fail

      - name: Delete App from Sauce Labs
        run: |
          SAUCE_USERNAME=$SAUCE_USERNAME
          SAUCE_ACCESS_KEY=$SAUCE_ACCESS_KEY

          # Read the file_id from the stored file
          file_id=$(cat file_id.txt)

          # Print the file_id value
          echo "File ID to be deleted: $file_id"

          # Add a delay of 10 seconds
          sleep 10

          curl -u "$SAUCE_USERNAME:$SAUCE_ACCESS_KEY" --location \
          --request DELETE "https://api.us-west-1.saucelabs.com/v1/storage/files/$file_id"

      - name: Upload Logs on Failure
        if: failure()
        uses: actions/upload-artifact@v2
        with:
          name: logs
          path: logs
Enter fullscreen mode Exit fullscreen mode

Let's break down what each step in the workflow does:

  • Checkout Repository: This step checks out the code from your GitHub repository.

  • Set Up Node.js: Installs Node.js with the specified version (16.13.0 in this case) on the GitHub runner.

  • Upload App to Sauce Labs: Uploads your mobile app (APK file) to Sauce Labs storage and stores the file ID in a text file for future reference.

  • Change to e2e/testing directory: Navigates to the directory where your end-to-end tests are located.

  • Install Dependencies: Installs project dependencies and runs the E2E tests for the Android app on Sauce Labs Android emulators. The continue-on-error flag is set to true to allow the workflow to continue even if the tests fail.

  • Delete App from Sauce Labs: Deletes the app from Sauce Labs storage using the file ID stored earlier. This is optional but helps clean up storage after the tests.

  • Upload Logs on Failure: If any step in the workflow fails (e.g., test failures), this step uploads the logs as artifacts for debugging.

GitHub Secrets

To securely store your Sauce Labs credentials (SAUCE_USERNAME and SAUCE_ACCESS_KEY), you should add them as secrets in your GitHub repository. To add secrets, follow these steps:

  1. Go to your GitHub repository.

  2. Click on "Settings" > "Secrets" > "New repository secret."

  3. Add a secret with the name SAUCE_USERNAME and set its value to your Sauce Labs username.

  4. Add another secret with the name SAUCE_ACCESS_KEY and set its value to your Sauce Labs access key.

Conclusion

With this GitHub Actions workflow, you can automate the E2E testing of your mobile app using Sauce Labs. When you push changes to your repository, GitHub Actions will trigger the workflow, and you'll receive test results and logs in case of failures. This setup helps ensure the quality and reliability of your mobile application through automated testing with Sauce Labs.

Remember to customize the workflow according to your specific project requirements, such as adjusting the Node.js version or the paths to your app and test files.

Top comments (0)