webdriver | selenium query | Cell 9 | Search

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.

Cell 8

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;

What the code could have been:

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

Dependencies and Imports

Function: createWebdriverClient