Skip to content

Commit

Permalink
fix(cli): fix webdriver being passed from index.js (#33)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephen Mathieson <[email protected]>
  • Loading branch information
michael-siek and stephenmathieson authored Jun 9, 2020
1 parent c0aa15c commit 7c939d6
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 50 deletions.
2 changes: 1 addition & 1 deletion packages/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const version = require('./package.json').version;
const axeTestUrls = require('./lib/axe-test-urls');
const saveOutcome = require('./lib/save-outcome');
const utils = require('./lib/utils');
const { startDriver } = require('./webdriver');
const { startDriver } = require('./lib/webdriver');

program
.version(version)
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/lib/axe-test-urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

const WebDriver = require('selenium-webdriver');
const AxeBuilder = require('axe-webdriverjs');
const { stopDriver } = require('./webdriver');

function testPages(urls, config, events) {
const driver = config.driver;

const scriptTimeout = (config.timeout || 20) * 1000.0;
driver.manage().setTimeouts({ script: scriptTimeout });
// End of the line, no more page left
if (urls.length === 0) {
stopDriver(config);
driver.quit();
return Promise.resolve([]);
}

Expand Down
15 changes: 1 addition & 14 deletions packages/cli/lib/webdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ const chrome = require('selenium-webdriver/chrome');

function startDriver(config) {
let builder;
const scriptTimeout = (config.timeout || 20) * 1000.0;

if (config.browser === 'chrome-headless') {
// Tell selenium use the driver in node_modules
const service = new chrome.ServiceBuilder(
Expand All @@ -27,23 +25,12 @@ function startDriver(config) {
} else {
builder = new Builder().forBrowser(config.browser);
}

// Launch a browser
config.driver = builder.build();
config.builder = builder;

return config.driver
.manage()
.timeouts()
.setScriptTimeout(scriptTimeout)
.then(() => config);
}

function stopDriver(config) {
config.driver.quit();
}

module.exports = {
startDriver,
stopDriver
startDriver
};
5 changes: 4 additions & 1 deletion packages/cli/test/axe-test-urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ describe('testPages', function () {
wait: async arg => arg,
switchTo: () => ({ defaultContent: () => {} }),
findElements: async () => [],
quit: async arg => arg
quit: async arg => arg,
manage: () => ({
setTimeouts: async arg => arg
})
};
config = { driver: mockDriver };
});
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/test/integrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const chrome = require('selenium-webdriver/chrome');
const http = require('http');
const nodeStatic = require('node-static');
const axeTestUrls = require('../lib/axe-test-urls');
const { startDriver, stopDriver } = require('../lib/webdriver');
const { startDriver } = require('../lib/webdriver');

describe('integrations', function () {
let program, urls, server;
Expand Down Expand Up @@ -36,7 +36,7 @@ describe('integrations', function () {
});

afterEach(async () => {
stopDriver(program);
program.driver.quit()

const service = chrome.getDefaultService();
if (service.isRunning()) {
Expand Down
47 changes: 18 additions & 29 deletions packages/cli/test/webdriver.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const assert = require('chai').assert;
const { startDriver, stopDriver } = require('../lib/webdriver');
const { startDriver } = require('../lib/webdriver');
const chromedriver = require('chromedriver');
const chrome = require('selenium-webdriver/chrome');
const path = require('path');
Expand All @@ -18,7 +18,6 @@ describe('startDriver', () => {
});

afterEach(async () => {
stopDriver(config);
const service = chrome.getDefaultService();
if (service.isRunning()) {
await service.stop();
Expand Down Expand Up @@ -74,35 +73,25 @@ describe('startDriver', () => {
assert.equal(service.executable_, config.chromedriverPath);
});

it('sets the --headless flag with chrome-headless', async () => {
browser = 'chrome-headless';
const { builder } = await startDriver(config);
const capabilities = await builder.getCapabilities();
const chromeOptions = capabilities.get('chromeOptions');

assert.isObject(chromeOptions);
assert.deepEqual(chromeOptions.args, ['--headless']);
});
it('sets the --headless flag with chrome-headless', async () => {
browser = 'chrome-headless';
await startDriver(config);
const capabilities = await config.builder.getCapabilities();
const chromeOptions = capabilities.get('chromeOptions');

it('sets the --chrome-options flag with no-sandbox', async () => {
browser = 'chrome-headless';
config.chromeOptions = ['--no-sandbox'];
const { builder } = await startDriver(config);
const capabilities = await builder.getCapabilities();
const chromeOptions = capabilities.get('chromeOptions');
assert.isObject(chromeOptions);
assert.deepEqual(chromeOptions.args, ['--headless']);
});

assert.isArray(chromeOptions.args);
assert.deepEqual(chromeOptions.args, ['--headless', '--no-sandbox']);
});
});
it('sets the --chrome-options flag with no-sandbox', async () => {
browser = 'chrome-headless';
config.chromeOptions = ['--no-sandbox'];
await startDriver(config);
const capabilities = await config.builder.getCapabilities();
const chromeOptions = capabilities.get('chromeOptions');

describe('stopDriver', () => {
it('calls browser.quit', () => {
let called = 0;
stopDriver({
browser: 'chrome-headless',
driver: { quit: () => called++ }
});
assert.equal(called, 1);
});
assert.isArray(chromeOptions.args);
assert.deepEqual(chromeOptions.args, ['--headless', '--no-sandbox']);
});
});

0 comments on commit 7c939d6

Please sign in to comment.