9

SauceLabs gives examples of how to write remote tests using the WD node package. I prefer the selenium-webdriver package. Is there some way to use that remotely instead?

Artjom B.
  • 58,311
  • 24
  • 111
  • 196
Anonymous
  • 3,136
  • 2
  • 33
  • 47

1 Answers1

18

Taking the sample code from the selenium-webdriver docs, we can modify it as follows to talk to Sauce Labs's selenium cloud. It assumes you've got credentials in ENV vars, of course you could hardcode them if you want to be less secure.

var webdriver = require('selenium-webdriver');

var sauce = 'http://ondemand.saucelabs.com:80/wd/hub';
var driver = new webdriver.Builder().
    usingServer(sauce).
    withCapabilities({
        browserName: 'Chrome',
        platform: 'Windows 2012',
        name: 'Sample selenium-webdriver test',
        username: process.env.SAUCE_USERNAME,
        accessKey: process.env.SAUCE_ACCESS_KEY
    }).
    build();

driver.get('http://www.google.com');
driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');
driver.findElement(webdriver.By.name('btnG')).click();
driver.wait(function() {
    return driver.getTitle().then(function(title) {
        return title === 'webdriver - Google Search';
    });
}, 1000);

driver.quit();
Jonathan Lipps
  • 448
  • 4
  • 10
  • What's the syntax for running tests in multiple browsers? Array instead of object in withCapabilities? – Steven May 28 '15 at 11:52
  • @Steven: you should use an env var matrix passing `SELENIUM_BROWSER`. Similarly I propose using `SELENIUM_REMOTE_URL` instead of `usingServer`. That way you can run local tests on your desktop without connecting to Sauce. – Danielle Madeley Jul 13 '15 at 23:57