LambdaTest Selenium Automation Grid is a cloud based scalable Selenium testing platform which enables you to run your automation scripts on 2000+ different browsers and operating systems. You can now run your JavaScript Selenium automated test cases on a scalable Selenium infrastructure that is running real browsers and real operating systems.
This post will help you in getting started with configuring and running your JavaScript based automation test scripts on LambdaTest Selenium cloud platform using WD. This topic will help you to:
Run a sample Selenium WD test using JavaScript on LambdaTest Automation.
To comprehend and configure Selenium test suite with respect to your capabilities.
Selecting the specific browsers to run your test on.
Explore advanced features of LambdaTest.
To run your test cases in parallel to reduce build times.
To test your locally hosted pages.
All the code samples in this documentation can be found in the WD LambdaTest Repository on GitHub. You can either download or clone the repository to quickly run your tests.
Before getting started with Automated Scripts using Selenium WD automation on LambdaTest Automation, you need to:
Install WD through npm using the below command.
1 |
npm install wd |
Next step is to install Selenium dependencies for Node.js using npm. Here’s the command to run:
1 |
npm i selenium-webdriver |
Follow our documentation on Lambda Tunnel to know it all. OS specific instructions to download and setup tunnel binary can be found at the following links.
Download the binary file of:
Running Selenium WD test scripts on LambdaTest Selenium grid is as easy as changing a few lines of code. To start with, you would have to invoke Selenium remote webdriver instead of local browser webdriver. In addition, since we are using remote webdriver, we have to define which browser environment we want to run the test. We do that by passing browser environment details to LambdaTest Selenium grid via desired capabilities class. You can use LambdaTest Capabilities Generator to select & pass those browser environment specifications.
Let’s checkout sample Selenium WD code running LambdaTest Selenium grid. This is a simple Selenium WD automation script that test a sample to-do list app. The code marks two list items as done, add a list item and then finally give the total number of pending items as output.
You can also find this code at our GitHub repository for WD.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//single_test.js module.exports = { name: 'Google\'s Search Functionality', run : function (browser) { return browser .get("https://www.google.com/ncr") .elementByName('q') .sendKeys('lambdatest\n') .sleep(5000) .title() .should.become('lambdatest - Google Search'); } }; |
Below is the export.config file where we will be declaring the desired capabilities.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//single.conf.js user= process.env.LT_USERNAME || "<your username>", key= process.env.LT_ACCESS_KEY || "<your accessKey>", exports.config = { seleniumHost: 'hub.lambdatest.com', seleniumPort: 80, test: '../tests/single_test.js', capabilities: [{ browserName: 'chrome', platform: 'win10', version: '67', name: "single-test", build: "WD Selenium Sample" }] } |
Now, let’s create lambdatest.js.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
//lambdatest.js var chai = require("chai"), chaiAsPromised = require("chai-as-promised"), wd = require('wd'), colors = require('colors'), child_process = require('child_process') chai.use(chaiAsPromised); chai.should(); chaiAsPromised.transferPromiseness = wd.transferPromiseness; wd.addPromiseChainMethod( 'onQuit', function(done) { if(done) done(); return this; } ); function runOnlambdatest(caps, test, done){ console.log("Starting Test: " + test.name.green + '\n'); var browser = wd.promiseChainRemote(config.seleniumHost, config.seleniumPort, username, accessKey); // optional extra logging browser.on('status', function(info) { console.log(info.cyan); }); browser.on('command', function(eventType, command, response) { console.log(' > ' + eventType.green, command, (response || '').grey); }); browser.on('http', function(meth, path, data) { console.log(' > ' + meth.yellow, path, (data || '').grey); }); test.run(browser.init(caps)).fin(function() { return browser.quit(); }).onQuit(done).done(); } var config_file = process.argv[2] || 'conf.js' var config = require(config_file).config; var test = require(config.test); var username = process.env.LT_USERNAME || config.user; var accessKey = process.env.LT_ACCESS_KEY || config.key; for(var i in config.capabilities){ var caps = config.capabilities[i]; if(caps["tunnel"]){ console.log("Connecting local"); } else { runOnlambdatest(caps, test); } } |
Now, finally create package.json file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
//package.json { "name": "wd-lambdatest-sample", "version": "0.1.0", "readme": "WD Integration with [lambdatest](https://www.lambdatest.com)", "description": "Selenium examples for WD and lambdatest Automate", "repository": { "type": "git", "url": "git+https://github.com/lambdatest/wd-lambdatest-sample.git" }, "keywords": [ "wd", "lambdatest", "wd-lambdatest-sample", "webdriver", "selenium" ], "author": "lambdatest", "bugs": { "url": "https://github.com/lambdatest/wd-lambdatest-sample/issues" }, "homepage": "https://github.com/lambdatest/wd-lambdatest-sample#readme", "scripts": { "test": "npm run single && npm run parallel", "single": "node scripts/lambdatest.js ../conf/single.conf.js", "parallel": "node scripts/lambdatest.js ../conf/parallel.conf.js" }, "dependencies": { "chai": "^3.5.0", "chai-as-promised": "^5.2.0", "colors": "^1.1.2", "wd": "^1.11.1" } } |
The Selenium Webdriver test would open a URL, mark the first two items in the list as done, add an item in the list, and return the total number of pending item. Your results would be displayed on the test console (or command-line interface if you are using terminal/cmd) and on LambdaTest dashboard. LambdaTest Dashboard will help you view all your text logs, screenshots and video recording for your entire Selenium tests.
You would need to execute the below command in your terminal/cmd.
1 2 3 |
//Running First Test npm install npm run single |
Parallel Testing is one of the most demanding features of LambdaTest Selenium Grid. By parallel testing, you can run more than one test case, simultaneously. So you can execute a single test scenario across different browsers or could run different test scenarios across the same browser but with different browser versions. Wondering how many parallel test cases can you run? That depends entirely on the number of concurrent session under your opted plan.
Run the below command for parallel testing.
1 2 |
//Running Tests In Parallel npm run parallel |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
//parallel.conf.js user= process.env.LT_USERNAME || "<your username>", key= process.env.LT_ACCESS_KEY || "<your accessKey>", exports.config = { seleniumHost: 'hub.lambdatest.com', seleniumPort: 80, test: '../tests/single_test.js', commonCapabilities: { name: "parallel-test", build: "wd-lambdatest-sample" }, capabilities: [{ browserName: 'chrome', platform: 'win10', version: '67', },{ browserName: 'firefox', platform: 'win10', version: '60', },{ browserName: 'safari', platform: 'macos 10.13', version: '11', }] } // Code to support common capabilities exports.config.capabilities.forEach(function(caps){ for(var i in exports.config.commonCapabilities) caps[i] = caps[i] || exports.config.commonCapabilities[i]; }); |
To prevent over-exploitation of the platform we have added a capacity constraint on the number of tests that can be queued at our platform. Your maximum queue capacity will be dependent upon the number of concurrent sessions your LambdaTest account is eligible for. Below formula will help you calculate the maximum capacity for your queue.
Maximum Queuing Capacity
Maximum number of parallel tests that can be queued = n + 150
Here, n = number of concurrent sessions.
For example, if you have opted for pricing where you can avail up to 10 concurrent sessions. Then the maximum number of parallel test cases that can be queued will be equal to 10 + 150 i.e. 160 parallel test cases. Queuing allows you to manage your test pipeline in a hassle-free manner. LambdaTest would automatically schedule and execute test cases for you.
LambdaTest Queuing Policy
Here you can get more information about LambdaTest Queuing Policy.
Queuing Timeout
There is also a limit on how long a test case can stay in the queue. If your test case stays in the queue for more than 15 minutes, the test case would be timed out and would not be executed.