在节点上使用Selenium的WebDriverJs进行屏幕快照的正确语法

时间:2021-03-08 19:17:20

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

在使用Selenium的webdriverjs运行webdriver测试时,正确的截屏方式是什么?

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.

我已经启动了独立的selenium服务器,我可以看到在selenium服务器上记录下截图的命令,但是截图没有被保存。

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 个解决方案

#1


22  

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:

截屏返回一个承诺,将解决与Base64编码png。要编写数据,您需要执行以下操作:

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

更多的文档可以在这里找到

#1


22  

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:

截屏返回一个承诺,将解决与Base64编码png。要编写数据,您需要执行以下操作:

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

更多的文档可以在这里找到