javascript 如何让 karma webdriver 启动器使用我的 selenium 服务器/网格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24691790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:15:15  来源:igfitidea点击:

How do I get karma webdriver launcher to use my selenium server/grid

javascriptnode.jsseleniumkarma-runnerselenium-grid

提问by Evolk

Can anyone help shed some light on to what is stopping Karma javascript test runner from connecting to and using my selenium grid/server?

任何人都可以帮助阐明阻止 Karma javascript 测试运行器连接和使用我的 selenium 网格/服务器的原因吗?

I have a successfully working selenium grid environment that I already use with python selenium bindings for web application system testing. I'm running Selenium Server v.2.34.0 currently and it has 4 separate grid nodes connected to it.

我有一个成功运行的 selenium 网格环境,我已经将它与 python selenium 绑定一起用于 Web 应用程序系统测试。我目前正在运行 Selenium Server v.2.34.0,它有 4 个独立的网格节点连接到它。

I want to also leverage and reuse this resource for javascript testing against multiple browsers. Specifically I'm using the node.js based Karma test runner executing jasmine based unit tests. I've installed the "karma-webdriver-launcher" plugin. I can run my javascript tests with karma locally spawning firefox, chrome or IE browsers just fine.

我还想利用和重用此资源对多个浏览器进行 javascript 测试。具体来说,我正在使用基于 node.js 的 Karma 测试运行程序执行基于 jasmine 的单元测试。我已经安装了“ karma-webdriver-launcher”插件。我可以使用 karma 在本地生成 firefox、chrome 或 IE 浏览器来运行我的 javascript 测试。

When I try to use the remote selenium server to use a browser from the pool/farm, it fails to find a browser and I get the following warning output:

当我尝试使用远程 selenium 服务器使用池/场中的浏览器时,它无法找到浏览器,并且我收到以下警告输出:

DEBUG [config]: autoWatch set to false, because of singleRun
DEBUG [plugin]: Loading karma-* from C:\nodedebug\itpt\node_modules
DEBUG [plugin]: Loading plugin C:\nodedebug\itpt\node_modules/karma-chrome-launcher.
DEBUG [plugin]: Loading plugin C:\nodedebug\itpt\node_modules/karma-firefox-launcher.
DEBUG [plugin]: Loading plugin C:\nodedebug\itpt\node_modules/karma-html-reporter.
DEBUG [plugin]: Loading plugin C:\nodedebug\itpt\node_modules/karma-ie-launcher.
DEBUG [plugin]: Loading plugin C:\nodedebug\itpt\node_modules/karma-jasmine.
DEBUG [plugin]: Loading plugin C:\nodedebug\itpt\node_modules/karma-phantomjs-launcher.
DEBUG [plugin]: Loading plugin C:\nodedebug\itpt\node_modules/karma-typescript-preprocessor.
DEBUG [plugin]: Loading plugin C:\nodedebug\itpt\node_modules/karma-webdriver-launcher.
DEBUG [plugin]: Loading inlined plugin (defining launcher:firefox).
INFO [karma]: Karma v0.12.16 server started at http://localhost:9876/
INFO [launcher]: Starting browser firefox via Remote WebDriver

WARN [launcher]: firefox via Remote WebDriver have not captured in 60000 ms, killing.
Killed Karma test.

When I runtime debug the karma-webdriver-launcher stepping through it, it seems to fail in the wd request. My node javascript skills are only at a beginner level though so I might be missing something obvious. All the config details seemed to be getting passed correctly though and the url used for connecting to the selenium server look correct to me.

当我运行时调试 karma-webdriver-launcher 单步执行时,它似乎在 wd 请求中失败。我的 node javascript 技能只是初级水平,所以我可能会遗漏一些明显的东西。尽管所有配置细节似乎都被正确传递,并且用于连接到 selenium 服务器的 url 对我来说看起来是正确的。

It fails inside this call of karma-webdriver-launcher\node_modules\wd\lib\webdriver.js on line 33,

它在第 33 行的 karma-webdriver-launcher\node_modules\wd\lib\webdriver.js 调用中失败,

this._request(httpOpts, function(err, res, data) {

Here's my karma.config.js file:-

这是我的 karma.config.js 文件:-

// Karma configuration
module.exports = function (config) {
var webdriverConfig = {
  hostname: '172.17.126.52',
  port: 9625
}
config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',

// frameworks to use
frameworks: ['jasmine'],

// list of files / patterns to load in the browser
files: [
    'Scripts/Libs/JsResources.js',

  // watch every ts file
  'Scripts/Local/**/*.ts'
],

// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
    '**/*.ts': ['typescript']
},

typescriptPreprocessor: {
    // options passed to the typescript compiler
    options: {
        target: 'ES5', // (optional) Specify ECMAScript target version: 'ES3' (default), or 'ES5'
    }
},


// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['dots', 'html'],

// web server port
port: 9876,

// enable / disable colors in the output (reporters and logs)
colors: true,

// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_DEBUG,

// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,

customLaunchers: {
    'firefox': {
        base: 'WebDriver',
        config: webdriverConfig,
        browserName: 'firefox',
    }
},
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['firefox'],

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true
});
};

回答by Evolk

I've got it working. Two problems had to be fixed from my initial post.

我已经开始工作了。必须从我最初的帖子中解决两个问题。

1st Fix: Inside the karma.conf.js file I had to set the hostname to the IP address of the machine that was running karma (i.e. my local machine). Don't set this to the IP address of the selenium server grid hub.

第一个修复:在 karma.conf.js 文件中,我必须将主机名设置为运行 karma 的机器(即我的本地机器)的 IP 地址。不要将其设置为 selenium 服务器网格集线器的 IP 地址。

config.set({
...
hostname: '172.123.123.123',
...
})

2nd Fix: My karma project package.json file was missing this line in the devDependencies dictionary.

第二个修复:我的 karma 项目 package.json 文件在 devDependencies 字典中缺少这一行。

"karma-webdriver-launcher": "~0.2.0",

So the package.json contents looks like:-

所以 package.json 内容看起来像:-

{
  "name": "MyKarmaProject",
  "devDependencies": {
    "karma": "~0.12.16",
    "karma-chrome-launcher": "~0.1.4",
    "karma-firefox-launcher": "~0.1.3",
    "karma-html-reporter": "^0.2.3",
    "karma-ie-launcher": "~0.1.5",
    "karma-webdriver-launcher": "~0.2.0",
    "karma-jasmine": "^0.2.2",
    "karma-phantomjs-launcher": "^0.1.4",
    "karma-typescript-preprocessor": "0.0.7",
    "phantomjs": "^1.9.7-12"
  }
}

I believe if you run from your project directory cmd prompt "npm install" after the package.json file is update it will ensure everything is downloaded and installed correctly. Once this was done the karma runner was able to connect to my selenium grid server hub and request the appropriate browser node from the pool. I hope this question and answer helps someone else in the future!

我相信如果你在 package.json 文件更新后从你的项目目录 cmd 提示符“npm install”运行,它将确保所有东西都被正确下载和安装。完成此操作后,karma runner 能够连接到我的 selenium 网格服务器集线器并从池中请求适当的浏览器节点。我希望这个问题和答案在未来可以帮助其他人!