我们正在构建一个Ionic混合应用程序,我们实施了由CI系统运行的自动E2E测试。所有这些测试都一直正常,直到我们在“app.module”中实现了APP_INITIALIZE,才能在初始化之前获得浏览器语言。
这是我们的APP_INITIALIZE实现的脏代码:
-app.module.ts
export function configurateLocaleData(dynamicRegisterLocaleService: DynamicRegisterLocaleService): () => Promise<any> {
return () => dynamicRegisterLocaleService.configurateLocaleData();
}
...
providers: [
...
{
provide: APP_INITIALIZER,
useFactory: configurateLocaleData,
deps: [ DynamicRegisterLocaleService ],
multi: true
}
...
-dynamic寄存器-locale.service.ts
import {Injectable} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {AppConfig} from "../app.constants";
import {DateLocaleService} from "./dateLocale.service";
import {registerLocaleData} from "@angular/common";
@Injectable()
export class DynamicRegisterLocaleService {
constructor(private httpClient: HttpClient) {
}
public configurateLocaleData(): Promise<any> {
let currentLang = DateLocaleService.getLanguage();
return this.httpClient.get("../assets/locales/" + currentLang + ".js", {responseType: "text"}).toPromise()
.then(data => {
const startPos = data.indexOf(AppConfig.EXPORT_DEFAULT);
let localeText = "return " + data.substring(startPos + AppConfig.EXPORT_DEFAULT.length);
registerLocaleData((new Function(localeText))());
});
}
}
这些是我们正在运行的一些量角器 - 黄瓜测试,它的配置是:
-cucumber.conf.js
exports.config = {
allScriptsTimeout: 70000,
framework: 'custom',
frameworkPath: require.resolve('protractor-cucumber-framework'),
directConnect: true,
restartBrowserBetweenTests: true,
capabilities: {
'browserName': 'chrome',
'chromeOptions': {
'args': ['--disable-web-security', '--headless', '--disable-gpu', '--no-sandbox'],
'perfLoggingPrefs': {
'enableNetwork': true,
'enablePage': false
}
},
loggingPrefs: {
performance: 'ALL',
browser: 'ALL'
}
},
specs: [
'../e2e/cucumber/features/*.feature'
],
cucumberOpts: {
require: ['../e2e/cucumber/steps/*.steps.js'],
strict: true
},
};
-cucumber.steps.js
var TestsCfg = require("./../tests.constants.js");
var SupportFunctions = require("./../support/support-functions.js");
var {defineSupportCode, After} = require("cucumber");
var chai = require("chai").use(require("chai-as-promised"));
var expect = chai.expect;
defineSupportCode(function({setDefaultTimeout, When, Then, Given, And}) {
setDefaultTimeout(TestsCfg.CUCUMBER_STEPS_MAX_TIMEOUT_MS);
After(function() {
return browser.manage().logs().get('browser').then(function(browserLogs) {
console.log("Console logs for this scenario: ");
browserLogs.forEach(function(log){
console.log(log.message);
});
});
});
When("I navigate to App", function() {
return browser.get(TestsCfg.APP_TEST_URL);
});
Then("the title should be {string}", function(title) {
return expect(browser.getTitle()).to.eventually.eql(title);
});
Given("I should be able to login as {string}", function(username) {
var usernameInput = element.all(by.css(".login-input .text-input")).get(0).getWebElement();
var passwordInput = element.all(by.css(".login-input .text-input")).get(1).getWebElement();
var loginButton = element(by.id("loginButton"));
return browser.wait(SupportFunctions.isElementVisible(loginButton)).then(function() {
usernameInput.click();
usernameInput.sendKeys(username);
passwordInput.click();
passwordInput.sendKeys(pass);
return loginButton.click();
});
});
Then("the dashboard should be shown for house {string}", function(house) {
var headerSpan = element(by.css(".header-text"));
return browser.wait(SupportFunctions.isElementVisible(headerSpan)).then(function() {
return expect(headerSpan.getText()).to.eventually.eql(house);
});
});
还有更多测试,但所有测试都以相同的方式工作。我们转到所需的URL,我们登录然后运行特定的测试。由于它是一个Ionic应用程序,它们都有ExpectedConditions所以它等到所有内容都正确加载。
问题是,所有这些测试都在本地正确运行,并且在实现APP_INITIALIZE之前,它们也在我们的CI系统中运行。实施APP_INITIALIZE后,每次测试都会出现这些错误:
我们的CI是Bitbucket Pipelines,运行Docker镜像weboaks/node-karma-protractor-chrome:debian-node8
,其中包含Chromium版本64.0.3282.119。我们使用chromedriver 2.37,它是与Chromium 64兼容的最新版本。
这些是我们从测试中得到的事实:
"--headless"
标志的情况下运行Protractor-Cucumber时它可以正常工作。有人知道这是Angular,Protractor还是Chromium bug?
谢谢!
答案 0 :(得分:0)
Protractor
有禁用此等待的方法。 browser.waitForAngularEnabled(false);
我把它放在config
中的onPrepare
文件中,如
onPrepare: async () => {
await browser.waitForAngularEnabled(false);
},
您也可以尝试在开始时将其直接放入测试中。