Reputation: 31
I am comparing text from an element with a string. Both are identical and has an apostrophe in it. I am using Protractor in typescript with Chai to.equal the expect fails.
option1=element(by.xpath()); // I'll do it
async getOption1() {
return await this.Option1.getText();
}
expect(getOption1()).to.equal("I'll do it"); //fails
AssertionError + expected - actual
-I'll do it
+I'll do it
It is something to do with the apostrophe i guess, but the below statement passes.
expect("I'll do it").to.equal("I'll do it")
Can someone please let me know how to fix this?
Thanks
Upvotes: 2
Views: 3675
Reputation: 7733
TL;DR; use encodeURI to see all invisible characters.
I faced this issue with utf-8 bom, to display all the string characters (also invisible characters) I used encodeURI.
console.log({
string1: encodeURI(string1),
string2: encodeURI(string2)
});
Upvotes: 1
Reputation: 13712
getText()
is Async API which return a promise. chai
can't handle promise directly, you can use another package chai-as-promised together to handle promise.
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.
Upvotes: 1
Reputation: 31
Thanks for the response. The string was not equal because one string had “right single quotation mark” instead of the apostrophe.
Upvotes: 0
Reputation: 1199
I've got a conjecture that in fact may not be equal due to eg. ,
\n
or any other symbol. The easiest way to check it is:
this.Option1.getText().then((elementText) => {
console.log('a' + elementText + 'b');
});
If it does not print aI'll do itb
- you get the reason.
Upvotes: 2
Reputation: 734
I'm not sure which methods are you using to declare your variables, or why, so I would just give it an example with my way. To compare an object with a string, in this case, I would do the following:
var option1 = element(by.xpath()); // I'll do it
var getOption1 = function() {
return Option1.getText();
};
expect(getOption1()).toBe("I'll do it");
Hope it helps.
Upvotes: 0