1
votes

Tried Karma with karma-webdriver-launcher && karma-selenium-grid-launcher to launch chrome/firefox to execute tests but not able to open browser

Can someone please share working code for e2e test using selenium webdriver and karma, searched on google but not able to find complete code only snippets are there.

My karma config file ::

 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
});
}

test file ::

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"
 } 
 }

getting error :

 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

1 Answers

1
votes

I got it working with Selenium and Standalone Chrome using Docker.

Note: The same config should work outside Docker, but it means setting up Chrome, Chromedriver and Selenium Hub yourself. Especially Selenium Hub (java) is a hassle.

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 (bit outdated probably):

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

I run Selenium using Docker on the host network: docker run -d --network=host selenium/standalone-chrome:3, this exposes http://127.0.0.1:4444/wd/hub on the host machine. Then I wait until Selenium Hub is responsive before starting karma.conf.js.

log:

> 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.

I don't set customLauncher.ChromeWebDriver.config because I'm matching the default Selenium Hub location. This also matches the default in wd, which is the package that karma-webdriver-launcher uses.