DEV Community

Jaira Encio for AWS Community ASEAN

Posted on • Edited on

Setting up Selenium CI/CD using AWS Codebuild and Codepipeline

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


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

Copy the following inside requirements.txt


 txt
selenium==3.12.0
ipython==7.0.1


Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

if name == "main":
unittest.main()

Enter fullscreen mode Exit fullscreen mode




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

Enter fullscreen mode Exit fullscreen mode



  1. 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

code2

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

build2

build3

build4

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

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

pipe2

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

pipe3

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

output2

Jaira Encio Linkedin

Top comments (4)

Collapse
 
jahirshawon profile image
Jahir Uddin

getting an error here , can you look into this ?
Image description

Collapse
 
jairaencio profile image
Jaira Encio

Is your app.py same as above?

Collapse
 
jahirshawon profile image
Jahir Uddin

yeah, used all resource from this doc as it is .

Thread Thread
 
jairaencio profile image
Jaira Encio • Edited

hi @jahirshawon I configured the install_driver.sh to use chromedriver.storage.googleapis.co... instead of "wget -N chromedriver.storage.googleapis.co... -P ~/" . just tested right now and my build is working