带有量角器和Angular 8的UI测试:如何使单个“ it()”正确等待2分钟?

问题描述 投票:0回答:2

我知道在stackoverflow上已经存在一些类似的问题,它们涵盖了类似的问题。但是所有人都没有解决我的特定问题。

问题描述

我希望量角器测试的一个步骤要等待2分钟才能继续。我将所有其他方法已经提到的所有方法都应用在了stackoverflow上,但是都失败了。

[我将向您展示我的初始测试的样子,然后再进行下一步,我更改了什么,遇到了什么问题。

我的package.json

{
  "name": "application",
  "version": "0.1.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "8.2.14",
    "@angular/cdk": "8.2.3",
    "@angular/cli": "8.3.23",
    "@angular/common": "8.2.14",
    "@angular/compiler": "8.2.14",
    "@angular/core": "8.2.14",
    "@angular/forms": "8.2.14",
    "@angular/material": "8.2.3",
    "@angular/platform-browser": "8.2.14",
    "@angular/platform-browser-dynamic": "8.2.14",
    "@angular/router": "8.2.14",
    "@ng-bootstrap/ng-bootstrap": "5.1.0",
    "@types/jszip": "^3.1.7",
    "ajv-keywords": "^3.4.1",
    "bootstrap": "4.3.1",
    "core-js": "^2.6.11",
    "hammerjs": "^2.0.8",
    "jquery": "^3.4.1",
    "jszip": "^3.2.2",
    "ngx-bootstrap": "^3.3.0",
    "popper.js": "^1.16.1",
    "rxjs": "~6.4.0",
    "tslib": "^1.10.0",
    "web-animations-js": "2.3.1",
    "xmlbuilder": "^13.0.2",
    "zone.js": "^0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.803.23",
    "@angular/compiler-cli": "8.2.14",
    "@angular/language-service": "8.2.14",
    "@types/jasmine": "^3.3.16",
    "@types/jasminewd2": "^2.0.8",
    "@types/node": "^11.15.5",
    "codelyzer": "5.1.0",
    "jasmine-core": "~3.3.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "^4.4.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "^2.0.6",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.5.1",
    "protractor": "^5.4.3",
    "ts-node": "8.3.0",
    "tslint": "5.18.0",
    "typescript": "3.5.3"
  }
}

My protractor.conf

const { SpecReporter } = require('jasmine-spec-reporter');

exports.config = {
  allScriptsTimeout: 11000,
  specs: [
    './src/**/*.e2e.ts'
  ],
  capabilities: {
    'browserName': 'firefox'
  },
  directConnect: true,
  baseUrl: 'http://localhost:4200/',
  framework: 'jasmine',
  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 30000,
    print: function() {}
  },
  onPrepare() {
    require('ts-node').register({
      project: require('path').join(__dirname, './tsconfig.e2e.json')
    });
    jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
  }
};

初始测试

import { browser } from 'protractor';
import { HeaderComponentPO } from '../../src/app/components/generic-ui/header/header.component.po';

describe('Test the backup recovery mechanism', () => {

    it('logs start', () => {
        browser.sleep(500);
        console.log('processing 1000_backup_recovery_mechanism.e2e.ts');
    });

    it('10) calls the app on localhost', () => {
        browser.get('http://localhost:4200');
    });

    it('20) clears the localStorage', () => {
        browser.executeScript('window.localStorage.clear();');
    });

    it('30) waits 2 minutes, then expects the button to appear', () => {
        browser.driver.sleep(120000);
        expect<any>(HeaderComponentPO.getRecoveryButton().isDisplayed()).toBeTruthy();
    });

    it('logs end', () => {
        console.log('finished 1000_backup_recovery_mechanism.e2e.ts');
    });
});

步骤30之后发生错误]

Failed: Timed out after 11000 ms
ScriptTimeoutError: Timed out after 11000 ms

方法一-延长茉莉花的DEFAULT_TIMEOUT_INTERVAL

beforeEach(function() {
    originalJasminTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
    jasmine.DEFAULT_TIMEOUT_INTERVAL = 180000;
});

