Skip to content

Commit

Permalink
fix(webdriverjs): prevent selnium undefined -> null transformation (#402
Browse files Browse the repository at this point in the history
)
  • Loading branch information
AdnoC committed Nov 29, 2021
1 parent a436e52 commit be3912d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"html"
],
"statements": 95,
"branches": 93,
"branches": 92,
"functions": 100,
"lines": 95,
"exclude": [
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/lib/axe-test-urls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('testPages', function () {
let mockDriver: any;

beforeEach(() => {
const func = async (arg: any) => arg;
const func = async (arg: any) => '{}';
mockDriver = {
get: func,
executeAsyncScript: func,
Expand Down Expand Up @@ -49,7 +49,7 @@ describe('testPages', function () {

mockDriver.executeAsyncScript = async (script: string) => {
asyncScripts.push(script);
return script;
return '{}';
};
mockDriver.wait = async (script: string) => {
waitCalls++;
Expand Down Expand Up @@ -82,7 +82,7 @@ describe('testPages', function () {
const asyncScripts: string[] = [];
mockDriver.executeAsyncScript = async (script: string) => {
asyncScripts.push(script);
return script;
return '{}';
};

await testPages(['http://foo'], config);
Expand Down
29 changes: 19 additions & 10 deletions packages/webdriverjs/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export function axeRunPartial(
driver: WebDriver,
context: ContextObject,
options: RunOptions
): Promise<PartialResult> {
): Promise<string> {
return promisify(
driver.executeAsyncScript<PartialResult>(`
driver.executeAsyncScript<string>(`
var callback = arguments[arguments.length - 1];
var context = ${JSON.stringify(context)} || document;
var options = ${JSON.stringify(options)} || {};
window.axe.runPartial(context, options).then(callback);
window.axe.runPartial(context, options).then(res => JSON.stringify(res)).then(callback);
`)
);
}
Expand All @@ -58,13 +58,15 @@ export function axeFinishRun(
driver: WebDriver,
axeSource: string,
config: Spec | null,
partialResults: Array<PartialResult | null>,
partialResults: Array<string>,
options: RunOptions
): Promise<AxeResults> {
// Inject source and configuration a second time with a mock "this" context,
// to make it impossible to sniff the global window.axe for results.
return promisify(
driver.executeAsyncScript<AxeResults>(`
driver
.executeAsyncScript<string>(
`
var callback = arguments[arguments.length - 1];
${axeSource};
Expand All @@ -77,9 +79,12 @@ export function axeFinishRun(
}
var partialResults = ${JSON.stringify(partialResults)};
partialResults = partialResults.map(res => JSON.parse(res));
var options = ${JSON.stringify(options || {})};
window.axe.finishRun(partialResults, options).then(callback);
`)
window.axe.finishRun(partialResults, options).then(res => JSON.stringify(res)).then(callback);
`
)
.then(res => JSON.parse(res))
);
}

Expand Down Expand Up @@ -110,16 +115,20 @@ export function axeRunLegacy(
// https://github.com/vercel/pkg/issues/676
// we need to pass a string vs a function so we manually stringified the function
return promisify(
driver.executeAsyncScript<AxeResults>(`
driver
.executeAsyncScript<string>(
`
var callback = arguments[arguments.length - 1];
var context = ${JSON.stringify(context)} || document;
var options = ${JSON.stringify(options)} || {};
var config = ${JSON.stringify(config)} || null;
if (config) {
window.axe.configure(config);
}
window.axe.run(context, options).then(callback);
`)
window.axe.run(context, options).then(res => JSON.stringify(res)).then(callback);
`
)
.then(res => JSON.parse(res))
);
}

Expand Down
8 changes: 4 additions & 4 deletions packages/webdriverjs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,13 @@ class AxeBuilder {
private async runPartialRecursive(
context: ContextObject,
initiator = false
): Promise<PartialResults> {
): Promise<string[]> {
if (!initiator) {
await axeSourceInject(this.driver, this.axeSource, this.config);
}
// IMPORTANT: axeGetFrameContext MUST be called before axeRunPartial
const frameContexts = await axeGetFrameContext(this.driver, context);
const partials: PartialResults = [
const partials: string[] = [
await axeRunPartial(this.driver, context, this.option)
];

Expand All @@ -227,7 +227,7 @@ class AxeBuilder {
if (switchedFrame) {
await this.driver.switchTo().parentFrame();
}
partials.push(null);
partials.push('null');
}
}
return partials;
Expand All @@ -236,7 +236,7 @@ class AxeBuilder {
/**
* Use axe.finishRun() to turn partial results into actual results
*/
private async finishRun(partials: PartialResults): Promise<AxeResults> {
private async finishRun(partials: string[]): Promise<AxeResults> {
const { driver, axeSource, config, option } = this;

const win = await driver.getWindowHandle();
Expand Down
23 changes: 23 additions & 0 deletions packages/webdriverjs/tests/axe-webdriverjs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as fs from 'fs';
import { Server, createServer } from 'http';
import { Webdriver, connectToChromeDriver } from './test-utils';
import AxeBuilder from '../src';
import { axeRunPartial } from '../src/browser';
const dylangConfig = require('./fixtures/external/dylang-config.json') as Spec;

describe('@axe-core/webdriverjs', () => {
Expand Down Expand Up @@ -68,6 +69,16 @@ describe('@axe-core/webdriverjs', () => {
assert.isArray(results.inapplicable);
});

it('handles undefineds', async () => {
await driver.get(`${addr}/external/index.html`);
const results = await new AxeBuilder(driver).analyze();
assert.isNotNull(results);
assert.isArray(results.violations);
assert.isArray(results.incomplete);
assert.isArray(results.passes);
assert.isArray(results.inapplicable);
});

it('returns correct results metadata', async () => {
await driver.get(`${addr}/index.html`);
const results = await new AxeBuilder(driver).analyze();
Expand Down Expand Up @@ -539,6 +550,18 @@ describe('@axe-core/webdriverjs', () => {
});
});

describe('browser functions', () => {
it('serializes results', async () => {
await driver.get(`${addr}/external/nested-iframes.html`);
await driver.executeScript(`
window.axe = {
runPartial: (c, o) => Promise.resolve({ violations: [], passes: [] })
};
`);
const res = await axeRunPartial(driver, null as any, null as any);
assert.equal(typeof res, 'string');
});
});
describe('for versions without axe.runPartial', () => {
let axe403Source: string;
before(() => {
Expand Down

0 comments on commit be3912d

Please sign in to comment.