Skip to content

Instantly share code, notes, and snippets.

@donaldpipowitch donaldpipowitch/README.md
Last active Jul 10, 2017

Embed
What would you like to do?
selenium-webdriver and selenium-standalone example

This example shows you how to use selenium-webdriver and selenium-standalone in modern versions with Firefox, Chrome and Safari and without the promise manager.

const selenium = require('selenium-standalone');
const {
Builder,
By,
until,
Capability,
Capabilities,
Browser,
promise
} = require('selenium-webdriver');
// these are the same things (the last one has maximum flexibility):
// new Builder().forBrowser('firefox')
// new Builder().withCapabilities(Capabilities.firefox())
// new Builder().withCapabilities(new Capabilities().set(Capability.BROWSER_NAME, Browser.FIREFOX))
// https://github.com/SeleniumHQ/selenium/wiki/WebDriverJs#moving-to-asyncawait
promise.USE_PROMISE_MANAGER = false;
// default url from selenium-standalone
const url = 'http://localhost:4444/wd/hub';
console.log('Try to run test!');
selenium.install(err => {
if (err) {
throw err;
}
console.log('Installed Selenium.');
selenium.start((err, child) => {
if (err) {
throw err;
}
console.log('Started Selenium.');
// note!: if you use a transpiler or Node 8, you really want to use async/await here ♥
// we can't await `driver` directly, because of a bug
// see https://github.com/SeleniumHQ/selenium/issues/4268
const driver = new Builder()
.forBrowser('chrome')
// if you don't want to use Chrome choose a different browser here:
// You must enable the 'Allow Remote Automation' option in Safari's Develop menu to control Safari via WebDriver.
// .forBrowser('safari')
// .forBrowser('firefox') // doesn't work currently, use the next lines instead
// .withCapabilities(
// new Capabilities()
// .set(Capability.BROWSER_NAME, Browser.FIREFOX)
// .set('acceptInsecureCerts', true) // it looks like this is currently needed
// )
.usingServer(url)
.build(); // returns `driver`
driver.getSession().then(() => {
console.log('Got session.');
driver
.get('http://www.google.com/ncr')
.then(() => driver.findElement(By.name('q')).sendKeys('webdriver'))
.then(() => driver.findElement(By.name('btnG')).click())
.then(() =>
driver.wait(until.titleIs('webdriver - Google Search'), 1000)
)
.then(() => {
console.log('Quit session session.');
return driver.quit();
})
.then(() => {
console.log('Killed child process.');
child.kill();
})
.catch(err => console.log('An ERROR!?', err));
});
});
});
{
"dependencies": {
"selenium-standalone": "^6.5.0",
"selenium-webdriver": "^3.4.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.