Skip to content

Commit

Permalink
refactor: make tests respect CHROMEDRIVER_PATH (dequelabs#690)
Browse files Browse the repository at this point in the history
* refactor(webdriverjs): make code more DRY

* refactor: make tests respect CHROMEDRIVER_PATH
  • Loading branch information
not-my-profile authored Mar 20, 2023
1 parent d11481b commit b09ec5a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 20 deletions.
20 changes: 14 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,13 @@ jobs:
steps:
- checkout
- restore_dependency_cache
# required for browser-driver-manager
- run: sudo apt-get update -y
- run: cd packages/webdriverjs && npx browser-driver-manager@1.0.4 install chrome chromedriver --verbose
- browser-tools/install-chrome
- browser-tools/install-chromedriver
- run: npm --prefix=packages/webdriverjs run build
- run: npm run coverage --prefix=packages/webdriverjs
- run:
command: npm run coverage --prefix=packages/webdriverjs
environment:
CHROMEDRIVER_PATH: /usr/local/bin/chromedriver

webdriverjs-example:
<<: *defaults
Expand All @@ -169,7 +171,10 @@ jobs:
- browser-tools/install-chrome
- browser-tools/install-chromedriver
- run: npm --prefix=packages/webdriverjs run build
- run: npm run test --prefix=packages/webdriverjs/tests/example
- run:
command: npm run test --prefix=packages/webdriverjs/tests/example
environment:
CHROMEDRIVER_PATH: /usr/local/bin/chromedriver

webdriverio:
docker:
Expand All @@ -182,7 +187,10 @@ jobs:
- restore_dependency_cache
- browser-tools/install-chrome
- browser-tools/install-chromedriver
- run: npm run coverage --prefix=packages/webdriverio
- run:
command: npm run coverage --prefix=packages/webdriverio
environment:
CHROMEDRIVER_PATH: /usr/local/bin/chromedriver

reporter-earl:
<<: *defaults
Expand Down
2 changes: 1 addition & 1 deletion packages/webdriverio/src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('@axe-core/webdriverio', () => {
let chromedriverProcess: ChildProcessWithoutNullStreams;

before(async () => {
const path = process.env.CI ? '/usr/local/bin/chromedriver' : chromedriver.path;
const path = process.env.CHROMEDRIVER_PATH ?? chromedriver.path;
chromedriverProcess = child_process.spawn(path, [`--port=${port}`]);
chromedriverProcess.stdout.pipe(process.stdout);
chromedriverProcess.stderr.pipe(process.stderr);
Expand Down
13 changes: 11 additions & 2 deletions packages/webdriverjs/tests/axe-webdriverjs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import { Server, createServer } from 'http';
import { Webdriver, connectToChromeDriver } from './test-utils';
import AxeBuilder from '../src';
import { axeRunPartial } from '../src/browser';
import child_process from 'child_process';
import { ChildProcessWithoutNullStreams } from 'child_process';

const dylangConfig = JSON.parse(
fs.readFileSync(
require.resolve('./fixtures/external/dylang-config.json'),
Expand All @@ -29,6 +32,8 @@ describe('@axe-core/webdriverjs', () => {
let axeForceLegacy: string;
let axeLargePartial: string;

let chromedriverProcess: ChildProcessWithoutNullStreams;

before(async () => {
const axePath = require.resolve('axe-core');
axeSource = fs.readFileSync(axePath, 'utf8');
Expand All @@ -46,13 +51,17 @@ describe('@axe-core/webdriverjs', () => {
'utf8'
);

chromedriver.start([`--port=${port}`]);
const binPath = process.env.CHROMEDRIVER_PATH ?? chromedriver.path;
chromedriverProcess = child_process.spawn(binPath, [`--port=${port}`]);
chromedriverProcess.stdout.pipe(process.stdout);
chromedriverProcess.stderr.pipe(process.stderr);

await delay(500);
await connectToChromeDriver(port);
});

after(() => {
chromedriver.stop();
chromedriverProcess.kill();
});

beforeEach(async () => {
Expand Down
13 changes: 10 additions & 3 deletions packages/webdriverjs/tests/example/webdriverjs-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ describe('@axe-core/webdriverjs example', () => {
});

beforeEach(async () => {
driver = new Builder()
const builder = new Builder()
.forBrowser('chrome')
.setChromeOptions(new chrome.Options().headless())
.build();
.setChromeOptions(new chrome.Options().headless());

if (process.env.CHROMEDRIVER_PATH) {
builder.setChromeService(
new chrome.ServiceBuilder(process.env.CHROMEDRIVER_PATH)
);
}

driver = builder.build();
});

afterEach(() => {
Expand Down
19 changes: 11 additions & 8 deletions packages/webdriverjs/tests/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ import net from 'net';
import chrome from 'selenium-webdriver/chrome';

export const Webdriver = (): WebDriver => {
let webdriver: WebDriver;
let builder: Builder;
if (process.env.REMOTE_SELENIUM_URL) {
webdriver = new Builder()
builder = new Builder()
.forBrowser('chrome')
.usingServer(process.env.REMOTE_SELENIUM_URL)
.setChromeOptions(new chrome.Options().headless())
.build();
.setChromeOptions(new chrome.Options().headless());
} else {
webdriver = new Builder()
builder = new Builder()
.setChromeOptions(new chrome.Options().headless())
.forBrowser('chrome')
.build();
.forBrowser('chrome');
}
if (process.env.CHROMEDRIVER_PATH) {
builder.setChromeService(
new chrome.ServiceBuilder(process.env.CHROMEDRIVER_PATH)
);
}
return webdriver;
return builder.build();
};

export const connectToChromeDriver = (port: number): Promise<void> => {
Expand Down

0 comments on commit b09ec5a

Please sign in to comment.