我有一个简单的WebdriverIO项目,有几个测试用例。我在"本地"运行所有内容(没有根据我的wdio.conf.js
设置声明CI
变量),browserName: "chrome"
处于无头模式,Mocha
测试运行器。
现在,我正在尝试对Selenium服务器(通过Docker启动)运行完全相同的测试用例,但是所有测试用例都失败了,并显示以下小错误消息:Timeout of 15000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
如果我转到Selenium Web控制台,我可以看到正在创建和删除的会话,因此配置应该没问题。
// wdio.conf.js
const cfg = require("config");
const fs = require("fs");
const path = require("path");
const source = { };
if (process.env.CI) { // ...if running on a CI/CD environment
source.hostname = "localhost";
source.maxInstances = 5;
source.path = "/wd/hub";
source.port = 4444;
source.services = ["selenium-standalone"];
} else {
source.filesToWatch = [];
}
exports.config = Object.assign({}, {
runner: "local",
bail: 0,
baseUrl: cfg.get("baseUrl"),
coloredLogs: true,
exclude: [
// "test/spec/**.spec.ts",
],
filesToWatch: [],
hostname: "localhost",
logLevel: "warn", // trace | debug | info | warn | error | silent
maxInstances: 1,
outputDir: `${__dirname}/logs/`,
path: "/",
port: 9515,
specs: ["./test/**/*",],
sync: true,
waitforTimeout: 25000,
framework: "mocha",
specFileRetries: 0,
mochaOpts: {
require: ["tsconfig-paths/register"],
timeout: 15000,
ui: "bdd",
},
capabilities: [
{
browserName: "chrome",
maxInstances: 5,
"goog:chromeOptions": {
args: [
"--disable-gpu",
"--headless",
"--no-sandbox",
"--test-type",
],
},
},
],
reporters: ["spec",],
services: ["chromedriver",],
before: function (capabilities, specs) {
require("ts-node").register({ files: true, transpileOnly: true });
},
// ...Cucumber specific hooks
}, source);
此外,我禁用了所有测试用例,并放了这样简单的东西:
browser.url(`https://duckduckgo.com/`);
$("#search_form_input_homepage").setValue("webdriverio");
$("#search_button_homepage").click();
const expected: string = "WebdriverIO · Next-gen WebDriver test framework for Node.js";
expect($("div#r1-0 h2.result__title a.result__a").getText()).to.contain(expected);
。尽管如此,相同的错误消息>:(
有什么线索吗?
TL;DR— 在容器级别定义/设置代理设置。
好吧,这是一个愚蠢的细节:我们使用公司代理。
我最初的想法是尝试在wdio.conf.js
中设置代理信息(在浏览器级别):
capabilities: [
{
browserName: "chrome",
maxInstances: 5,
"goog:chromeOptions": {
args: [
"--disable-gpu",
"--headless",
// "--no-sandbox",
"--test-type",
// "--window-size=1024x768",
],
},
proxy: {
httpProxy: "http://corporate.proxy:9080",
noProxy: ["127.0.0.1", "localhost", ".domain.tld",],
proxyType: "manual",
sslProxy: "http://corporate.proxy:9080",
},
},
],
。但这不起作用,所以我最终在容器级别(在 Docker Compose 文件中)进行了设置:
version: "3.7"
services:
chrome:
container_name: standalone-chrome
environment:
- CHROME_VERSION=76.0.3809 # ...make sure this is in sync with the chromedriver version in package.json
- HTTPS_PROXY=http://corporate.proxy:9080
- HTTP_PROXY=http://corporate.proxy:9080
- NO_PROXY=127.0.0.1,localhost,.domain.tld
- START_XVFB=false
healthcheck:
interval: 15s
retries: 3
timeout: 30s
test: "/opt/bin/check-grid.sh --host chrome --port 4444"
image: selenium/standalone-chrome:3.141.59-selenium
networks:
- global-network
ports:
- "4444:4444"
networks:
global-network:
driver: bridge
name: selenium-network
然后,我可以针对该 dockerized "独立" Chrome 实例或在 CI/CD 管道中通过以下方式运行测试:CI=docker npm run test
(映射为package.json
,例如:"scripts": { "test": "wdio" }
)。
希望这对将来的某人有所帮助...