首页 > 解决方案 > 角,量角器,黄瓜问题

问题描述

我对黄瓜和量角器完全陌生,以前从未编写过任何 e2e 测试。我正在将此 libaray用于 Angular,但在运行测试时遇到了各种困难。

我的步骤文件如下所示:

const {Given, Then} = require('cucumber');
const expect = require('expect');

Given('I navigate to the homepage', function (callback) {
  browser.get('http://localhost:4200');
  callback();
});

Then('I want to see a welcome message', function (callback) {
  expect($$('h1').first().getText()).toEqual('Welcome!');
  callback();
});

但它似乎getText()是一个异步调用。getText()似乎正在回报一个承诺。

该测试应该失败,因为h1is not的文本Welcome!。当我尝试捕获 promise 和expect()inthen()时,测试在它应该失败的地方成功。

网上有很多关于黄瓜/量角器的资源,都说不同的东西。很难知道如何开始。实际的 cucumber 文档没有给出测试浏览器元素的例子。

任何人都可以帮忙吗?我正在使用量角器黄瓜框架 6.1.1 和黄瓜 5.0.1。

标签: angularprotractorcucumbere2e-testingcucumberjs

解决方案


将以下步骤功能更改如下:

Then('I want to see a welcome message', function (callback) {
    $$('h1').first().getText().then(function(txt){
        console.log('in then() txt: ' +txt);
        expect(txt).toEqual('Welcome!');
        callback();
    });    
});

在您的场景中添加多行Then I want to see a welcome message 1,并为其添加以下步骤功能。

Then('I want to see a welcome message 1', function (callback) {
    expect($$('h1').first().getText()).resolves.toEqual('Welcome!');
    callback();
});

再次运行并告诉我结果和输出console.log('in then() txt: ' +txt)


推荐阅读