testing - 测试角度材料下拉 e2e 时出现 ScriptTimeoutError
问题描述
我有这个测试用例
it("should have list", async () => {
// wait until list populated
browser.wait(protractor.until.elementLocated(by.css(COMPONENT_CSS.ITEM)));
console.log("First...............");
try {
await componentPage.toggleDD();
} catch (err) {
console.log("err", err);
}
console.log("Second...............");
const count = await componentPage.getCount();
console.log("count", count);
expect(count).toBe(this.COUNT);
await componentPage.toggleDD();
});
这是toggleDD
async toggleDD(): Promise<any> {
const _dropdown = await element(by.id(this.CSS.DD_ID));
await _dropdown.click();
}
但这表明
err ScriptTimeoutError: script timeout
(Session info: chrome=84.0.4147.135)
(Driver info: chromedriver=84.0.4147.30 (48b3e868b4cc0aa7e8149519690b6f6949e110a8-refs/branch-heads/4147@{#310}),platform=Windows NT 6.3.9600 x86_64)
at Object.checkLegacyResponse (E:\PROJECTS\NewProject\node_modules\selenium-webdriver\lib\error.js:546:15)
at parseHttpResponse (E:\PROJECTS\NewProject\node_modules\selenium-webdriver\lib\http.js:509:13)
at E:\PROJECTS\NewProject\node_modules\selenium-webdriver\lib\http.js:441:30
at processTicksAndRejections (internal/process/task_queues.js:94:5)
From: Task: Protractor.waitForAngular() - Locator: By(css selector, *[id="my-dropdown"])
并且测试用例失败了,我试过把await
之前,browser.wait
试过by.xpath
,,来获得下拉菜单。by.id
by.css
我在调试模式下尝试过,元素存在于 DOM 上,但不知何故它没有获取元素。
检查其他元素以获取它显示的每个元素
- Failed: script timeout
(Session info: chrome=84.0.4147.135)
(Driver info: chromedriver=84.0.4147.30 (48b3e868b4cc0aa7e8149519690b6f6949e110a8-refs/branch-heads/4147@{#310}),platform=Windows NT 6.3.9600 x86_64)
我试过了
<div id="dummyId">Hello</div>
通过这个 div
const _divText = await element(by.id("dummyId")).getText();
即使为此我得到脚本超时错误
更多信息
这是beforeAll
beforeAll(async () => {
componentPage = new HomePage();
helperService = new HelperService();
COMPONENT_CSS = componentPage.CSS;
await helperService.navigate(helperService.PAGE_URL.HOME);
try {
await helperService.skipIntro();
} catch (err) {
console.error("Intro element not found");
}
// browser.waitForAngular();
});
和量角器.conf.js
// Protractor configuration file, see link for more information
// https://github.com/angular/protractor/blob/master/lib/config.ts
const { SpecReporter } = require("jasmine-spec-reporter");
exports.config = {
allScriptsTimeout: 50000,
specs: [
"./src/login/login.component.e2e-spec.ts",
"./src/home/home.component.e2e-spec.ts",
],
capabilities: {
"browserName": "chrome"
},
directConnect: true,
baseUrl: "http://localhost:4200/",
framework: "jasmine",
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 1000000,
print: function () {
}
},
onPrepare() {
require("ts-node").register({
project: require("path").join(__dirname, "./tsconfig.e2e.json")
});
jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
}
};
解决方案
Looking at the error, it appears that the element is not displayed before you want to interact with it. until.elementLocated
appears to call into findElements
and return the first element.
This means that it will return it if it is in the DOM but doesn't mean that it will be displayed or enabled to click. The isDisplayed
call explicitly checks that the element is displayed.
Maybe instead do something like this?
it("should have list", async () => {
// wait until list populated
await browser.wait(async () => {
return element(by.css(COMPONENT_CSS.ITEM)).isDisplayed();
}, 10000); // wait 10 seconds, make sure your protractor suite does not time out.
推荐阅读
- java - org.openqa.selenium.ElementNotVisibleException:元素不可见,chrome=66.0.3359.139 和 chromedriver=2.36.540470
- c++ - std::ifstream.read() 不会向我的缓冲区返回任何内容
- go - 没有 TZ 的解析时间
- javascript - 使用 jQuery 自动扩展输入(输入文本)
- python - 运行包含来自 python 脚本的多个字符串的命令行
- asterisk - Kamailio 代理服务器真的可以进行 SIP 呼叫吗?
- jquery-ui - jQuery UI:“基础不是构造函数”
- python - 使用 Python 将包括表格在内的整个 Word 文档复制到另一个文档
- c++ - 我不明白 LeetCode 上关于格雷码的解决方案
- javascript - TS/ES6:实例化类而不调用构造函数