String variable does not accept value ?? sign

I made the 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 my console output is equal to my blabla variable, I get the result:

The expected ['Is this my string'] will be true.

without any "?" sign.

How is this possible?

+3


source to share


4 answers


Argument for match

:

Regular expression object. If obj is not a RegExp passed, it is implicitly converted to RegExp using the new RegExp (obj).

So don't pass a string to it. Pass it a regex object explicitly (since that involves much less pain, which converts strings to regex and has to deal with two levels of syntax to escape).

Regular expressions are treated ?

like a special character (in the context of your code, this means “ g

must appear 0 or 1 times.” You need to avoid question marks if you want to match them.



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

      

However, if you wanted to match an entire string, it would be easier to just do this:

var blabla = "Is this my string?";
expect(text).toBe(blabla);

      

+5


source


The argument match

must be a regular expression where it ?

has special meaning. You probably meant toEqual()

:

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

      

If you want regex match, create regex object and use toMatch()

:



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

      

Note that protractor is expect()

"fixed" to resolve promises implicitly and you don't need to use then()

.

+3


source


You probably misunderstood what the method does match

in JS lines:

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/match

Basically a regex will be used to return groups that match, so in this case

< ("Is this my string").match("Is this my string?");
> ["Is this my string"]

      

The answer is correct. What you want to do is just compare strings, just do:

< "Is this my string" === "Is this my string?";
> false

      

Note that this has nothing to do with the test engine you are using (which I don’t know) , but maybe a better way to do it than

expect(text === blabla).toBe(true);

      

Something

expect(text, blabla).toBeEqual();

      

So the error message is pretty;)

+1


source


The string argument supplied match()

is a regular expression, and ?

in this context means "match the previous zero or one time". What does it do in your example :-)

You can explicitly avoid the make question by writing \?

, in which case the behavior is as you expect.

Greetings,

0


source







All Articles