Karma:无法使用karma-webdriver-launcher启动浏览器

时间:2019-10-21 07:25:14

标签: javascript selenium-webdriver webdriver karma-runner karma-mocha

使用karma-webdriver-launcher和&karma-selenium-grid-launcher尝试了Karma,以启动chrome / firefox执行测试,但无法打开浏览器

有人可以分享使用硒网络驱动程序和业力进行e2e测试的工作代码吗,在Google上进行了搜索,但仅在这些代码段中找不到完整的代码。

我的业力配置文件::

 module.exports = function (config) {
var webdriverConfig = {
    hostname: 'localhost',
    port: 4444,
  }


let customLaunchers = {

    firefoxCustom: {
        base: 'WebDriver',
        config: webdriverConfig,
        browserName: 'firefox',
        version:'ANY',
        platform:'ANY'
      },

};

config.set({
  basePath: './',
  frameworks: ["mocha"],
  reporters: ['progress'],

  plugins: [
    'karma-webdriver-launcher',
  'karma-selenium-grid-launcher',
  'karma-mocha',
  'selenium-webdriver'
],


  customLaunchers: customLaunchers,
  browsers: [ 'firefoxCustom'],
  files: [
    "tests/*.spec.js"
  ],
  singleRun: true
});
}

测试文件::

const {Builder, By, until} = require('selenium-webdriver');

(async function example() {
const driver = await new Builder().forBrowser('firefox').build();

try {
    await driver.get('https://www.google.com');
    await driver.findElement(By.name('q')).sendKeys('hello');
    await driver.findElement(By.id('tsf')).submit();
}catch(err)
{
    console.log(err)
} 
finally {
    await driver.quit();
}
})();

package.json

{
 "name": "karmatest2",
 "version": "1.0.0",
 "main": "index.js",
 "scripts": {
  "test": ""
 },
 "author": "",
 "license": "ISC",
 "description": "",
 "devDependencies": {
 "chai": "^4.2.0",
 "chromedriver": "^77.0.0",
 "geckodriver": "^1.19.0",
 "karma": "^4.4.1",
 "karma-chai": "^0.1.0",
 "karma-chrome-launcher": "^3.1.0",
 "karma-firefox-launcher": "^1.2.0",
 "karma-ie-launcher": "^1.0.0",
 "karma-jasmine": "^2.0.1",
 "karma-mocha": "^1.3.0",
 "karma-selenium-grid-launcher": "^0.3.0",
 "karma-webdriver-launcher": "^1.0.7",
 "mocha": "^6.2.2",
 "require": "^2.4.20",
 "selenium-webdriver": "^3.6.0",
 "wd": "^1.11.4"
 } 
 }

获取错误:

 21 10 2019 11:53:49.329:INFO [karma-server]: Karma v4.4.1 server started at 
 http://0.0.0.0:9876/
 21 10 2019 11:53:49.332:INFO [launcher]: Launching browsers firefoxCustom with concurrency 
 unlimited
 21 10 2019 11:53:49.335:INFO [launcher]: Starting browser firefox via Remote WebDriver
 21 10 2019 11:53:49.374:ERROR [WebDriver]: WebDriver command failed {
 spec: {
   platform: 'ANY',
   testName: 'Karma test',
   tags: [],
   version: 'ANY',
   base: 'WebDriver',
   browserName: 'firefox'
  },
 error: Error: [get("http://localhost:9876/?id=18408460")] Error response status: 6 Selenium 
 error: No active session with ID url

1 个答案:

答案 0 :(得分:0)

我通过Docker在Selenium和Standalone Chrome上运行。相同的方法也可以在Docker外部运行,但这意味着您自己设置Chrome,Chromedriver和Selenium Hub,这本身就很麻烦。

karma.conf.js

module.exports = function karmaConfig(config) {

    config.set({
        frameworks: ['jasmine'],
        files: [
            // src files
            // spec files
        ],
        exclude: [],
        preprocessors: {},
        reporters: ['dots'],
        port: 9876,
        logLevel: config.LOG_INFO,
        autoWatch: false,
        singleRun: true,

        // RELEVANT BIT
        browsers: ['ChromeWebDriver'],
        customLaunchers: {
            ChromeWebDriver: {
                base: 'WebDriver',
                browserName: 'chrome',
                version: 'ANY',
                platform: 'ANY',
            }
        },
        captureTimeout: 60000,
        // END RELEVANT BIT
    });

};

package.json(可能有点过时):

"karma": "1.3.0",
"karma-jasmine": "0.1.6",
"karma-webdriver-launcher": "1.0.7",

我在主机网络docker run -d --network=host selenium/standalone-chrome:3上使用Docker运行Selenium,这暴露了http://127.0.0.1:4444/wd/hub。然后,我等到Selenium Hub响应后再启动karma.conf.js。

日志:

> karma start tests/javascript/karma.conf.js

20 05 2020 18:42:52.289:INFO [karma]: Karma v1.3.0 server started at http://localhost:9876/
20 05 2020 18:42:52.298:INFO [launcher]: Launching browser ChromeWebDriver with unlimited concurrency
20 05 2020 18:42:52.317:INFO [launcher]: Starting browser chrome via Remote WebDriver
20 05 2020 18:42:56.024:INFO [Chrome 81.0.4044 (Linux 0.0.0)]: Connected on socket /#BeUqePvSlEC_aYzHAAAA with id 6615338
............................................................
Chrome 81.0.4044 (Linux 0.0.0): Executed 60 of 60 SUCCESS (0.67 secs / 0 secs)
20 05 2020 18:42:57.874:INFO [WebDriver]: Killed Karma test.
2020/05/20 18:42:57 Command finished successfully.

我没有设置customLauncher.ChromeWebDriver.config,因为我匹配了默认的Selenium Hub位置。 This also matches the default in wd,这是karma-webdriver-launcher使用的软件包。