【问题标题】:Selenium Webdriver (node.js) take screenshot and save test resultsSelenium Webdriver (node.js) 截屏并保存测试结果
【发布时间】:2014-05-21 05:00:37
【问题描述】:

我开始为网站应用程序开发测试,但遇到了一些问题。

我正在使用 Node.js、webdriver、chromedriver 和 selenium rc。

问题是: 1. 如何截屏并保存在脚本所在的文件夹中。 2. 有没有办法保存测试用例的测试日志?例如,如果在页面上检查一个元素但没有找到它,我该如何输出呢?

【问题讨论】:

  • 不要同时问两个问题——这会使其更复杂,因此对其他人的用处不大。你可以例如通过删除问题的“如何保存测试日志”部分来解决此问题。

标签: javascript node.js selenium-webdriver


【解决方案1】:

为了保存测试日志,您通常使用测试运行程序。当您检查一个元素是否在页面上并且您找不到它时,您会引发异常(通常是断言错误),测试运行器将记录这一点并将其标记为失败的测试。 In the documentation they suggest you use Mocha.

至于将屏幕截图保存到磁盘,the api 看起来像这样

driver.takeScreenshot().then(
    function(image, err) {
        require('fs').writeFile('out.png', image, 'base64', function(err) {
            console.log(err);
        });
    }
);

【讨论】:

  • 似乎它不起作用......我使用 selenium-webdriver,而不是 webdriverjs。不知道它们之间是否有任何区别。
  • 看起来不像:code.google.com/p/selenium/wiki/WebDriverJs#Installing_from_NPM,您看到 Paz 的实际问题是什么?如果您想在 StackOverflow 上获得好的答案,您必须非常具体。
  • TypeError: Object [object Object] has no method 'saveScreenshot' at Object.<anonymous> (D:\*** ** Automation\User-Login.js:28:8) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:902:3
  • 好的,是的,我使用的是不同的库。试试我对香草硒的更新答案。
  • 工作正常。谢谢,我很感激。你忘了在这里关闭括号function(err { :)
【解决方案2】:

aychedee 的回答改编成一个完整的承诺,该承诺将在文件写入后解决并在写入失败时拒绝:

const util = require('util')
const fsp = require('fs').promises

function takeScreenshot(driver, file){
  return driver.takeScreenshot()
    .then(image => fsp.writeFile(file, image, 'base64'))
}

或者在 async 函数中

async function takeScreenshot(driver, file){
  let image = await driver.takeScreenshot()
  await fsp.writeFile(file, image, 'base64')
}

【讨论】:

  • 异步函数中有一个多余的)
【解决方案3】:

为了记录,这是你用WebdriverJS截图的方式:

var webdriverjs = require('webdriverjs'),
    client = webdriverjs.remote({
        desiredCapabilities: {
            browserName: 'chrome'
        }
    });

client
    .init()
    .url('http://google.com')
    .saveScreenshot(__dirname + '/googleScreenshot.png')
    .end();

您也可以使用WebdriverCSS 进行截图。它是 WebdriverJS 做 CSS 回归测试的插件。和PhantomCSS差不多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-06
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    • 2011-03-26
    相关资源
    最近更新 更多