I attempted to use Karma along with karma-webdriver-launcher and karma-selenium-grid-launcher in order to launch Chrome/Firefox for test execution, but I am encountering issues with browser opening.
Could someone kindly provide me with a working code snippet for end-to-end testing using Selenium Webdriver and Karma? I have searched online but only found partial code snippets.
This is my Karma configuration 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
});
}
This is my 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();
}
})();
This is my package.json file:
{
"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",
...
}
}
I am encountering the following error:
21 10 2019 11:53:49.329:INFO [karma-server]: Karma v4.4.1 server started at http://0.0.0.0:9876/
...