WebdriverIO 测试不会 运行 相应地针对 dockerized Selenium 服务器

WebdriverIO tests don't run accordingly against dockerized Selenium server

我有一个带有一些测试用例的简单 WebdriverIO 项目。我 运行 一切 "locally"(没有 CI 根据我的 wdio.conf.js 设置声明的变量)在 headless 模式下 browserName: "chrome",和 Mocha 测试 运行ner.

现在,我正在尝试 运行 针对 Selenium 服务器(通过 Docker 启动)的完全相同的测试用例,但所有测试用例都失败并显示以下小错误消息:Timeout of 15000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

If I go to the Selenium Web console I can see the sessions being created and deleted, so the configuration should be OK.

// 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 — define/set 容器级别的代理设置。


好吧,这是一个愚蠢的细节:我们使用公司代理。

我最初的想法是尝试在 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 "standalone" Chrome 实例的测试,或者在 CI/CD 管道中通过: CI=docker npm run test (映射在 package.json 赞:"scripts": { "test": "wdio" }).

希望这对以后的人有所帮助...