The code requires two modules: ../Core
and webdriverio
, and imports specific functions and constants from ../Core
. It defines a function createWebdriverClient
that creates a WebdriverIO client configuration object with various settings and capabilities, including browser name, Chrome-specific options, and log level.
var importer = require('../Core');
var {remote} = require('webdriverio');
var {
getSessions,
onlyOneWindow,
getAllSessionUrls,
} = importer.import("only one window",
"get all session urls",
"manage webdriver sessions")
var MAX_SESSIONS = 4;
//var MAX_SESSIONS = 36;
function createWebdriverClient(host, port) {
var webdriverServer = {
services: ['selenium-standalone', 'chromedriver'],
sync: false,
debug: false,
host: host || 'localhost',
port: port || 4444,
logLevel: 'silent',
baseUrl: 'https://webdriver.io',
pageLoadStrategy: 'eager',
connectionRetryTimeout: 1000,
capabilities: {
browserName: 'chrome',
'goog:chromeOptions': {
prefs: {
'download.default_directory': '/data/downloads',
'profile.default_content_setting_values.notifications': 2,
'exited_cleanly': true,
'exit_type': 'None'
},
args: [
// We stopped using sessions here because it injects the session using the API below
// TODO: https://superuser.com/questions/461035/disable-google-chrome-session-restore-functionality
//'user-data-dir=/tmp/profile-' + MAX_SESSIONS + 1,
// 'start-fullscreen',
'no-sandbox',
'disable-session-crashed-bubble',
'disable-infobars',
'new-window',
'disable-geolocation',
'disable-notifications',
'show-saved-copy',
'silent-debugger-extension-api'
//'kiosk'
]
}
},
};
//console.log('deleting webdriver from cache');
//Object.keys(require.cache).filter(k => k.includes('webdriver') || k.includes('wdio'))
// .forEach(k => delete require.cache[k]);
var promise = remote(webdriverServer);
var client;
//remote.on('error', e => console.log(e.message));
//remote.on('end', () => console.log('Daemon: Closing browser'));
const connectSession = importer.import("connect webdriver session");
return promise
.then(r => client = r)
.then(() => connectSession(client))
.then(() => getSessions(client))
.then(() => onlyOneWindow(client))
.then(() => getAllSessionUrls(client))
.catch(e => {
console.log(e);
isError = e;
throw new Error('there is an error with the client ' + e);
})
.then(() => client);
}
module.exports = createWebdriverClient;
const { importFunction } = require('../Core');
const { remote } = require('webdriverio');
const MAX_SESSIONS = 4;
const DEFAULT_HOST = 'localhost';
const DEFAULT_PORT = 4444;
const DEFAULT_BROWSER = 'chrome';
const createWebdriverClient = (host = DEFAULT_HOST, port = DEFAULT_PORT, browser = DEFAULT_BROWSER) => {
const capabilities = {
browserName: browser,
'goog:chromeOptions': {
prefs: {
'download.default_directory': '/data/downloads',
'profile.default_content_setting_values.notifications': 2,
'exited_cleanly': true,
'exit_type': 'None'
},
args: [
'no-sandbox',
'disable-session-crashed-bubble',
'disable-infobars',
'new-window',
'disable-geolocation',
'disable-notifications',
'show-saved-copy',
'silent-debugger-extension-api',
]
}
};
const webdriverServer = {
services: ['selenium-standalone', 'chromedriver'],
sync: false,
debug: false,
host,
port,
logLevel:'silent',
baseUrl: 'https://webdriver.io',
pageLoadStrategy: 'eager',
connectionRetryTimeout: 1000,
capabilities
};
const connectSession = importFunction('connect webdriver session');
return remote(webdriverServer)
.then(client => {
console.log('Connecting to webdriver session...');
return connectSession(client);
})
.then(() => getSessions(client))
.then(() => onlyOneWindow(client))
.then(() => getAllSessionUrls(client))
.catch(e => {
console.error(`Error creating webdriver client: ${e.message}`);
throw e;
})
.then(() => client);
};
module.exports = createWebdriverClient;
Code Breakdown
../Core
: a custom module containing utility functionswebdriverio
: a Node.js driver for Selenium WebDriver../Core
module:
getSessions
onlyOneWindow
getAllSessionUrls
MAX_SESSIONS
with a value of 4 (or 36 in a commented-out line)host
and port
, which default to localhost
and 4444
respectivelyservices
: an array of services to use (in this case, Selenium-Standalone and ChromeDriver)sync
: a boolean indicating whether the client should wait for the test to complete (set to false
)debug
: a boolean indicating whether to enable debug logging (set to false
)host
and port
: the host and port to use for the clientlogLevel
: the log level to use (set to silent
)baseUrl
: the base URL for the client (set to https://webdriver.io
)pageLoadStrategy
: the page load strategy to use (set to eager
)connectionRetryTimeout
: the timeout for retrying connections (set to 1000
ms)capabilities
: an object containing browser capabilities, including:
browserName
: the name of the browser to use (set to chrome
)goog:chromeOptions
: an object containing Chrome-specific options
prefs
: an object containing Chrome preferences, including:
download.default_directory
: the directory to use for downloads (set to /data/downloads
)profile.default_content_setting_values.notifications
: the notification setting for the profile (set to 2
)exited_cleanly
: a boolean indicating whether the browser should exit cleanly (set to true
)exit_type
: the exit type to use (set to None
)args
: an array of arguments to pass to the Chrome browser, including:
no-sandbox
: a flag indicating that the browser should not run in a sandboxed environmentdisable-session-crashed-bubble
: a flag indicating that the browser should not display a crashed session bubbledisable-infobars
: a flag indicating that the browser should not display infobarsnew-window
: a flag indicating that the browser should open a new windowdisable-geolocation
: a flag indicating that the browser should not use geolocationdisable-notifications
: a flag indicating that the browser should not display notificationsshow-saved-copy
: a flag indicating that the browser should show the saved copy of the pagesilent-debugger-extension-api
: a flag indicating that the browser should use the silent debugger extension API