首页 > 解决方案 > DOM 中不存在元素,isPresent() 应该返回 false,但事实并非如此。量角器挂在 then() 块内,茉莉花规格超时

问题描述

在我的页面中,有一个覆盖导航栏的欢迎模式。我想测试导航栏的功能,所以我应该先关闭模式才能点击导航栏。

  1. 量角器运行第一个测试:cancel-icon(close button)存在于 DOM > isPresent() 返回true> 量角器点击它并关闭模式(它从 DOM 中删除)。一切都好。
  2. 它运行第二次测试,浏览器再次获取页面。这个时间cancel-icon(close button)在 DOM 中不存在,因为它在上一步中被删除 > isPresent() 应该返回 false 但它挂起并且什么也不返回,直到 Jasmine 规范超时 > 测试失败。

这是我的代码:

describe('My test', function () { 
    beforeEach(function () {
        browser.get(url);
    });

    it('The user clicks on `Discover` in navbar, app-feed-discover should be displayed.', async function () {
        let result = await element(by.id('cancel-icon')).isPresent();
        if (result) await element(by.id('cancel-icon')).click();
        await element(by.css('app-feed-item-component:nth-of-type(1) > div')).click();
        expect(element(by.css('app-feed-discover')).isPresent()).toBeTruthy();
    });

    it('The user clicks on `Products` in navbar, app-products-discover should be displayed.', async function () {
        let result = await element(by.id('cancel-icon')).isPresent();
        if (result) await element(by.id('cancel-icon')).click();
        await element(by.css('app-feed-item-component:nth-of-type(2) > div')).click();
        expect(element(by.css('app-products-discover')).isPresent()).toBeTruthy();
    });
});

我收到了这个错误:

[11:21:18] I/launcher - Running 1 instances of WebDriver
[11:21:18] I/local - Starting selenium standalone server...
[11:21:19] I/local - Selenium standalone server started at http://192.168.5.199:50919/wd/hub
Started
.FA Jasmine spec timed out. Resetting the WebDriver Control Flow.


Failures:
1) My test The user clicks on `Products` in navbar, app-products-discover should be displayed.
  Message:
    Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
  Stack:
    Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
        at listOnTimeout (internal/timers.js:554:17)
        at processTimers (internal/timers.js:497:7)

2 specs, 1 failure
Finished in 247.172 seconds

[11:25:28] I/local - Shutting down selenium standalone server.
[11:25:28] I/launcher - 0 instance(s) of WebDriver still running
[11:25:28] I/launcher - chrome #01 failed 1 test(s)
[11:25:28] I/launcher - overall: 1 failed spec(s)
[11:25:28] E/launcher - Process exited with error code 1

这是我的 conf.js

exports.config = {
    framework: 'jasmine',
    specs: ['final.js'],
    getPageTimeout: 30000,
    allScriptsTimeout: 30000,
    jasmineNodeOpts: {
        defaultTimeoutInterval: 30000
    },
    onPrepare: function () {
        browser.manage().timeouts().implicitlyWait(30000);
    },
    multiCapabilities: [
        {browserName: 'chrome'}
    ]
}

标签: seleniumselenium-webdriverjasmineprotractorangular10

解决方案


最后我想出了如何识别 DOM 上不存在的元素!这很容易!

虽然如果元素不存在 isPresent() 不能正常工作,但我们可以使用它browser.executeScript来代替。它工作正常!

let result = await browser.executeScript(`return document.getElementById("some-id")`);

不用说,如果找到它,则返回元素。如果不是,则返回null


推荐阅读