Skip to main content

JavaScript With Selenium – Running JavaScript Automation Scripts On LambdaTest Selenium Grid


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 test cases of JavaScript automation testing on a scalable Selenium infrastructure that is running real browsers and real operating systems.

This post will help you in getting started with the configuration and guide you with running your JavaScript automation testing scripts on LambdaTest’s cloud Selenium grid. This guide will help you:

  • Run a sample Selenium WebDriver test using JavaScript with Selenium test automation.
  • Comprehend and configure Selenium test suite with respect to your capabilities.
  • Select the specific browsers to run your test on.
  • Dig deeper into the advanced features of LambdaTest.
  • Run your test cases in parallel to reduce build times.
  • Test locally hosted pages.

Prerequisites For Running JavaScript With Selenium#


All the code samples in this documentation can be found in the JavaScript LambdaTest Repository on Image GitHub. You can either download or clone the repository to quickly run your tests.

Before getting started with Selenium automation testing on LambdaTest, you need to:

  • Make sure you are using the latest version of JavaScript.
  • Download Selenium JavaScript bindings from the official website. Latest versions of Selenium Client and WebDriver are ideal for running your JavaScript automation testing script on LambdaTest’s Selenium Grid.
  • Install npm from the official website by clicking here.
  • Download LambdaTest tunnel binary file if you wish to test your locally hosted or privately hosted projects.
  • Although, it isn’t necessary, but it is always recommended to use a test automation framework. You can use Protractor for JavaScript.

Getting Started using JavaScript Automation Testing with Selenium#


The first step in using LambdaTest platform is to understand LambdaTest’s Selenium Grid capabilities. Our Selenium Grid uses Remote WebDriver instead of normal Selenium client browser drivers so if you are migrating from locally run Selenium, you would have to invoke LambdaTest Selenium Remote WebDriver. Next, you need to specify in your code, which browser, browser versions, operating systems, and resolution you wish to run your test on, along with defining LambdaTest specific capabilities. You can checkout LambdaTest Capabilities Generator tool to understand more about how you can define running browser environments and leverage advanced LambdaTest capabilities.

Now, let us begin with a simple Selenium Remote WebDriver test first. The JavaScript testing framework code below tests a simple to-do application with basic functionalities like mark items as done, add items in a list, calculate total pending items etc. You can also find this at our JavaScript GitHub repository.

// spec.jsdescribe('Add todo Lists', function () {    browser.ignoreSynchronization = true;    it('Add Customer Test', function () {        browser.get('https://lambdatest.github.io/sample-todo-app/');         browser.driver.findElement(by.name('li1')).then(function (foundElement) {        foundElement.click();            });         browser.driver.findElement(by.name('li2')).then(function (foundElement) {                foundElement.click();            });     browser.driver.findElement(by.id('sampletodotext')).then(function (foundElement) {            foundElement.clear();            foundElement.sendKeys("Yey, Let's add it to list");            });    browser.driver.findElement(by.id('addbutton')).then(function (foundElement) {                foundElement.click();            });         var foo = element(by.xpath('//html/body/div/div/div/ul/li[6]/span'));            expect(foo.getText()).toEqual("Yey, Let's add it to list");      browser.executeScript("lambda-status=passed");        browser.driver.quit();      });});

Below is the config.js file where we will be declaring the desired capabilities.

