使用量角器,在跨度中得到一个文本。

时间:2022-11-13 17:54:46

I need to check, using expect and Jasmine, if the text of the span in the button is "Vigente". How can I do it?

我需要使用expect和Jasmine检查按钮中span的文本是否为“Vigente”。我怎么做呢?

<button _ngcontent-hke-28="" class="btn btn-success disabled" tabindex="-1">
<span _ngcontent-hke-28="" class="glyphicon glyphicon-check">
</span> Vigente</button>

Can I use xpath like this:

我可以这样使用xpath吗:

/html/body/application/auth-container/layout/div/main-content/div/norma-cadastro/div/form/div[8]/div/button/text(Vigente)

/ html /身体/应用程序/ auth-container / / div /主要内容/ div布局/ norma-cadastro / div /表单/ div[8]/ div /按钮/文本(Vigente)

2 个解决方案

#1


2  

First of all, the XPath expression you are asking about is not correct - the Vigente part inside the parenthesis is not needed. Also, try not use absolute XPath expressions as the more levels in the path you have, the higher chances it'll break. And, XPaths are not recommended by the Protractor team.

首先,您所询问的XPath表达式不正确——括号内的Vigente部分不需要。另外,不要使用绝对XPath表达式,因为在路径上的级别越高,它就越有可能崩溃。而且,pro拖拉机团队不推荐XPaths。

Instead, I would locate the element by partial button text and check the presence of this element:

相反,我将通过部分按钮文本查找元素,并检查该元素的存在:

expect(element(by.partialButtonText("Vigente")).isPresent()).toBe(true);

Or, you can locate the element by, say, a CSS selector and get the text:

或者,您可以通过CSS选择器来定位元素并获取文本:

expect($("button.btn-success").getText()).toContain(Vigente);

The button.btn-success CSS selector is though quite broad, see if you can improve it.

按钮。btn-success CSS选择器是相当广泛的,看看你能否改进它。

#2


0  

Can't say if it is correct or not.. But you can check it yourself in Chrome Dev tools. Just do inspect element on the element. And then right click on the element in HTML and click Copy. I would recommend using ID's or angular model if you can. XPaths change with changing HTML (unless you use ID's in XPath anyway) and then maintaining the tests becomes a task.

不能说它是否正确。但是你可以在Chrome开发工具中自己检查。只需要检查元素。然后右键单击HTML中的元素并单击Copy。如果可以,我建议使用ID或角模型。XPaths随着HTML的改变而改变(除非您在XPath中使用ID),然后维护测试就变成了一项任务。

See the image.

看到的图像。

使用量角器,在跨度中得到一个文本。

#1


2  

First of all, the XPath expression you are asking about is not correct - the Vigente part inside the parenthesis is not needed. Also, try not use absolute XPath expressions as the more levels in the path you have, the higher chances it'll break. And, XPaths are not recommended by the Protractor team.

首先,您所询问的XPath表达式不正确——括号内的Vigente部分不需要。另外,不要使用绝对XPath表达式,因为在路径上的级别越高,它就越有可能崩溃。而且,pro拖拉机团队不推荐XPaths。

Instead, I would locate the element by partial button text and check the presence of this element:

相反,我将通过部分按钮文本查找元素,并检查该元素的存在:

expect(element(by.partialButtonText("Vigente")).isPresent()).toBe(true);

Or, you can locate the element by, say, a CSS selector and get the text:

或者,您可以通过CSS选择器来定位元素并获取文本:

expect($("button.btn-success").getText()).toContain(Vigente);

The button.btn-success CSS selector is though quite broad, see if you can improve it.

按钮。btn-success CSS选择器是相当广泛的,看看你能否改进它。

#2


0  

Can't say if it is correct or not.. But you can check it yourself in Chrome Dev tools. Just do inspect element on the element. And then right click on the element in HTML and click Copy. I would recommend using ID's or angular model if you can. XPaths change with changing HTML (unless you use ID's in XPath anyway) and then maintaining the tests becomes a task.

不能说它是否正确。但是你可以在Chrome开发工具中自己检查。只需要检查元素。然后右键单击HTML中的元素并单击Copy。如果可以,我建议使用ID或角模型。XPaths随着HTML的改变而改变(除非您在XPath中使用ID),然后维护测试就变成了一项任务。

See the image.

看到的图像。

使用量角器,在跨度中得到一个文本。