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.
Last active
July 10, 2017 07:48
-
-
Save donaldpipowitch/a4e179c6790abb70480ce0ce32fe64b5 to your computer and use it in GitHub Desktop.
selenium-webdriver and selenium-standalone example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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