Comprehensive Guide to Setting Up Automated Testing in CI/CD Pipelines with AWS and Selenium
Sidra Saleem
Posted on May 31, 2024
Introduction
In the modern development landscape, CI and CD pipelines ensure that changes in code are automatically tested, integrated, and deployed. This guide documents the basic steps of creating a CI/CD pipeline using Amazon Web Services (AWS) and Selenium for automated testing. Areas to be covered include CLI and console-based setups, setting up a Selenium test project, configuration of the CI/CD pipeline, running tests, and monitoring.
Prerequisites
Before the actual work can begin, make sure you have the following:
- An AWS Account
- AWS CLI installed and configured in your local machine
- AWS IAM user with the necessary permissions to create and manage resources
- Basic knowledge in the following AWS services: EC2, S3, CodePipeline, CodeBuild
Setting Up AWS Environment
AWS CLI Setup Steps
- Install AWS CLI
Visit AWS CLI Documentation and follow the instructions to install the AWS CLI. Since the installation will depend on the operating system in your local machine, the steps will likely be slightly different. In a nutshell, for Unix-like systems, you can always execute the following commands:
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /
- Configure AWS CLI
After installing the AWS CLI, configure it with following command. Replace the placeholders with your AWS Access Key, Secret Key, Region and Output format.
aws configure
- Create IAM Role
To create the IAM role that our EC2 instances and CodeBuild will use, execute the following commands:
aws iam create-role --role-name SeleniumTestRole --assume-role-policy-document file://trust-policy.json
aws iam attach-role-policy --role-name SeleniumTestRole --policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess
aws iam attach-role-policy --role-name SeleniumTestRole --policy-arn arn:aws:iam::aws:policy/AWSCodePipelineFullAccess
aws iam attach-role-policy --role-name SeleniumTestRole --policy-arn arn:aws:iam::aws:policy/AWSCodeBuildAdminAccess
Please replace file://trust-policy.json with your path to the trust policy JSON file. The trust-policy.json file should contain:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
- Create S3 Bucket
Create an S3 bucket to store your build artifacts:
aws s3 mb s3://my-selenium-artifacts
Replace my-selenium-artifacts with a unique name for your S3 bucket. The bucket name must be unique among all bucket names in AWS.
- Create Key Pair for EC2
Create a key pair that can be used to access your EC2 instances:
aws ec2 create-key-pair --key-name SeleniumKeyPair --query 'KeyMaterial' --output text > SeleniumKeyPair.pem
chmod 400 SeleniumKeyPair.pem
This command will create a new key pair called SeleniumKeyPair and save the private key to a file called SeleniumKeyPair.pem The chmod 400 command ensures the private key file has the correct permissions.
- Launch EC2 Instance
Launch an EC2 instance that will be used for running Selenium tests:
aws ec2 run-instances --image-id ami-0c55b159cbfafe1f0 --count 1 --instance-type t2.micro --key-name SeleniumKeyPair --security-group-ids sg-xxxxxxxx --subnet-id subnet-xxxxxxxx
Replace ami-0c55b159cbfafe1f0 with the correct AMI ID according to your region, and replace security-group-ids and subnet-id with the appropriate values. The security group must have rules which grant inbound access on port 22 from your IP address.
AWS Console Setup Steps
Login to AWS Management Console
- Go to AWS Management Console and login with your AWS credentials.
Create IAM Role
- Go to the IAM service.
- Click on "Roles" in the left pane.
- Click the "Create role" button.
- Under "Select type of trusted entity", select "AWS service", then select the "EC2" service.
- Click "Next: Permissions" button.
- In the search box provided, search for and select the following policies:
- AmazonS3FullAccess
- AWSCodePipelineFullAccess
- AWSCodeBuildAdminAccess
- Click "Next: Tags" (optional) and then "Next: Review".
- Give the role a name: SeleniumTestRole
Create an S3 Bucket
- Go to the S3 service.
- Click on "Create bucket" and the page may look something like this.
- Enter your bucket name which must be unique within all bucket names in AWS.
- Set the configurations as you wish.
- Click "Create bucket".
Create Key Pair for EC2
- Go to the EC2 service.
- In the navigation pane, choose Key Pairs.
- Choose the Create Key Pair button.
- Enter a name for the key pair (e.g., SeleniumKeyPair).
- Choose Create key pair.
- Your browser will download the key file automatically and store it securely.
Launch EC2 Instance
- Go to the EC2 service.
- Choose the Launch Instance button.
- Select an Amazon Linux 2 AMI.
- Choose an instance type (e.g., t2.micro).
- Go through set up the instance details, including attaching the IAM role that we created previously.
- Add storage as required.
- Configure the security group to allow SSH access (port 22) and any other ports you may need.
- Review and launch the instance, selecting the key pair we just created.
Creating a Selenium Test Project
Initializing the Project
Let's start by creating a new directory for your Selenium tests and initializing a new Node.js project:
mkdir selenium-tests
cd selenium-tests
npm init -y
npm install selenium-webdriver mocha chai
This will create a new Node.js project with the necessary dependencies for Selenium, Mocha (a test framework), and Chai (an assertion library).
Writing Test Cases
Create a new test file e.g., test.js in your project directory and write the following code to jot down a simple Selenium test:
const {Builder, By, until} = require('selenium-webdriver');
const {expect} = require('chai');
describe('Google Search', function() {
it('should find the correct title', async function() {
let driver = await new Builder().forBrowser('chrome').build();
try {
await driver.get('http://www.google.com');
let title = await driver.getTitle();
expect(title).to.equal('Google');
} finally {
await driver.quit();
}
});
});
This test will open the Google Chrome browser to google.com, get its title and confirm it with the expected title "Google".
Installing WebDriver and Browsers
To run Selenium tests, you need WebDriver and a browser installed on your EC2 instance. Here's how to install Chrome and ChromeDriver:
- Connect to your EC2 instance:
ssh -i "SeleniumKeyPair.pem" ec2-user@<your-ec2-instance-public-dns>
- Install Google Chrome:
sudo yum install -y amazon-linux-extras
sudo amazon-linux-extras install epel -y
sudo yum install -y chromium
- Install ChromeDriver:
wget https://chromedriver.storage.googleapis.com/90.0.4430.24/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
sudo mv chromedriver /usr/bin/
Check the latest compatible version of Chrome and replace the version and URL as needed.
Configuring EC2 Instance for Selenium Tests
In order to make the EC2 instance capable of running the Selenium tests with Chrome, you need to configure Xvfb (X virtual framebuffer) for headless execution:
- Install Xvfb:
sudo yum install -y Xvfb
- Start Xvfb:
Xvfb :99 -ac &
export DISPLAY=:99
Note these commands can be included in a startup script or initialization step for the CI/CD pipeline for ease of setup.
Configuring CI/CD Pipeline
Creating a CodeCommit Repository
Create a new CodeCommit repository for storing the code for your Selenium tests.
aws codecommit create-repository --repository-name selenium-tests
Pushing Code to CodeCommit
From the terminal, in your project directory, initialize a git repository, add the remote repo for CodeCommit, and push the code.
git init
git remote add origin codecommit::us-east-1://selenium-tests
git add .
git commit -m "Initial commit"
git push -u origin master
You would first need to have the correct IAM permissions configured for Git for it to interact with CodeCommit.
Creating Build Specification
Create a file named buildspec.yml in your project directory. Insert the following content in it.
version: 0.2
phases:
install:
commands:
- npm install
pre_build:
commands:
- echo Pre-build phase
build:
commands:
- npm test
artifacts:
files:
- '**/*'
In the build specification, the CodeBuild project will execute commands to install dependencies, run your tests, and archive the test results.
Creating CodeBuild Project
- Open the AWS Management Console and navigate to the CodeBuild dashboard.
- Click on the "Create build project" button.
- Enter a project name. For example, SeleniumTestProject.
- For Source provider, choose CodeCommit. Under Source version, click "master" branch.
- For Environment image, choose Managed image. For Operating System, select Ubuntu. For runtime, select Node.js.
- For Service Role, select the CodeBuild service role.
- In the BuildSpec section, make sure "Use a buildspec file" is selected.
- In the Artifacts section, select the previously created S3 bucket.
- Click Create build project button.
Creating CodePipeline
- Open the CodePipeline service from the AWS management console.
- Hit the create pipeline button.
- Specify a name for the pipeline (e.g., SeleniumPipeline).
- In the "Source" stage, select the source provider as "CodeCommit" and provide the repository and the branch.
- In the "Build" stage, select the build provider as "CodeBuild" and provide the build project that was created in the previous steps.
- Optionally, create a deploy stage in the pipeline.
- Click the "Create pipeline" button.
Running and Monitoring Tests
Committing Changes
Now, whenever you make any modifications in your Selenium test or respective code, commit and push all the changes to the CodeCommit repository.
git add .
git commit -m "Updated tests"
git push origin master
Monitoring Pipeline Execution
Go to the AWS Management Console and click on the CodePipeline service to monitor how your new pipeline is working. You will see the processes for each stage execution, like source, build, and, optionally, deploy.
Viewing Build Logs
Click on the CodeBuild service and select your build project. On this page, you can see the logs related to each build execution, including the output of your test cases.
Conclusion
Well, that's it. You just set up a very solid environment for your Selenium automated test with a CI/CD AWS pipeline. With this setup, the changes in code are automatically tested and integrated into the software, making sure that all processes provide assurance on the code quality and software stability. This is a setup for you to customize the configurations and extend the pipeline according to the requirements of projects, scaling up your testing efforts.
Posted on May 31, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.