{"id":16091,"date":"2022-05-17T00:59:53","date_gmt":"2022-05-17T04:59:53","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=16091"},"modified":"2022-05-27T02:52:02","modified_gmt":"2022-05-27T06:52:02","slug":"github-actions-to-execute-test-against-localhost-at-ci-stage","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/github-actions-to-execute-test-against-localhost-at-ci-stage\/","title":{"rendered":"GitHub Actions to execute tests against localhost"},"content":{"rendered":"<p>Do you want to setup a GitHub Actions to execute some tests against localhost at CI stage for every branch? <\/p>\n<hr>\n<h3>Background<\/h3>\n<p>In this post, we will discuss how we at <a href=\"https:\/\/qxf2.com\/?utm_source=rust_sqs_lambda&#038;utm_medium=click&#038;utm_campaign=From%20blog\">Qxf2<\/a> 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.<\/p>\n<hr>\n<h3>Let&#8217;s begin<\/h3>\n<p>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, <a href=\"https:\/\/docs.github.com\/en\/actions\/using-workflows\/workflow-syntax-for-github-actions\">workflow<\/a>.<\/p>\n<pre lang=\"yml\">\r\nname: Create GitHub Action\r\n\r\non:\r\npush:\r\nbranches:\r\n- your_branch_name\r\n\r\nThe job is defined along with the child keys which defines the properties of the job.\r\n\r\njobs:\r\npre_deploy_tests:\r\n\r\nruns-on: ubuntu-latest\r\nstrategy:\r\nfail-fast: false\r\nmax-parallel: 5\r\nmatrix:\r\npython-version: [3.8.12]\r\n<\/pre>\n<p>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.<\/p>\n<pre lang=\"yml\">\r\nsteps:\r\n- uses: actions\/checkout@v1\r\n- name: Set up Python ${{ matrix.python-version }}\r\nuses: actions\/setup-python@v1\r\nwith:\r\npython-version: ${{ matrix.python-version }}\r\n<\/pre>\n<p>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.  <\/p>\n<pre lang=\"yml\">\r\n- name: Install dependencies\r\nrun: |\r\nset -ex\r\nwget https:\/\/dl.google.com\/linux\/direct\/google-chrome-stable_current_amd64.deb\r\nsudo apt install .\/google-chrome-stable_current_amd64.deb\r\n- uses: nanasess\/setup-chromedriver@v1\r\n- run: chromedriver --url-base=\/wd\/hub &amp;\r\n<\/pre>\n<hr>\n<h3>How to run the local server?<\/h4>\n<p>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 &#8216;&#038;&#8217; at the end of one command and change the directory (cd..) to include other install commands.<\/p>\n<pre lang=\"yml\">\r\n- name: Run local server\r\n      run: |\r\n        cd html\/\r\n        python run_server.py &\r\n        cd ..\r\n<\/pre>\n<hr>\n<h3>How do you run the local server and the tests in parallel?<\/h3>\n<p>While, I made sure the local server started first before executing the test, I figured that the tests wouldn&#8217;t execute in sequence. Hence, I had to make it run in parallel after the local server started, so that the selenium tests get&#8217;s executed properly. I introduced a job matrix with max-parallel to run the local server and the tests in parallel.<\/p>\n<pre lang=\"yml\">\r\nmax-parallel: 5\r\n<\/pre>\n<pre lang=\"yml\">\r\n- name: Run local server\r\n      run: |\r\n        cd html\/\r\n        python run_server.py &\r\n        sleep 5\r\n        cd ..\r\n        pip install -r tests\/Requirements.txt\r\n        python -m pytest -U http:\/\/localhost:port\/ \r\n<\/pre>\n<p>As, I am using python in this example, The <strong>pip install -r tests\/Requirements.txt<\/strong> installs the specified packages with the mentioned versions.  <\/p>\n<hr>\n<h3>Finally, Execute your Selenium tests against localhost in headless mode<\/h3>\n<p>In the end, you execute the pytest against the localhost in headless mode and check your tests running in the console.<\/p>\n<pre lang=\"yml\">\r\n- name: Run local server\r\n      run: |\r\n        cd html\/\r\n        python run_server.py &\r\n        sleep 5\r\n        cd ..\r\n        pip install -r tests\/Requirements.txt\r\n        python -m pytest -U http:\/\/localhost:port\/ --browser headless-chrome\r\n<\/pre>\n<hr>\n<h3>The complete snippet!<\/h3>\n<p>So, here is the complete snippet for you to use directly in your setup.<\/p>\n<pre lang=\"yml\">\r\nname: Create GitHub Action\r\n\r\non:\r\n  push:\r\n    branches:\r\n      - your_branch_name\r\n\r\njobs:\r\n  pre_deploy_tests:\r\n\r\n    runs-on: ubuntu-latest\r\n    strategy:\r\n      fail-fast: false\r\n      max-parallel: 5\r\n      matrix:\r\n        python-version: [3.8.12]\r\n\r\n    steps:\r\n    - uses: actions\/checkout@v1\r\n    - name: Set up Python ${{ matrix.python-version }}\r\n      uses: actions\/setup-python@v1\r\n      with:\r\n        python-version: ${{ matrix.python-version }}\r\n    - name: Install dependencies\r\n      run: |\r\n        set -ex\r\n        wget https:\/\/dl.google.com\/linux\/direct\/google-chrome-stable_current_amd64.deb\r\n        sudo apt install .\/google-chrome-stable_current_amd64.deb        \r\n        python -m pip install --upgrade pip\r\n        pip install -r requirements.txt\r\n        pip install msedge-selenium-tools\r\n    - uses: nanasess\/setup-chromedriver@v1\r\n    - run: chromedriver --url-base=\/wd\/hub &        \r\n    - name: Run local server\r\n      run: |\r\n        cd html\/\r\n        python run_server.py &\r\n        sleep 5\r\n        cd ..\r\n        pip install -r tests\/Requirements.txt\r\n        python -m pytest -U http:\/\/localhost:port\/ --browser headless-chrome\r\n<\/pre>\n<hr>\n<p>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.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":38,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52,301,107,18],"tags":[312,309,24],"class_list":["post-16091","post","type-post","status-publish","format-standard","hentry","category-continuous-integration","category-github-action","category-pytest","category-python","tag-continuous-integration","tag-github-actions","tag-python-2"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/16091","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=16091"}],"version-history":[{"count":41,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/16091\/revisions"}],"predecessor-version":[{"id":16508,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/16091\/revisions\/16508"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=16091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=16091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=16091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}