LambdaTest Selenium automation grid enables you to perform end-to-end automation tests on a secure, reliable, and scalable Selenium infrastructure. You can run your Node.js automation test scripts on 2000+ browser and operating system environments giving you a higher test coverage and at significantly less built times. This post will help you to quickly get started with running your Node.js test scripts on LambdaTest Selenium automation grid.
1 |
npm install npm@latest -g |
Next step is to install Selenium dependencies for Node.js using npm. Here’s the command to run:
1 |
npm i selenium-webdriver |
Once you have installed necessary dependencies, you are all set to run your Node.js automation scripts on LambdaTest Selenium grid. To get started you can try out the code below. It’s a simple Node.js scripts that opens up Google.com, search LambdaTest in Google search, and then output the title of the search result page. You can also fork it from our Node.js GitHub repository. Do make sure to create the JS file in the same folder where you have downloaded Selenium.
Do make sure to add the Username and Key in the code below. You can find both these values in profiles section.
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
/* LambdaTest selenium automation sample example Configuration ---------- username: Username can be found at automation dashboard accessKey: AccessKey can be generated from automation dashboard or profile section Result ------- Execute NodeJS Automation Tests on LambdaTest Distributed Selenium Grid */ const webdriver = require('selenium-webdriver'); /* Setup remote driver Params ---------- platform : Supported platform - (Windows 10, Windows 8.1, Windows 8, Windows 7, macOS High Sierra, macOS Sierra, OS X El Capitan, OS X Yosemite, OS X Mavericks) browserName : Supported platform - (chrome, firefox, Internet Explorer, MicrosoftEdge, Safari) version : Supported list of version can be found at https://www.lambdatest.com/capabilities-generator/ */ // username: Username can be found at automation dashboard const USERNAME = '{username}'; // AccessKey: AccessKey can be generated from automation dashboard or profile section const KEY = '{accessKey}'; // gridUrl: gridUrl can be found at automation dashboard const GRID_HOST = 'hub.lambdatest.com/wd/hub'; function searchTextOnGoogle() { // Setup Input capabilities const capabilities = { platform: 'windows 10', browserName: 'chrome', version: '67.0', resolution: '1280x800', network: true, visual: true, console: true, video: true, name: 'Test 1', // name of the test build: 'NodeJS build' // name of the build } // URL: https://{username}:{accessKey}@hub.lambdatest.com/wd/hub const gridUrl = 'https://' + USERNAME + ':' + KEY + '@' + GRID_HOST; // setup and build selenium driver object const driver = new webdriver.Builder() .usingServer(gridUrl) .withCapabilities(capabilities) .build(); // navigate to a url, search for a text and get title of page driver.get('https://www.google.com/ncr').then(function() { driver.findElement(webdriver.By.name('q')).sendKeys('LambdaTest\n').then(function() { driver.getTitle().then(function(title) { setTimeout(function() { console.log(title); driver.quit(); }, 5000); }); }); }); } searchTextOnGoogle(); |
You can execute the test from your terminal through this command.
1 |
node index.js |
Do note the use of remote webdriver here. We have passed browser environment configurations through capabilities class. You can set your environment details as you wish.
To setup your own choice of environment for the test in the code, you can leverage our Capability Generator tool.
Tear Down: The tear down method helps LambdaTest platform understand when the execution of a test is finished. It’s important to tear down each test or else it will give timeout errors.