Javascript 使用量角器验证文本部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34086547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:53:46  来源:igfitidea点击:

Verify the part of text using protractor

javascriptjasmineprotractor

提问by Swati Khandelwal

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 Trouseris contained in the above text or not.

现在,我想验证字符串ArrowGrey Slim Fit Formal Trouser是否包含在上述文本中。

Please suggest!

请建议!

回答by alecxe

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

有多种方法可以使用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.

toStartWith()来自jasmine-matchers哪里。

回答by Swati Khandelwal

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

感谢@winlinuz 的帮助。我自己找到了解决方案。使用toContain会起作用。

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

Above code works perfectly!

以上代码完美运行!

回答by winlinuz

You can assert this way:

你可以这样断言:

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