Without going into too much detail, I just wanted to post a snippet of how to get all these technologies playing nicely together:
var webdriver = require('selenium-webdriver'); var fs = require('fs'); var driver = new webdriver.Builder() .withCapabilities(webdriver.Capabilities.phantomjs()) .build(); webdriver.WebDriver.prototype.saveScreenshot = function(filename) { return driver.takeScreenshot().then(function(data) { fs.writeFile(filename, data.replace(/^data:image\/png;base64,/,''), 'base64', function(err) { if(err) throw err; }); }) }; webdriver.By.sizzle = function(selector) { driver.executeScript("return typeof Sizzle==='undefined'").then(function(noSizzle) { if(noSizzle) driver.executeScript(fs.readFileSync('sizzle.min.js', {encoding: 'utf8'})); }); return new webdriver.By.js("return Sizzle('"+selector.replace(/"/g,'\\"')+"')[0]"); }; driver.get('http://google.com/'); driver.findElement({sizzle:'input[name=q]'}).sendKeys('cheese\n'); driver.saveScreenshot('cheese.png'); driver.quit();
Node that you will need to start the Selenium server before you can run this example. Do so via:
java -jar selenium-server-standalone-2.35.0.jar
One thought on “Selenium, PhantomJS, Node, Screenshots and Sizzle”
Nice article.. been looking throughout the web for example using node,phantomjs and selenium…
I have been trying to do similar thing.. but ran into issue when I’m trying to assert an element ID exists or not…
Here is my StackOverflow question..
http://stackoverflow.com/q/24556738/2958224
Can you please help me on it.. where am I going wrong.. thank you!
HBK35