AWS DevOps & Developer Productivity Blog
Using AWS CodePipeline, AWS CodeBuild, and AWS Lambda for Serverless Automated UI Testing
Testing the user interface of a web application is an important part of the development lifecycle. In this post, I’ll explain how to automate UI testing using serverless technologies, including AWS CodePipeline, AWS CodeBuild, and AWS Lambda.
I built a website for UI testing that is hosted in S3. I used Selenium to perform cross-browser UI testing on Chrome, Firefox, and PhantomJS, a headless WebKit browser with Ghost Driver, an implementation of the WebDriver Wire Protocol. I used Python to create test cases for ChromeDriver, FirefoxDriver, or PhatomJSDriver based the browser against which the test is being executed.
Resources referred to in this post, including the AWS CloudFormation template, test and status websites hosted in S3, AWS CodeBuild build specification files, AWS Lambda function, and the Python script that performs the test are available in the serverless-automated-ui-testing GitHub repository.
S3 Hosted Test Website:
AWS CodeBuild supports custom containers so we can use the Selenium/standalone-Firefox and Selenium/standalone-Chrome containers, which include prebuild Firefox and Chrome browsers, respectively. Xvfb performs the graphical operation in virtual memory without any display hardware. It will be installed in the CodeBuild containers during the install phase.
Build Spec for Chrome and Firefox
The build specification for Chrome and Firefox testing includes multiple phases:
- The environment variables section contains a set of default variables that are overridden while creating the build project or triggering the build.
- As part of install phase, required packages like Xvfb and Selenium are installed using yum.
- During the pre_build phase, the test bed is prepared for test execution.
- During the build phase, the appropriate DISPLAY is set and the tests are executed.
version: 0.2
env:
variables:
BROWSER: "chrome"
WebURL: "https://sampletestweb.s3-eu-west-1.amazonaws.com/website/index.html"
ArtifactBucket: "codebuild-demo-artifact-repository"
MODULES: "mod1"
ModuleTable: "test-modules"
StatusTable: "blog-test-status"
phases:
install:
commands:
- apt-get update
- apt-get -y upgrade
- apt-get install xvfb python python-pip build-essential -y
- pip install --upgrade pip
- pip install selenium
- pip install awscli
- pip install requests
- pip install boto3
- cp xvfb.init /etc/init.d/xvfb
- chmod +x /etc/init.d/xvfb
- update-rc.d xvfb defaults
- service xvfb start
- export PATH="$PATH:`pwd`/webdrivers"
pre_build:
commands:
- python prepare_test.py
build:
commands:
- export DISPLAY=:5
- cd tests
- echo "Executing simple test..."
- python testsuite.py
Because Ghost Driver runs headless, it can be executed on AWS Lambda. In keeping with a fire-and-forget model, I used CodeBuild to create the PhantomJS Lambda function and trigger the test invocations on Lambda in parallel. This is powerful because many tests can be executed in parallel on Lambda.
Build Spec for PhantomJS
The build specification for PhantomJS testing also includes multiple phases. It is a little different from the preceding example because we are using AWS Lambda for the test execution.
- The environment variables section contains a set of default variables that are overridden while creating the build project or triggering the build.
- As part of install phase, the required packages like Selenium and the AWS CLI are installed using yum.
- During the pre_build phase, the test bed is prepared for test execution.
- During the build phase, a zip file that will be used to create the PhantomJS Lambda function is created and tests are executed on the Lambda function.
version: 0.2
env:
variables:
BROWSER: "phantomjs"
WebURL: "https://sampletestweb.s3-eu-west-1.amazonaws.com/website/index.html"
ArtifactBucket: "codebuild-demo-artifact-repository"
MODULES: "mod1"
ModuleTable: "test-modules"
StatusTable: "blog-test-status"
LambdaRole: "arn:aws:iam::account-id:role/role-name"
phases:
install:
commands:
- apt-get update
- apt-get -y upgrade
- apt-get install python python-pip build-essential -y
- apt-get install zip unzip -y
- pip install --upgrade pip
- pip install selenium
- pip install awscli
- pip install requests
- pip install boto3
pre_build:
commands:
- python prepare_test.py
build:
commands:
- cd lambda_function
- echo "Packaging Lambda Function..."
- zip -r /tmp/lambda_function.zip ./*
- func_name=`echo $CODEBUILD_BUILD_ID | awk -F ':' '{print $1}'`-phantomjs
- echo "Creating Lambda Function..."
- chmod 777 phantomjs
- |
func_list=`aws lambda list-functions | grep FunctionName | awk -F':' '{print $2}' | tr -d ', "'`
if echo "$func_list" | grep -qw $func_name
then
echo "Lambda function already exists."
else
aws lambda create-function --function-name $func_name --runtime "python2.7" --role $LambdaRole --handler "testsuite.lambda_handler" --zip-file fileb:///tmp/lambda_function.zip --timeout 150 --memory-size 1024 --environment Variables="{WebURL=$WebURL, StatusTable=$StatusTable}" --tags Name=$func_name
fi
- export PhantomJSFunction=$func_name
- cd ../tests/
- python testsuite.py
The list of test cases and the test modules that belong to each case are stored in an Amazon DynamoDB table. Based on the list of modules passed as an argument to the CodeBuild project, CodeBuild gets the test cases from that table and executes them. The test execution status and results are stored in another Amazon DynamoDB table. It will read the test status from the status table in DynamoDB and display it.
AWS CodeBuild and AWS Lambda perform the test execution as individual tasks. AWS CodePipeline plays an important role here by enabling continuous delivery and parallel execution of tests for optimized testing.
Here’s how to do it:
In AWS CodePipeline, create a pipeline with four stages:
- Source (AWS CodeCommit)
- UI testing (AWS Lambda and AWS CodeBuild)
- Approval (manual approval)
- Production (AWS Lambda)
Pipeline stages, the actions in each stage, and transitions between stages are shown in the following diagram.
This design implemented in AWS CodePipeline looks like this:
CodePipeline automatically detects a change in the source repository and triggers the execution of the pipeline.
In the UITest stage, there are two parallel actions:
- DeployTestWebsite invokes a Lambda function to deploy the test website in S3 as an S3 website.
- DeployStatusPage invokes another Lambda function to deploy in parallel the status website in S3 as an S3 website.
Next, there are three parallel actions that trigger the CodeBuild project:
- TestOnChrome launches a container to perform the Selenium tests on Chrome.
- TestOnFirefox launches another container to perform the Selenium tests on Firefox.
- TestOnPhantomJS creates a Lambda function and invokes individual Lambda functions per test case to execute the test cases in parallel.
You can monitor the status of the test execution on the status website, as shown here:
When the UI testing is completed successfully, the pipeline continues to an Approval stage in which a notification is sent to the configured SNS topic. The designated team member reviews the test status and approves or rejects the deployment. Upon approval, the pipeline continues to the Production stage, where it invokes a Lambda function and deploys the website to a production S3 bucket.
I used a CloudFormation template to set up my continuous delivery pipeline. The automated-ui-testing.yaml template, available from GitHub, sets up a full-featured pipeline.
When I use the template to create my pipeline, I specify the following:
- AWS CodeCommit repository.
- SNS topic to send approval notification.
- S3 bucket name where the artifacts will be stored.
The stack name should follow the rules for S3 bucket naming because it will be part of the S3 bucket name.
When the stack is created successfully, the URLs for the test website and status website appear in the Outputs section, as shown here:
Conclusion
In this post, I showed how you can use AWS CodePipeline, AWS CodeBuild, AWS Lambda, and a manual approval process to create a continuous delivery pipeline for serverless automated UI testing. Websites running on Amazon EC2 instances or AWS Elastic Beanstalk can also be tested using similar approach.
About the author
Prakash Palanisamy is a Solutions Architect for Amazon Web Services. When he is not working on Serverless, DevOps or Alexa, he will be solving problems in Project Euler. He also enjoys watching educational documentaries.