Overview

Prerun scripts allow you to configure the TestDriver virtual machine (VM) to launch specific browsers before running your tests. This is particularly useful for testing your application across multiple browsers and ensuring compatibility.

By using prerun scripts, you can:

  • Install and launch different browsers based on your test requirements.
  • Customize the test environment for specific scenarios.
  • Ensure consistent browser configurations across operating systems.

Supported browsers

The following browsers can be installed and launched using prerun scripts:

  • Google Chrome
  • Mozilla Firefox

Prerun script example: Installing and launching browsers

The following example demonstrates how to install and launch Google Chrome or Firefox on Windows, macOS, and Linux using a prerun script.

Example: prerun script

prerun: |
  if [ "${{ matrix.browser }}" == "chrome" ]; then
    if [ "${{ matrix.os }}" == "windows" ]; then
      # Install and launch Google Chrome on Windows
      $ProgressPreference = 'SilentlyContinue'
      Invoke-WebRequest -Uri "https://dl.google.com/chrome/install/latest/chrome_installer.exe" -OutFile "$env:TEMP\chrome_installer.exe"
      Start-Process -FilePath "$env:TEMP\chrome_installer.exe" -ArgumentList "/silent", "/install" -Wait
      Start-Process -FilePath "C:\Program Files\Google\Chrome\Application\chrome.exe"
    elif [ "${{ matrix.os }}" == "mac" ]; then
      # Install and launch Google Chrome on macOS
      brew install --cask google-chrome
      open -a "Google Chrome"
    else
      # Install and launch Google Chrome on Linux
      wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
      sudo apt install ./google-chrome-stable_current_amd64.deb -y
      google-chrome &
    fi
  else
    if [ "${{ matrix.os }}" == "windows" ]; then
      # Install and launch Firefox on Windows
      $ProgressPreference = 'SilentlyContinue'
      Invoke-WebRequest -Uri "https://download.mozilla.org/?product=firefox-latest&os=win64&lang=en-US" -OutFile "$env:TEMP\firefox_installer.exe"
      Start-Process -FilePath "$env:TEMP\firefox_installer.exe" -ArgumentList "/S" -Wait
      Start-Process -FilePath "C:\Program Files\Mozilla Firefox\firefox.exe"
    elif [ "${{ matrix.os }}" == "mac" ]; then
      # Install and launch Firefox on macOS
      brew install --cask firefox
      open -a "Firefox"
    else
      # Install and launch Firefox on Linux
      sudo apt update
      sudo apt install firefox -y
      firefox &
    fi
  fi

Testing multiple browsers

You can use the GitHub matrix strategy to test your application across multiple browsers. This ensures comprehensive browser compatibility testing.

Example: Matrix strategy for browser testing

strategy:
  matrix:
    os: [windows, mac, linux]
    browser: [chrome, firefox]

Full workflow example

name: Test Action

permissions:
  actions: read
  contents: read
  statuses: write
  pull-requests: write

on:
  pull_request:
    types: [opened, synchronize, reopened, ready_for_review, labeled, unlabeled]

jobs:
  test-action:
    name: Test Action
    runs-on: ubuntu-latest
    strategy:
      matrix:
        os: [windows, mac, linux]
        browser: [chrome, firefox]
    steps:
      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '16'

      - uses: replayableio/testdriver-action@main
        with:
          prompt: |
            1. open youtube
            2. find a cat video
            3. quit the browser
            4. /summarize
          os: ${{ matrix.os }}
          prerun: |
            # Add prerun script here
          key: ${{ secrets.TD_API_KEY }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          FORCE_COLOR: "3"

Notes

  • Cross-Platform Compatibility: Ensure your prerun scripts are compatible with the operating system specified in the matrix.
  • Browser Versions: Always install the latest stable versions of browsers to ensure compatibility with modern web standards.
  • Performance: Launching browsers in prerun scripts ensures they’re ready for immediate use during tests, reducing setup time.