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

情到浓时终转凉″ 提交于 2019-12-08 15:05:36

问题


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");

回答1:


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



来源:https://stackoverflow.com/questions/15902142/correct-syntax-for-taking-screenshots-with-seleniums-webdriverjs-on-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!