Selenium Webdriver Chrome Javascript

S

Selenium and JavaScript | CrossBrowserTesting Documentation

Selenium and JavaScript | CrossBrowserTesting Documentation

Selenium is a great tool to automate our functional tests on websites and web applications in our favorite language. With CrossBrowserTesting, you can use Selenium and JavaScript to run automated browser tests on thousands of real mobile and desktop browsers in the cloud.
Note:
You will need a Username and Authkey to run your tests on CrossBrowserTesting To get yours, sign up for a free trial or purchase a plan.
Install Selenium
To get started, make sure you have Selenium’s language bindings for JavaScript installed and ready. You can find the official resource for this here:
Or if you have NPM loaded on your machine (comes with distribution), you can type the following into the terminal:npm install selenium-webdriver
Run your first test
Once you have Selenium-WebDriver in place, you should be ready to start testing. Copy the following script into your text-editor of choice:
“use strict”; var webdriver = require(“selenium-webdriver”),
SeleniumServer = require(“selenium-webdriver/remote”). SeleniumServer; var cbtHub = “;var username =’YOUR_USERNAME’; //replace with your email address var authkey = ‘YOUR_AUTHKEY’; //replace with your authkey var caps = { name: ‘Basic Test Example’, build: ‘1. 0′, version: ’70’, platform: ‘Windows 10’, screen_resolution: ‘1366×768’, record_video: ‘true’, record_network: ‘false’, browserName: ‘Chrome’, username: username, password: authkey};
async function basicExample(){ try{ var driver = new er(). usingServer(cbtHub). withCapabilities(caps) (); await (”); await tTitle()(function(title) { (“The title is: ” + title)}); ();} catch(err){ handleFailure(err, driver)}}
basicExample();function handleFailure(err, driver) { (‘Something went wrong! n’,, ‘n’); ();}
Choose browsers
To choose your environment, you can select from Browsers, Operating System, Resolutions, and Devices. You can use our easy Selenium Wizard to select your configuration or hit our browser list endpoint.
Record videos
For superior debugging capabilities, CrossBrowserTesting offers the ability to record a video of your Selenium test session. You can find a copy of your test results here.
var caps = { ‘record_video’: ‘true’}
Record network
To record the network packets during your test for performance debugging, set the following to “true”.
var caps = { ‘record_network’: ‘true’}
With our Local Tunnel, you can run a test on a local URL or behind your company’s firewall. There are two different ways to connect the CrossBrowserTesting local tunnel: our client and our Chrome Extension.
To learn more about setting up, starting, and using your tunnel connection, see About Local Testing.
When a tunnel is open, any automated test can access websites locally, behind a firewall, or across a proxy. There is no need to set a capability during your test.
To speed your automated testing, you can run your tests in parallel, making use of several browsers or devices at one time. To run parallel tests using Node we will use the Flow function.
var username =’YOUR_USERNAME’; //replace with your email address var authkey = ‘YOUR_AUTHKEY’; //replace with your authkey var webdriver = require(‘selenium-webdriver’), SeleniumServer = require(‘selenium-webdriver/remote’). SeleniumServer, request = require(‘request’);var remoteHub = “” + username + “:” + authkey + “”;var browsers = [ { browserName: ‘Chrome’, platform: ‘Windows 10′, version: ’64’, screen_resolution: ‘1366×768’}, { browserName: ‘Chrome’, platform: ‘Mac OSX 10. 14′, version: ’71×64’, screen_resolution: ‘1366×768’}, { browserName: ‘Internet Explorer’, platform: ‘Windows 8. 1′, version: ’11’, screen_resolution: ‘1366×768’}];var flows = (function(browser) { var caps = { name: ‘Node Parallel Example’, browserName: owserName, version: rsion, platform: atform, screen_resolution: reen_resolution, username: username, password: authkey}; async function parallelExample(){ try{ var driver = new er(). usingServer(remoteHub). withCapabilities(caps) (); await tSession()(function(session){ var sessionId = _; //need for API calls (‘Session ID: ‘, sessionId); (‘See your test run at: ‘ + sessionId)}); await (”); var element = await ndElement((‘q’)); await ndKeys(‘cross browser testing’); await (); await tTitle()(function(title) { (“The title is: ” + title); if (title! == (‘cross browser testing – Google Search’)) { throw Error(‘Unexpected title: ‘ + title);}}); ();} catch(err){ (‘Exception! n’,, ‘n’); ();}} parallelExample();});
You can take snapshots of errors during your test run for easy debugging and documentation. You can share these snapshots through Jira, Slack, or Email. To take a snapshot during your test run, you just have to invoke our ();To see how to call the API, read below.
var username =’YOUR_USERNAME’; //replace with your email address var authkey = ‘YOUR_AUTHKEY’; //replace with your authkey var webdriver = require(‘selenium-webdriver’);var SeleniumServer = require(‘selenium-webdriver/remote’). SeleniumServer;var request = require(‘request’);var remoteHub = ”;var caps = { name: ‘Login Form Example’, build: ‘1. 0′, version: ’70’, platform: ‘Windows 10’, screen_resolution: ‘1366×768’, record_video: ‘true’, record_network: ‘false’, browserName: ‘Chrome’, username: username, password: authkey};var sessionId = null;
(‘Connection to the CrossBrowserTesting remote server’);
async function fullExample(){ try{ var driver = new er(). withCapabilities(caps) (); (‘Waiting on the browser to be launched and the session to start’); await tSession()(function(session){ sessionId = _; //need for API calls (‘Session ID: ‘, sessionId); (‘See your test run at: ‘ + sessionId)}); //load your URL await (”); //take snapshot via cbt api await driver. takeSnapshot(); //find checkout and click it await ndElement((“username”)). sendKeys(“); //send keys to element to enter text await ndElement((“//*[@type=”password””]””)). sendKeys(“”test123″”); //take snapshot via cbt api driver. takeSnapshot(); //click the archive button await ndElement((“”button[type=submit]””))(); //wait on logged in message await (((“”logged-in-message””))
10000); //take snapshot via cbt api await driver. takeSnapshot(); //quit the driver await () //set the score as passing setScore('pass')(function(result){ ('SUCCESS! set score to pass')});} catch(e){ webdriverErrorHandler(e

10000); //take snapshot via cbt api await driver. takeSnapshot(); //quit the driver await () //set the score as passing setScore(‘pass’)(function(result){ (‘SUCCESS! set score to pass’)});} catch(e){ webdriverErrorHandler(e

driver)}}

Frequently Asked Questions about selenium webdriver chrome javascript

About the author

proxyreview

If you 're a SEO / IM geek like us then you'll love our updates and our website. Follow us for the latest news in the world of web automation tools & proxy servers!

By proxyreview

Recent Posts

Useful Tools