Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String variable is not accepting "?" sign

I made code with:

element(by.className('charge')).getText()
    .then(function(text){
        var blabla = "Is this my string?";

        expect(text.match(blabla)).toBe(true);
        console.log(text);
    });

And even is output of my console equal to my blabla variable, I'm getting result:

Expected [ 'Is this my string' ] to be true.

without any "?" sign.

How is it possible?

like image 697
Yte Avatar asked Sep 27 '26 19:09

Yte


2 Answers

The argument for match is:

A regular expression object. If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp(obj).

So don't pass it a string. Explicitly pass it a regular expression object (since that involves much less pain that converting strings in to regex and having to deal with two levels of syntax to escape through).

Regular expressions treat ? as a special character (in the context of your code it means "The g should appear 0 or 1 time". You need to escape question marks if you want to match them.

var blabla = /Is this my string\?/;

That said, if you want to match the whole string, it would be easier to just make that the test:

var blabla = "Is this my string?";
expect(text).toBe(blabla);
like image 50
Quentin Avatar answered Sep 29 '26 07:09

Quentin


The argument to match is meant to be a regular expression where ? has a special meaning. You probably meant toEqual() instead:

expect(element(by.className('charge')).getText()).toEqual("Is this my string?");

If you want a regular expression match, make a regular expression object and use toMatch():

var blabla = /Is this my string\?/;
expect(element(by.className('charge')).getText()).toMatch(blabla);

Note that in protractor expect() is "patched" to resolve promises implicitly and you don't need to use then().

like image 45
alecxe Avatar answered Sep 29 '26 09:09

alecxe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!