当 internjs 中的测试失败时,如何截取屏幕截图? [英] How do I take a screenshot when a test in internjs fails?

查看:70
本文介绍了当 internjs 中的测试失败时,如何截取屏幕截图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有当 InternJs 中的测试失败时,我才知道如何截取屏幕截图.我的 registerSuite 中有这个简单的测试;

I am having issues figuring out how to take a screenshot ONLY when a test fails in InternJs. I have this simple test in my registerSuite;

'verify google homepage': function () {
    var url = 'https://www.google.com/';
    return this.remote
        .get(url)
        .getCurrentUrl()
        .then(function (data) {
            assert.strictEqual(data, url, 'Incorrect URL');
        })
        .findByName('q')
            .click()
 }

我可以简单地使用以下代码创建屏幕截图;

I can simply create a screenshot using the following code;

.takeScreenshot
.then(function (data) {
    fs.writeFileSync('/path/to/some/file', data, 'base64');
)}

我只想截图,如果上面的测试没有通过断言或无法找到定位器.

I want to only take a screenshot, if the above test fails the assertion or is unable to find the locator.

我查看了 afterEach 方法,但我不知道如何获取最后一个测试的状态以应用条件.

I looked into the afterEach method, but I can't figure out how to get the status of the last test to apply a conditional.

所以我的问题是,有没有人将他们的 internjs 测试设置为仅对失败进行截图以及它是如何完成的?

So my question is, has anyone setup their internjs test to only take screenshots on failures and how was it accomplished?

推荐答案

  1. 目前无法通过 beforeEachafterEach 方法与当前正在执行的测试进行交互;此功能将在 Intern 的下一个版本中提供.

  1. It is not currently possible to interact with the currently executing test from beforeEach or afterEach methods; this capability is coming in the next version of Intern.

Selenium 服务器默认提供每个 Selenium 命令失败的屏幕截图,它是 error.detail.screen 属性上的 Buffer 对象.如果 Selenium 命令失败,只需使用这个已经有截图等着你的属性.

Selenium server, by default, provides a screenshot on every Selenium command failure, which is a Buffer object on the error.detail.screen property. If a Selenium command fails, just use this property which already has the screenshot waiting for you.

对于断言失败,您可以创建一个简单的承诺助手来为您截屏:

For assertion failures, you can create a simple promise helper to take a screenshot for you:

function screenshotOnError(callback) {
  return function () {
    try {
      return callback.apply(this, arguments);
    }
    catch (error) {
      return this.remote.takeScreenshot().then(function (buffer) {
        fs.writeFileSync('/path/to/some/file', buffer);
        throw error;
      });
    }
  };
}

// ...

'verify google homepage': function () {
  return this.remote.get(url).getCurrentUrl().then(screenshotOnError(function (actualUrl) {
    assert.strictEqual(actualUrl, url);
  }));
}

如果像这样手动包装所有回调太不方便,您还可以创建并使用自定义接口来注册您的测试,以类似的方式自动为您包装测试函数.我将把它留给读者作为练习.

If it’s too inconvenient to wrap all your callbacks manually like this, you can also create and use a custom interface for registering your tests that wraps the test functions automatically for you in a similar manner. I’ll leave that as an exercise for the reader.

这篇关于当 internjs 中的测试失败时,如何截取屏幕截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