通过css使用元素来检查元素是否存在于量角器中 [英] Use element by css to check if element exists in Protractor

查看:25
本文介绍了通过css使用元素来检查元素是否存在于量角器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;

这个测试失败了,它说:

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

这会导致错误:

错误:未找到使用定位器 By.CssSelector(...) 的元素

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

是的,我知道不会找到任何元素,但是如何使用 element(by.css(...)) 创建工作测试?

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

有谁知道如何实现这一目标?还是 element(by.css()) 不是这里使用的方法?

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

推荐答案

您可以使用 isPresent 测试 element 是否存在.这里是 isPresent 的量角器文档 函数.

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

因此,您的代码将类似于:

So, your code would be something like:

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

这篇关于通过css使用元素来检查元素是否存在于量角器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