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
- Create source code files
- Create CodeCommit Repository and push files
- Create a build project in AWS CodeBuild
- 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
- Create CodeCommit Repository and push files
Open the CodeCommit console at https://console.aws.amazon.com/codesuite/codecommit/home then click create repository
Enter Repository name and description (optional) then click create
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
Open the AWS CodeBuild console at https://console.aws.amazon.com/codesuite/codebuild/home then click Create project
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
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.
On the Step 2: Add source stage page; in Source provider, select AWS CodeCommit, repo name, and branch name.
On the Step 3: Add build stage page, select AWS Codebuild and project name
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.
Top comments (4)
getting an error here , can you look into this ?
Is your app.py same as above?
yeah, used all resource from this doc as it is .
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