量角器在获取元素文本时超时

Protractor times out fetching text for an element

本文关键字:超时 文本 元素 获取 量角器      更新时间:2023-09-26

将cucumber与Protractor结合使用时,我遇到了一个不寻常的情况,即getText的值从未用相关元素的文本解析,导致我的步骤一直超时。

Then the header should contain the content:
"""
Lorem Ipsum
"""

使用chai-(如所承诺的)作为断言库,我的步骤定义如下:

this.Then(/^the header should contain the content:$/, function(content, callback) {
    var text = element(by.css('.description'));
    this.expect(text.getText())
      .to.eventually.equal(content)
      .and.notify(callback);
  });
但是,在运行测试时,这会给我以下错误:

function timed out after 10000 milliseconds

另外奇怪的是,如果我显式断言getText()返回的promise是否被解析,我的测试通过了:

this.expect(text.getText()).to.eventually.be.fulfilled
// true

如果我只是将自己的解析处理程序附加到getText(),我可以很好地查看内容,因此选择元素和拉内部文本似乎没有问题。

text.getText().then(function(content) {
  console.log(content)
  // Lorem Ipsum
})

我有点不确定这里发生了什么。这个问题似乎只有在试图用chai来解决承诺时才会发生。我错过了什么?

这是我的世界,如果有帮助的话。这是非常基本的:

var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);

function World() {
  this.expect = chai.expect;
  this.assert = chai.assert;
}
module.exports = function() {
  this.World = World;
};

以下是我目前使用的软件包版本:

量角器= 3.3.0
黄瓜= 1.2.0
量角器-黄瓜-骨架= 0.6.0

chai-as-promise = 5.3.0

我不确定我能帮助你的为什么这个问题(真的看起来像你正在做的应该工作),但基于什么工作,我认为你可以把你的断言在一个解析处理程序,以获得测试通过。

text.getText().then(function(content) {
  this.expect(content)
    .to.equal(expectedContent)
    .and.notify(callback);
});

.Then语句中没有定义期望的内容。尝试添加"([^"]*)":

this.Then(/^the header should contain the content: "([^"]*)"$/, function(content, callback) {

有意思。对我来说没问题。您是否尝试记录传入的内容和从get text返回的文本?

this.Then(/^the header should contain the content:$/, function(content, callback) {
  var elem = element(by.css('.description'));
  elem.getText().then(function(elemText) {
    console.log("Element text: " + elemText);
    console.log("Content text: " + content);
    callback();
  });
});

我唯一的其他建议是在步骤文件中拉入Chai:

var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
var expect = chai.expect;

然后尝试不使用this:

this.Then(/^the header should contain the content:$/, function(content, callback) {
  expect(element(by.css('.description')).getText()).to.eventually.equal(content).and.notify(callback);
});