Setting up Selenium CI/CD using AWS Codebuild and Codepipeline
Tags: selenium, automation, cicd, testing
Published: 2022-01-13 09:29:10

In this post we will use CodeBuild and CodePipeline as a build environment for our selenium scripts located in CodeCommit repo. 

We will also be using selenium in headless mode so we can run our automated tests without a visible UI shell. 


## File Structure
``` shell
── /MyFolder/               # root folder
  ├── /app.py               # source code main test
  ├── /requirements.txt     # dependencies
  ├── /buildspec.yml        # build spec as part of the source code
  └── /install_driver.sh    # chromedriver and binary
```

**Overview**
1. Create source code files
2. Create CodeCommit Repository and push files
3. Create a build project in AWS CodeBuild
4. Create pipeline in CodePipeline and build all together

## 1. Create source code files
We will be creating the ff:
- buildspec.yml
- requirements.txt
- install_driver.sh
- app.py

### Copy the following inside `install_driver.sh`
``` sh
# Download and Install chromedriver
wget -N https://chromedriver.storage.googleapis.com/71.0.3578.80/chromedriver_linux64.zip -P ~/
unzip ~/chromedriver_linux64.zip -d ~/
rm ~/chromedriver_linux64.zip
sudo mv -f ~/chromedriver /usr/local/bin/chromedriver
sudo chown root:root /usr/local/bin/chromedriver
sudo chmod 0755 /usr/local/bin/chromedriver


# Install chrome broswer
curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list
apt-get -y update
apt-get -y install google-chrome-stable
```

### Copy the following inside `requirements.txt`
``` txt
selenium==3.12.0
ipython==7.0.1
```
Note: I recommend to use chromedriver version between 70 and 63. You can check diff versions in here https://chromedriver.storage.googleapis.com/

### Copy the following inside `app.py`
``` python
from selenium import webdriver
import unittest
from time import sleep

class app_test_case(unittest.TestCase):


    def setUp(self):
        chromeOptions = webdriver.ChromeOptions()
        driver_path = '/usr/local/bin/chromedriver'
        chromeOptions.add_argument('--headless')
        chromeOptions.add_argument('--disable-gpu')
        chromeOptions.add_argument('--no-sandbox')


        self.driver = webdriver.Chrome(driver_path, chrome_options=chromeOptions)
        self.driver.implicitly_wait(30)
        self.driver.maximize_window()
        path = 'https://www.facebook.com/'
        self.base_url = path

    def test_i_d_e_script1(self):
        driver = self.driver
        driver.get(self.base_url)

        get_title = driver.title
        print(get_title)


    def tearDown(self):
        sleep(5)
        self.driver.quit()




if __name__ == "__main__":
    unittest.main()
```

### Copy the following inside `buildspec.yml`
``` yaml
version: 0.2


phases:
  install:
    commands:
       - pip install -U -r requirements.txt
       - sh install_driver.sh 
  build:
    commands:
       - python app.py
```

## 2. Create CodeCommit Repository and push files
1. Open the CodeCommit console at https://console.aws.amazon.com/codesuite/codecommit/home then click create repository

2. Enter Repository name and description (optional) then click create


![code1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mgbg6g6p0mhuic6avj20.png)


![code2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2v4qovktk1r59yk2lrlf.png)

Clone repository and push all file requirements. You can specify a certain branch but we will just be using master.

## 3. Create a build project in AWS CodeBuild
1. Open the AWS CodeBuild console at https://console.aws.amazon.com/codesuite/codebuild/home then click Create project

2. Fill in the following sections. Once complete, choose Create build project at the bottom of the page.

Sections:
Project configuration - Name and description
Source provider - AWS Codecommit, repo name
Environment - Ubuntu, Standard, aws/codebuild/standard:6.0
Buildspec
Batch configuration
Artifacts - none
Logs


![build1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zefhfo4991mys8m8sgvr.png)

![build2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbtnmga29psi7qbmcy1y.png)

![build3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dmmyop1adu8jb83zdmnc.png)

![build4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n6irzfvhfcgabk6vvf67.png)


## 4. Create pipeline in CodePipeline and build all together
We will now combine our process by using AWS CodePipeline to automatically pull our source code in CodeCommit and run build with AWS CodeBuild.

Open the CodePipeline console at http://console.aws.amazon.com/codesuite/codepipeline/home and choose Create Pipeline

On the Step 1: Choose pipeline settings page; in Pipeline name, enter the name for your pipeline.

Choose New service role to allow CodePipeline to create a new service role in IAM.

![pipe1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hx9sosip5t3qzbxeo92g.png)

On the Step 2: Add source stage page; in Source provider, select AWS CodeCommit, repo name, and branch name.

![pipe2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y4olirb7mzbv5q2i1dsw.png)


On the Step 3: Add build stage page, select AWS Codebuild and project name

![pipe3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dz4kcvilizvv0icrv16s.png)

On the Step 4: Add deploy stage page, Choose Skip deploy stage.

On the Step 5: Review, Review all changes then click create pipeline.

## Output
You should see Succeeded status after running pipeline. The build log shows from prebuild ,running command, and generating output.


![output1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ncrgibhx0fsupbuiy64d.png)

![output2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yzrlxbyflaxdih1psmx8.png)

[Jaira Encio Linkedin](https://www.linkedin.com/in/jaira-mae-encio-929a68176/)