selenium - DOM 中不存在元素,isPresent() 应该返回 false,但事实并非如此。量角器挂在 then() 块内,茉莉花规格超时
问题描述
在我的页面中,有一个覆盖导航栏的欢迎模式。我想测试导航栏的功能,所以我应该先关闭模式才能点击导航栏。
- 量角器运行第一个测试:
cancel-icon(close button)
存在于 DOM > isPresent() 返回true
> 量角器点击它并关闭模式(它从 DOM 中删除)。一切都好。 - 它运行第二次测试,浏览器再次获取页面。这个时间
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'}
]
}
- 节点版本:
v14.15.3
- 量角器版本:
v7.0.0
- 角度版本:
v10.0.3
- 浏览器:
Chrome
- 操作系统和版本
MacOS Catalina v10.15.7
解决方案
最后我想出了如何识别 DOM 上不存在的元素!这很容易!
虽然如果元素不存在 isPresent() 不能正常工作,但我们可以使用它browser.executeScript
来代替。它工作正常!
let result = await browser.executeScript(`return document.getElementById("some-id")`);
不用说,如果找到它,则返回元素。如果不是,则返回null
。
推荐阅读
- reactjs - 如何在 AWS EC2 中部署 create-react-app
- python - 无法安装 pipenv 无法升级 pip
- git - Git:从 github 上传到部署的服务器
- javascript - 如何在呈现数据之前编辑或操作我在我的状态下获得的数据
- mysql - 按列时间戳对mysql表进行分区
- hyperledger - 在超级账本作曲家建模语言中,我们是否可以定义一个具有属性的资源,该属性是具有键值对的键控集合?
- python - Pascal VOC 类分割:用于训练的真实像素标记
- z3 - 具有不明确的函数定义公理的意外未知数
- jasmine - 量角器:在新选项卡中检查 pdf 文档
- grails - 在 grails 中填充下拉列表