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:

Setting Up AWS Environment

AWS CLI Setup Steps

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

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

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

Create IAM Role

Create an S3 Bucket

Create Key Pair for EC2

Launch EC2 Instance

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:

  1. Connect to your EC2 instance:
ssh -i "SeleniumKeyPair.pem" ec2-user@<your-ec2-instance-public-dns>
sudo yum install -y amazon-linux-extras

sudo amazon-linux-extras install epel -y

sudo yum install -y chromium
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:

sudo yum install -y 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

Creating CodePipeline

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.

Leave a Reply

Your email address will not be published. Required fields are marked *