使用css元素检查量角器中是否存在元素

时间:2021-06-19 01:21:20

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

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

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

This test fails, it says:

这个测试失败了,它说:

    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();
});

This results in an error:

这导致一个错误:

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

Yes, I know that no element will be found, but how can 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?

有人知道怎么做到吗?或者元素(by.css())不是这里要使用的方法?

2 个解决方案

#1


75  

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

您可以测试元素是否与isp一起存在。这是isp现代函数的量角器文档。

So, your code would be something like:

所以,你的代码应该是这样的:

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

#2


21  

You need to test if the element is present:

您需要测试元素是否存在:

expect(element(by.css('.elementClass')).isPresent()).toBe(false);

#1


75  

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

您可以测试元素是否与isp一起存在。这是isp现代函数的量角器文档。

So, your code would be something like:

所以,你的代码应该是这样的:

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

#2


21  

You need to test if the element is present:

您需要测试元素是否存在:

expect(element(by.css('.elementClass')).isPresent()).toBe(false);