GitHub Actions to execute tests against localhost

Do you want to setup a GitHub Actions to execute some tests against localhost at CI stage for every branch?


Background

In this post, we will discuss how we at Qxf2 have setup a GitHub Actions to execute selenium tests against localhost at the CI stage for every branch and the errors that we faced while setting up and how did we overcome them.


Let’s begin

So, we will start by creating the GitHub Actions with the trigger for the workflow as push event on the branch. The workflow syntax can be referred here, workflow.

name: Create GitHub Action
 
on:
push:
branches:
- your_branch_name
 
The job is defined along with the child keys which defines the properties of the job.
 
jobs:
pre_deploy_tests:
 
runs-on: ubuntu-latest
strategy:
fail-fast: false
max-parallel: 5
matrix:
python-version: [3.8.12]

The steps use actions/checkout that checks out repository into a runner and allows you to run the scripts. Next, the Python gets setup and the dependencies are installed.

steps:
- uses: actions/checkout@v1
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}

While installing the dependencies, make sure to install the google chrome and driver to run it in headless mode. I have encountered an error while trying to run the test in headless mode.

- name: Install dependencies
run: |
set -ex
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo apt install ./google-chrome-stable_current_amd64.deb
- uses: nanasess/setup-chromedriver@v1
- run: chromedriver --url-base=/wd/hub &

How to run the local server?

To run the local server, the script (run_server.py in this case) was present inside html/ folder and hence, I changed directory to html/ and ran the script. While running the local server, you may need to navigate to a different directory to install some more dependencies. Here, I was facing an issue to include all the run commands at once. The solution is actually simple as you can see below. You have to just add ‘&’ at the end of one command and change the directory (cd..) to include other install commands.

- name: Run local server
      run: |
        cd html/
        python run_server.py &
        cd ..

How do you run the local server and the tests in parallel?

While, I made sure the local server started first before executing the test, I figured that the tests wouldn’t execute in sequence. Hence, I had to make it run in parallel after the local server started, so that the selenium tests get’s executed properly. I introduced a job matrix with max-parallel to run the local server and the tests in parallel.

max-parallel: 5
- name: Run local server
      run: |
        cd html/
        python run_server.py &
        sleep 5
        cd ..
        pip install -r tests/Requirements.txt
        python -m pytest -U http://localhost:port/

As, I am using python in this example, The pip install -r tests/Requirements.txt installs the specified packages with the mentioned versions.


Finally, Execute your Selenium tests against localhost in headless mode

In the end, you execute the pytest against the localhost in headless mode and check your tests running in the console.

- name: Run local server
      run: |
        cd html/
        python run_server.py &
        sleep 5
        cd ..
        pip install -r tests/Requirements.txt
        python -m pytest -U http://localhost:port/ --browser headless-chrome

The complete snippet!

So, here is the complete snippet for you to use directly in your setup.

name: Create GitHub Action
 
on:
  push:
    branches:
      - your_branch_name
 
jobs:
  pre_deploy_tests:
 
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      max-parallel: 5
      matrix:
        python-version: [3.8.12]
 
    steps:
    - uses: actions/checkout@v1
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v1
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install dependencies
      run: |
        set -ex
        wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
        sudo apt install ./google-chrome-stable_current_amd64.deb        
        python -m pip install --upgrade pip
        pip install -r requirements.txt
        pip install msedge-selenium-tools
    - uses: nanasess/setup-chromedriver@v1
    - run: chromedriver --url-base=/wd/hub &        
    - name: Run local server
      run: |
        cd html/
        python run_server.py &
        sleep 5
        cd ..
        pip install -r tests/Requirements.txt
        python -m pytest -U http://localhost:port/ --browser headless-chrome

I have tried to list out the steps for setting up a GitHub Actions to execute tests against local host and I hope this blog will help the readers to setup one for them as well.


Leave a Reply

Your email address will not be published. Required fields are marked *