admin管理员组

文章数量:1203808

I want to verify that whether certain text exists in a string or not (using protractor).

In my case, the following code:

element(by.css('h1.text-center')).getText();

will result to:

ArrowGrey Slim Fit Formal Trouser -1 (Size - X)

Now, I want to verify that whether the string ArrowGrey Slim Fit Formal Trouser is contained in the above text or not.

Please suggest!

I want to verify that whether certain text exists in a string or not (using protractor).

In my case, the following code:

element(by.css('h1.text-center')).getText();

will result to:

ArrowGrey Slim Fit Formal Trouser -1 (Size - X)

Now, I want to verify that whether the string ArrowGrey Slim Fit Formal Trouser is contained in the above text or not.

Please suggest!

Share Improve this question edited Dec 4, 2015 at 15:07 alecxe 474k126 gold badges1.1k silver badges1.2k bronze badges asked Dec 4, 2015 at 10:47 Swati KhandelwalSwati Khandelwal 1952 gold badges2 silver badges14 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 14

There are multiple ways to have a partial string match using jasmine:

expect(text).toContain("ArrowGrey Slim Fit Formal Trouser");
expect(text).toMatch("ArrowGrey Slim Fit Formal Trouser");
expect(text).toStartWith("ArrowGrey Slim Fit Formal Trouser");

where toStartWith() is coming from jasmine-matchers.

Thanks @winlinuz for your help. I myself found the solution to it. Using toContain would work.

expect(element(by.css('h1.text-center')).getText()).toContain('ArrowGrey Slim Fit Formal Trouser');

Above code works perfectly!

You can assert this way:

expect(element(by.css('h1.text-center')).getText()).toEqual('ArrowGrey Slim Fit Formal Trouser');

本文标签: javascriptVerify the part of text using protractorStack Overflow