Correct syntax for taking screenshots with Selenium's WebDriverJs on Node
Asked Answered
E

1

13

What is the correct way of taking a screenshot when running a webdriver test with Selenium's webdriverjs?

I have the stand-alone selenium server started and I can see the command for taking screenshot is logged on the selenium-server, but the screenshot is not being saved.

My code is the following:

var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().usingServer('http://localURL:4444/wd/hub').withCapabilities({'browserName': 'chrome'}).build();
driver.get([URL to webserver on my local machine])

driver.takeScreenshot("c:\\selenium_local_map\\out1.png");
Euratom answered 9/4, 2013 at 12:39 Comment(0)
P
24

Take screenshot returns a promise that will resolve with a Base64 encoded png. To write the data, you'll need to do something like the following:

function writeScreenshot(data, name) {
  name = name || 'ss.png';
  var screenshotPath = 'C:\\selenium_local_map\\';
  fs.writeFileSync(screenshotPath + name, data, 'base64');
};

driver.takeScreenshot().then(function(data) {
  writeScreenshot(data, 'out1.png');
});

More documentation can be found here

Postdoctoral answered 20/4, 2013 at 3:25 Comment(3)
This is awesome, thank you! Just wanted to add for fellow Node n00bs who are going to copy and paste this then wonder what to do about the resulting error - add the following to line 2: var fs = require('fs');Brod
For this code to work, you need to include the following line: var fs = require('fs'); I spent some time trying to find out what that "fs" was about. you have to install the package then this solution will work: italic npm install fs italicSteiner
Node.js includes the fs module. You just need to require it, not install it separately.Sixtasixteen

© 2022 - 2024 — McMap. All rights reserved.