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.
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 which triggers a Lambda Function, and then the task is executed.
Where to use Lambda?
You can use it with multiple services
And also you can use it with Chatbot
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 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 new role and click on Create Button
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 Basic setting section you need to increase the memory to the max value and also increase the timeout to be 5 min.
Then on Save button to upload your code and apply the changes
9- Wait 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 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
https://aws.amazon.com/lambda/
https://github.com/blackboard/lambda-selenium
In Part 2 we will talking 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
hello, im unable to click any button after launching browser in lambda. Can you provide syntax to click button in nodejs script.