如何使用Protractor测试页面上不存在的元素?

3

我将尝试测试页面上是否存在某个元素。

我已经尝试使用以下方法,但每次都会出现错误:

方法:

expect(element(CastModule.PersonXpath).isDisplayed()).toEqual(false);

错误:失败:等待Protractor与页面同步超时秒。请参见https://github.com/angular/protractor/blob/master/docs/f ...

你推荐什么方法?

8个回答

6

哈哈,@alecxe,你比我提交得早! - Brine
在Karma报告中收到错误消息“element不是函数”。 - 151291

4

是的,测试"不可见"的内容可能会很困难。您应该可以使用isPresent()方法,它表示在DOM中是否存在,而isDisplayed()则表示它实际上是可见的,我认为这可能是您遇到的问题。尝试...

expect(element(CastModule.PersonXpath).isPresent()).toEqual(false);

您可能也想将此分解为一个方法,并使用预期条件


好的,谢谢。例如,如果我检查了“x”分钟后该元素不再显示,我需要使用什么? - user3188198

1

为了检查可见性(如果isDisplayedisPresent无法工作),只需使用:

 if (!EC.invisibilityOf(ug.personXpath)) {
   throw new Error("Partner logo is not displayed");
 }

1
错误看起来与元素显示无关。它似乎与页面同步有关。尝试忽略同步,然后在期望上方等待angular:
browser.ignoreSynchronization = true;
browser.waitForAngular();
expect(element(CastModule.PersonXpath).isDisplayed()).toEqual(false);

0
您也可以尝试以下代码来处理元素的显示与隐藏。以下代码根据元素的可见性返回true或false。
browser.wait(() => {
 return element(by.className("claasName")).isDisplayed()
      .then(
           (hasDisplayed) => {
              console.log("Has displayed: "+ hasDisplayed);
              return hasDisplayed === false;
            }
        );
   }, 5000)
        .then(
            () => {
                return true;
            },
            () => {
                return false;
            }
        )

0

我成功找到了一个解决方案,使用了protractor库。

var EC = protractor.ExpectedConditions;     browser.wait(EC.invisibilityOf(element(by.xpath(CastModule.PersonXpath))), 5000).then(function() {
            if (EC.invisibilityOf(element(by.xpath(x.LanguagesXpath)))) {
                console.log("Persons module is not present - as expected for this scenario");
            } else {
                throw new Error("Element STILL present");
            }
        });
    });

-1

使用元素存在性,请使用:

var elm =  element(CastModule.PersonXpath);
                elm.isPresent().then(function (present) {
                    if (present) {
                         element(CastModule.PersonXpath).then(function (labels) {

                        });

                    } else {
                        console.log('Element did not found');

                    }`enter code here`

-1
expect(elem.isNull===undefined).to.be.equal(true);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接