Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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.

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.

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:

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.

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

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

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

// 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
});
};
like image 855
Evolk Avatar asked Jul 11 '14 06:07

Evolk


People also ask

Does karma use selenium?

If you are using javascript to write unit tests - via jasmine/mocha/chai or equivalent then - the argument that karma relies heavily on javascript and selenium doesn't - is redundant.

What is the default port for Selenium Grid?

To start nodes open the command prompt and navigate to the directory, where the Selenium Server Standalone jar file is stored. When -role option that is provided is not specified, and it is not the hub, the default port is 5555.


1 Answers

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.

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

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

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

So the package.json contents looks like:-

{
  "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!

like image 97
Evolk Avatar answered Sep 20 '22 21:09

Evolk