UI Testing using Selenium WebDriver and Chrome inside AWS Lambda
Part 1 — Use Selenium Webdriver and Chrome inside AWS Lambda
What is AWS Lambda?
Amazon explains AWS Lambda (λ) as a ‘serverless’ compute service, meaning the developers, don’t have to worry about which AWS resources to launch, or how will they manage them, they just put the code on lambda and it runs, it’s that simple! It helps you to focus on core-competency i.e. App Building or the code.
Where will I use AWS Lambda?
AWS Lambda executes your backend code, by automatically managing the AWS resources. When we say ‘manage’, it includes launching or terminating instances, health checkups, auto-scaling, updating or patching new updates, etc.
You can use it with multiple services
And also you can use it with Chatbot
So, how does it work?
The code that you want Lambda to run is known as a Lambda function. Now, as we know a function runs only when it is called, right? Here, Event Source is the entity that triggers a Lambda Function, and then the task is executed.
Pricing in AWS Lambda
Like most of the AWS services, AWS Lambda is also a pay per use service, meaning you only pay what you use, therefore you are charged on the following parameters
- The number of requests that you make to your lambda function
- The duration for which your code executes.
* Source: AWS official website
How to use Selenium Webdriver and Chrome inside AWS Lambda?
Note:
- We will use Chromium version 62 in headless mode
- You need to have an AWS account, if you don’t have one you can your own account from this link
1- Login to AWS Management Console and Select AWS Lambda from Compute Services
2- Click on Create new a function
3- Select option Create from scratch (default select)
4- Add the function name and create a new role and click on Create Button
UPDATE:
In the new version don't select the latest version of NodeJs, Select version 8.10
5- Congratulation you create your first Lambda function
6- Select that you will upload the code from .zip folder
7- upload the file that you downloaded from this link
The code contains a node.js code that running selenium script using chrome in headless mode and get the page title from the URL
'use strict';exports.handler = (event, context, callback) => {
var webdriver = require('selenium-webdriver');
var chrome = require('selenium-webdriver/chrome');
var builder = new webdriver.Builder().forBrowser('chrome');
var chromeOptions = new chrome.Options();
const defaultChromeFlags = [
'--headless',
'--disable-gpu',
'--window-size=1280x1696', // Letter size'--no-sandbox',
'--user-data-dir=/tmp/user-data',
'--hide-scrollbars',
'--enable-logging',
'--log-level=0',
'--v=99',
'--single-process',
'--data-path=/tmp/data-path',
'--ignore-certificate-errors',
'--homedir=/tmp',
'--disk-cache-dir=/tmp/cache-dir'
]; chromeOptions.setChromeBinaryPath("/var/task/lib/chrome");
chromeOptions.addArguments(defaultChromeFlags);
builder.setChromeOptions(chromeOptions); var driver = builder.build();
driver.get(event.url);
driver.getTitle().then(function(title) { console.log("Page title for " + event.url + " is " + title)
callback(null, 'Page title for ' + event.url + ' is ' + title);
}); driver.quit();
};
8- In the Basic setting section, you need to increase the memory to the max value and also increase the timeout to be 5 min.
Then on the Save button to upload your code and apply the changes
9- Wait for the saving until the success status displayed
10 — Now we will add a Test event using a JSON file and add the URL as a test data
11- Save your test event and click on the Test button
Congratulation the test passed and the result will be the page title of the URL that you are using
You can also Monitor the requests using AWS CloudWatch and all the logs info.
References
In Part 2 we will talk about Testing at Scale with AWS Lambda and Part 3 will be about Using AWS CodePipeline, AWS CodeBuild, and AWS Lambda for Serverless Automated UI Testing
Good Luck and Happy Testing :)
We ♥ Automation Testing
AWS Lambda ♥ Selenium