afterEach(function() {
    jasmine.DEFAULT_TIMEOUT_INTERVAL = originalJasminTimeout;
});

步骤30之后发生错误]

Failed: Timed out after 11000 ms
ScriptTimeoutError: Timed out after 11000 ms

方法二-另外将浏览器驱动程序的脚本超时延长到相同程度

beforeEach(function() {
    originalJasminTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
    jasmine.DEFAULT_TIMEOUT_INTERVAL = 180000;
    browser.driver.manage().timeouts().setScriptTimeout(180000);
});

afterEach(function() {
    jasmine.DEFAULT_TIMEOUT_INTERVAL = originalJasminTimeout;
    browser.driver.manage().timeouts().setScriptTimeout(11000);
});

步骤30之后发生错误]

A Jasmine spec timed out. Resetting the WebDriver Control Flow.

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

方法三-缩短浏览器驱动程序的脚本超时时间

beforeEach(function() {
    originalJasminTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
    jasmine.DEFAULT_TIMEOUT_INTERVAL = 180000;
    browser.driver.manage().timeouts().setScriptTimeout(110000);
});

afterEach(function() {
    jasmine.DEFAULT_TIMEOUT_INTERVAL = originalJasminTimeout;
    browser.driver.manage().timeouts().setScriptTimeout(11000);
});

步骤30之后发生错误]

Failed: Timed out after 110000 ms
ScriptTimeoutError: Timed out after 110000 ms

我还尝试将timout放入方法声明中,而不是使用browser.driver.sleep。结果相同。它没有改变任何事情。

it('30) waits 2 minutes, then expects the button to appear', () => {
    expect<any>(HeaderComponentPO.getRecoveryButton().isDisplayed()).toBeTruthy();
}, 120000);

所以您可以看到,无论我做什么,都行不通。 Jasmine或Driver都在抱怨超时。我做错了什么?我在这里想念什么?

angular protractor
2个回答
0
投票

好,所以我自己发现了。问题是后台有一个“ waitForAngular”进程在运行,当您等待太长时间时,无论设置了什么超时,都会使聚会崩溃。因此,诀窍是停用此等待过程。我要做的就是添加此附加方法:

beforeAll(() => {
    browser.waitForAngularEnabled(false);
});

这又是我的整个测试,现在可以按预期运行。

import { browser } from 'protractor';
import { HeaderComponentPO } from '../../src/app/components/generic-ui/header/header.component.po';

describe('Test the backup recovery mechanism', () => {
    let originalJasminTimeout: number;

    beforeAll(() => {
        browser.waitForAngularEnabled(false);
    });

    beforeEach(function() {
        originalJasminTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
        jasmine.DEFAULT_TIMEOUT_INTERVAL = 400000;
        browser.driver.manage().timeouts().setScriptTimeout(400000);
    });

    afterEach(function() {
        jasmine.DEFAULT_TIMEOUT_INTERVAL = originalJasminTimeout;
        browser.driver.manage().timeouts().setScriptTimeout(11000);

    });


    it('logs start', () => {
        browser.sleep(500);
        console.log('processing 1000_backup_recovery_mechanism.e2e.ts');
    });

    it('10) calls the application on localhost', () => {
        browser.get('http://localhost:4200');
        browser.sleep(3000);
    });

    it('20) clears the localStorage', () => {
        browser.executeScript('window.localStorage.clear();');
    });

    it('30) expects the button not to be present', () => {
        expect<any>(HeaderComponentPO.getRecoveryButton().isPresent()).toBeFalsy();
    });

    it('40) waits for 5 minutes and 5 seconds, expects the button then to be displayed', () => {
        browser.driver.sleep(305000);
        expect<any>(HeaderComponentPO.getRecoveryButton().isDisplayed()).toBeTruthy();
    });


    it('logs end', () => {
        console.log('finished 1000_backup_recovery_mechanism.e2e.ts');
    });
});

-1
投票

无论您指定哪个超时,这都是您的默认超时(最大超时)

exports.config = {
  allScriptsTimeout: 11000,

只需使用5*60*1000值进行更新

© www.soinside.com 2019 - 2024. All rights reserved.