通过使用CSS元素,以检查是否在量角器存在元素量角器、元素、存在、CSS

2023-09-13 02:55:52 作者:囚你于无期

在量角器端到端测试,我想检查是否一个元素使用元素存在(by.css(...)),我的code:

In a protractor end to end test, I want to check if an element exist using element(by.css(...)), my code:

var myElement = element(by.css('.elementClass'));
expect(myElement).toBeUndefined;

此测试失败,它说:

    Expected { locator_ : { using : 'css selector', value : 'div[ng-switch-
    when="resultNav"]' }, parentElementFinder_ : null, opt_actionResult_ :
    undefined, opt_index_ : undefined, click : Function, sendKeys : Function, 
getTagName : Function, getCssValue : Function, getAttribute : Function, getText
 : Function, getSize : Function, getLocation : Function, isEnabled : Function, 
isSelected : Function, submit : Function, clear : Function, isDisplayed : 
Function, getOuterHtml : Function, getInnerHtml : Function, toWireValue : 
Function } to be undefined.

之后,我试图用一个承诺:

After that I tried to use a promise:

element(by.css('.elementClass')).then( functtion(data) {
    expect(data.getText())toBeUndefined();
});

这将导致一个错误:

Error: No element found using locator By.CssSelector(...)

是的,我知道没有的元素会被发现,但我使用的元素我可怎么办创建工作试验(by.css(...))?

Yes, I know that no element will be found, but how can I do I create a working test using element(by.css(...))?

有谁知道如何实现这一目标?或者是元素(by.css())不是方法用在这里?

Does anyone know how to achieve this? or is element(by.css()) not the method to use here?

推荐答案

您可以测试是否元素是present。 这里是量角器文档为为present 功能。

You can test whether the element is present. Here is the protractor docs for the isPresent function.

所以,你的code会是这样的:

So, your code would be something like:

var myElement = element(by.css('.elementClass'));
expect(myElement.isPresent()).toBeFalsy();