exports.config = {   // change this to your USERNAME and ACCESSKEY    seleniumAddress: 'https://USERNAME:[email protected]/wd/hub',  capabilities: [{   'name': 'Single Test ',    'build': 'JavaScript Example',    'browserName': 'Chrome',      //Selected browser name    'platform': 'WIN10',        //Selected OS name    'version':'69.0',            // Selected browser version    'video': true,  //To Enable video recording    }],  specs: './tests/*.js'};

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 test logs, screenshots, and video recording for your entire Selenium tests.

Execute The Test#

You would need to execute the below command in your terminal/cmd.

Running First Test

node config.js

Understanding & Configuring Desired Capabilities For JavaScript Automation Testing#


As we said earlier the first step is to configure your test scripts to connect with LambdaTest Selenium Automation Grid. In the above mentioned JavaScript testing framework code, the first thing you would notice is the invoking of remote webdriver instead of the native browser webdrivers. So for example if you are planning to run on Firefox browser in your local machine, you would be using Firefox browser driver like this: Local Browser Code

//In Conf.jsexports.config = {    seleniumAddress: 'https://localhost:4445/wd/hub',    // add more browsers to this array for more parallel tests!!  multiCapabilities: [{    'browserName': 'firefox',        'platform': 'WIN10',           'version':'62.0',    }],    //Add test scripts in this array to execute in parallel  specs: ['tests/spec.js']};

Since now you are performing the tests on LambdaTest’s Firefox browser, the code has to be modified a bit. To run the code on Firefox browser hosted by LambdaTest, you’d need to add the following command to your existing JavaScript testing framework code:

Remote WebDriver

seleniumAddress: 'https://USERNAME:[email protected]/wd/hub'

In the JavaScript testing framework code, we are passing browser, browser version, and operating system information, along with LambdaTest Selenium Grid capabilities via capabilities object.

Our Capabilities Generator will automatically generate the program, based on your provided input. For Instance, if you select the below configurations:

FIELDSSELECTED VALUES
Operating SystemWindows 8.1
BrowserMozilla Firefox
Browser Version62.0
Resolution1280×1024
Selenium Version3.11.0

Then LambdaTest Capabilities Generator will provide you with the below program:

//Browser/OS Capabilities
var capabilities = {    "build" : "your build name", //You can edit this and assign a build name    "name" : "your test name", // Assign a name to your Test    "platform" : "Windows 8.1", // The operating system on which you want to test your website    "browserName" : "Firefox", // The browser on which you want to test    "version" : "62.0", // The browser version which you've selected to perform the test upon    "resolution" : "1280x1024", // The resolution in which you want to run the test as per your operating system    "selenium_version" : "3.11.0", //The version of Selenium on which the test will run    "visual" : true,    "firefox.driver" : v0.21.0}

The most important capabilities to understand here are ‘browserName’, ‘versions’, and ‘platform’. They define which browser environment you wish to run the test on. Rest of the capabilities are important in test management and debugging. We have an inbuilt capabilities generator tool as well that you can use to generate capabilities code for your test suite.

Advanced LambdaTest Capabilities#


In addition to default Selenium Grid capabilities, LambdaTest also has platform specific capabilities like video recording of test runs, console logs of each test run, network logs of each test run, custom profiles for specific browsers, etc. Do checkout our documentations on LambdaTest Advanced capabilities as well.

Testing Locally Hosted Projects Using JavaScript With Selenium#


LambdaTest allows you to test locally hosted website or privately hosted web pages or web applications with the help of Secure Shell (SSH) Tunnel connection. LambdaTest tunnel allows you to test your website or web-pages for cross browser compatibility even before they go live. You can connect your local server with LambdaTest’s Secure Cloud server using SSH tunnel and can perform cross browser testing on LambdaTest’s Selenium Grid. You can also perform testing from different geo locations having different IP addresses and also test the applications secured with your organization’s firewall. To learn more about LambdaTest Secure Shell Tunnel refer to our complete documentation on LambdaTest tunnel.

To get started with LambdaTest tunnel, you can download our binary tunnel fine (.zip) and then extract it-

Once you extract the file, you need to enter the following command in your command terminal. Make sure that you redirect the pointer to the same folder in which you’ve extracted the tunnel file-

LT -user [user's login email] -key [user's access key]

So, let’s assume if your email ID is [email protected] and your user access key is 123asd321 then you’ll need to run the following command in command terminal-

LT -user [email protected] -key 123asd321

Once you get a message that your ‘Secure connection established, you may start your tests now’, you need to enter the following code in your JavaScript testing framework code block:

var capabilities = {        "tunnel" : true //test on localhost network    }

You can also add the capability using LambdaTest Capabilities Generator.

Tunnel Reference: For a complete reference to all the tunnel features we support, visit our tunnel page.

Mark Test As Pass Or Fail In JavaScript Automation Testing#


While performing JavaScript automation testing using Selenium on LambdaTest Selenium Grid, it is imperative to flag an automation test as either passed or failed. This is completely dependent on your testing requirement with respect to the validation of expected behaviour. You can mark whether an automation test passed or failed by adding the below code fragment in your automation script:

//Mark Test Status
browser.executeScript("lambda-status=passed");

Parallel Testing Using Javascript With Selenium#


Parallel testing is one of the most in-demand features of LambdaTest Selenium Grid. By parallel testing, you can run more than one test case, simultaneously. This means that parallel testing would allow you to execute numerous automation test cases altogether. So you can execute a single test scenario across different browsers or even 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 sessions under your opted plan.

For instance, if you have a bucket of 100 automated test cases, and the average time for execution of a single test is around 6 minutes. The time taken normally to completely execute all your test cases sequentially would be 600 minutes i.e. 10 hours. However, if you are opting for a plan which offers 2 concurrent sessions then you can perform 2 parallel test cases. This would divide your time in half as the total time taken through parallel testing with 2 concurrent sessions for the above scenario would be 300 minutes i.e. 5 hours. Similarly, if you have got 4 concurrent sessions with you then the time taken would be a quarter with respect to sequential testing. This way you could fasten your release cycles as much as you want.

Test automation frameworks like Protractor are really useful in running parallel tests. For instance, in our above example, you may notice that we are running our test in a single environment. If you want to cover multiple environments, you would have to change the hard code every time. Or you would have to use arrays, or multi-threading or something similar.

With LambdaTest, you can pace up your test build activities by performing parallel automation testing using JavaScript with Selenium and Protractor. Boost quality assurance of your product by performing extensive and fast-paced automated cross browser testing process with LambdaTest parallel testing.

Checkout the same code below to understand it better. You can also find it at our GitHub page here.

To perform parallel automated testing using LambdaTest Automation in JavaScript with Selenium.

Parallel Testing Example#


// spec.jsdescribe('Add todo Lists', function () {    browser.ignoreSynchronization = true;    it('Add Customer Test', function () {        browser.get('https://lambdatest.github.io/sample-todo-app/');         browser.driver.findElement(by.name('li1')).then(function (foundElement) {        foundElement.click();            });         browser.driver.findElement(by.name('li2')).then(function (foundElement) {                foundElement.click();            });     browser.driver.findElement(by.id('sampletodotext')).then(function (foundElement) {            foundElement.clear();            foundElement.sendKeys("Yey, Let's add it to list");            });    browser.driver.findElement(by.id('addbutton')).then(function (foundElement) {                foundElement.click();            });         var foo = element(by.xpath('//html/body/div/div/div/ul/li[6]/span'));            expect(foo.getText()).toEqual("Yey, Let's add it to list");      browser.executeScript("lambda-status=passed");        browser.driver.quit(); //really important statement for preventing your test execution from a timeout.    });});

Here is parallel conf which executes the same tests in parallel.

//Conf_Parallel
exports.config = {   // change this to your USERNAME and ACCESSKEY    seleniumAddress: 'https://USERNAME:[email protected]/wd/hub',   // add more browsers to this array for more parallel tests!!  multiCapabilities: [{    'name': 'LambdaTest Parallel',    'build': 'JavaScript Example',    'browserName': 'Chrome',         'platform': 'WIN10',            'version':'70.0',      }, {   'name': 'LambdaTest Parallel',    'build': 'JavaScript Example',    'browserName': 'Chrome',         'platform': 'WIN10',            'version':'69.0',  }],   //Add test scripts in this array to execute in parallel  specs: ['tests/spec.js']};

If you notice, with simple annotations and parameters, you can now run your test suite on multiple browsers every time, without changing the browser parameters in code files every time.

Queuing#


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 instance, if you are opting for a pricing where you can avail up to 10 concurrent sessions. Then the maximum number of test cases that can be queued will be equal to 10 + 150 i.e. 160 queued test cases. 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.

Last updated on