Protractor 比较两个相同的字符串失败,量角器中的chai to.equal

Protractor 比较两个相同的字符串失败,量角器中的chai to.equal,protractor,chai,Protractor,Chai,我正在将元素中的文本与字符串进行比较。两者都是相同的,并且有一个撇号。我在打字脚本中使用量角器,与Chai to.equal一起使用 预期失败了 option1=element(by.xpath()); // I'll do it async getOption1() { return await this.Option1.getText(); } expect(getOption1()).to.equal("I'll do it"); //fails 断言者 +

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

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

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

expect(getOption1()).to.equal("I'll do it");  //fails
断言者 +预期-实际

 -I'll do it
 +I'll do it
我想这与撇号有关,但下面的陈述是通过的

expect("I'll do it").to.equal("I'll do it")
有人能告诉我怎么解决这个问题吗


谢谢

我不确定您使用哪些方法来声明变量,或者为什么,所以我只想用我的方式给它举个例子。要将对象与字符串进行比较,在本例中,我将执行以下操作:

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

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

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

希望能有所帮助。

我不确定您使用哪些方法来声明变量,或者为什么,所以我只想用我的方式给它举个例子。要将对象与字符串进行比较,在本例中,我将执行以下操作:

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

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

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

希望有帮助。

我有一个猜想,事实上可能不相等,例如,
\n
或任何其他符号。最简单的检查方法是:

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

如果它不打印
,我将执行itb
-你知道原因。

我有一个猜想,事实上可能不相等,例如
\n
或任何其他符号。最简单的检查方法是:

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

如果没有打印
aI将执行itb
-您知道原因。

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

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

getText()
是返回承诺的异步API
chai
无法直接处理承诺,您可以一起使用另一个包来处理承诺

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.
getText()
是返回承诺的异步API
chai
无法直接处理承诺,您可以一起使用另一个包来处理承诺

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.
getText()
是返回承诺的异步API。对于
chai
,它不能直接处理承诺,您可以使用另一个包
chai as promised
一起处理承诺。
getText()
是返回承诺的异步API。对于
chai
,它不能直接处理承诺,您可以使用另一个包
chai作为承诺一起处理承诺。