首页 > 解决方案 > 测试角度材料下拉 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.idby.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 } }));
    }
};

标签: testingjasmineprotractorangular-e2e

解决方案


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.

See https://github.com/SeleniumHQ/selenium/blob/trunk/javascript/node/selenium-webdriver/lib/until.js#L241

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.

See https://github.com/SeleniumHQ/selenium/blob/trunk/javascript/node/selenium-webdriver/lib/webdriver.js#L2296

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.

推荐阅读