比较2个相同的字符串在量角器中与chai to.equal失败

时间:2019-02-15 21:06:14

标签: protractor chai

我正在将元素中的文本与字符串进行比较。两者是相同的,并且都带有撇号。我在与Chai to.equal的打字稿中使用量角器 期望失败。

Map headers = create_game_response.getHeaderFields()

List gameCodes = headers["GameCode"]

AssertionError      +预期-实际

assert gamesCodes[0] == "1jwoz2qy0js"

我想这与撇号有关,但是下面的语句通过了。

option1=element(by.xpath()); // I'll do it

async getOption1() {
        return await this.Option1.getText();
    }

expect(getOption1()).to.equal("I'll do it");  //fails

有人可以让我知道如何解决此问题吗?

谢谢

4 个答案:

答案 0 :(得分:0)

我不确定您使用的是哪种方法来声明变量,或者为什么使用它,所以我将以自己的方式举一个例子。为了将对象与字符串进行比较,在这种情况下,我将执行以下操作:

 var option1 = element(by.xpath()); // I'll do it

 var getOption1 = function() {
      return Option1.getText();
 };

 expect(getOption1()).toBe("I'll do it");

希望有帮助。

答案 1 :(得分:0)

我有一个推测,实际上可能由于例如而不相等。 \n或任何其他符号。最简单的检查方法是:

this.Option1.getText().then((elementText) => {
    console.log('a' + elementText + 'b');
});

如果它不打印aI'll do itb-您将得到原因。

答案 2 :(得分:0)

感谢您的回复。字符串不相等,因为一个字符串带有“正确的单引号”而不是撇号。

答案 3 :(得分:0)

getText()是异步API,可返回承诺。 chai无法直接处理承诺,您可以一起使用另一个软件包chai-as-promised处理承诺。

const chai = require('chai'),
chai.use(require('chai-as-promised'))

global.expect = chai.expect

// if the actual value is a promise, you muse use 'eventually'
// in pattern: expect().to.eventually.xxxx() as following
// otherwise, don't use eventually
expect(getOption1()).to.eventually.equal('I'll do it')

let name = 'tom'
expect(name).to.equal('tom') // don't use eventually at here, 
                             // due to variable: name is not a promise.