Executing Selenium test with python(pytest) using GitHub Actions
Tags: github, sdet, python, selenium
Published: 2022-01-13 04:22:09


[![DelRayo.tech](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cmeud6c5og1dh3xvbebp.png)](delrayo.tech)
You can find the working project [Here](https://github.com/delrayo/Pytest-Selenium-GitHubActions).

First we are going to create some example tests.
I'll be using selenium with python(pytest).

So for this example ill be creating 2 files one will be called **conftest.py** and the second one will be **test_web01.py**

[conftest.py in GitHub](https://github.com/delrayo/Pytest-Selenium-GitHubActions/blob/main/conftest.py)

```
import pytest
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.utils import ChromeType
from selenium import webdriver


@pytest.fixture()
def setup(request):
    chrome_service = Service(ChromeDriverManager(chrome_type=ChromeType.GOOGLE).install())

    chrome_options = Options()
    options = [
    "--headless",
    "--disable-gpu",
    "--window-size=1920,1200",
    "--ignore-certificate-errors",
    "--disable-extensions",
    "--no-sandbox",
    "--disable-dev-shm-usage"
]
    for option in options:
        chrome_options.add_argument(option)

    request.cls.driver = webdriver.Chrome(service=chrome_service, options=chrome_options)


    yield request.cls.driver
    request.cls.driver.close()
```
Now lets create the second file where the actual tests are. 

[test_web01.py in GitHub](https://github.com/delrayo/Pytest-Selenium-GitHubActions/blob/main/test_web01.py)

```
import pytest


@pytest.mark.usefixtures("setup")
class TestExampleOne:
    def test_title(self):
        self.driver.get('https://www.delrayo.tech')
        assert self.driver.title == "DelRayo.tech - Delrayo Tech"
    
    def test_title_blog(self):
        self.driver.get('https://www.delrayo.tech/blog')
        print(self.driver.title)
        
```
For running with GithubActions we have to create the following file structure on the repo.
- -.github
- --workflows
- ---test01.yaml

[test01.yaml in GitHub](https://github.com/delrayo/Pytest-Selenium-GitHubActions/blob/main/.github/workflows/test01.yml)

```
# .github/workflows/test01.yaml
name: test01
on:
  workflow_dispatch:  
jobs:
  test01:
    runs-on: ubuntu-latest
    steps:
      - name: Check out this repo
        uses: actions/checkout@v2
     #Setup Python   
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.9'
      - name: Install software
        run: sudo apt-get install -y chromium-browser
      - name: Install the necessary packages
        run: pip install requests webdriver-manager selenium pytest
      - name: Run the PytTest script
        run: pytest -rA
```
Now we just have to go to the actions section on github, select the workflow named test01 and click on the button run workflow.

![GitHub Actions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qjgur61nreucendrmqrv.png)

You can configure this workflow to execute on different triggers modifying 

```

on:
  workflow_dispatch:  

```
With any of these [options](https://docs.github.com/es/actions/learn-github-actions/events-that-trigger-workflows). 

![Test Pass with GitHub Actions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w7jap1twkeqphe6l7rzn.png)
 

You can find the working project [Here](https://github.com/delrayo/Pytest-Selenium-GitHubActions).