Skip to content

Commit

Permalink
fix(webdriverio): use executeAsync() vs execute() (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-siek committed Aug 31, 2021
1 parent b9743c2 commit 0e4aa3a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
4 changes: 1 addition & 3 deletions packages/webdriverio/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,8 @@ export default class AxeBuilder {
this.excludes,
this.disableFrameSelectors
);
// https://github.com/webdriverio/webdriverio/issues/6607#issuecomment-808457664
await client.switchToFrame(null);

const { runPartialSupported } = await axeSourceInject({
const runPartialSupported = await axeSourceInject({
client,
axeSource
});
Expand Down
28 changes: 14 additions & 14 deletions packages/webdriverio/src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ describe('@axe-core/webdriverio', () => {
it('throws when a setup fails', async () => {
let error: Error | null = null;

const brokenSource = axeSource + `;window.axe.utils = {}`;
const brokenSource = axeSource + `;window.axe.utils = {};`;
await client.url(`${addr}/index.html`);
try {
await new AxeBuilder({ client, axeSource: brokenSource })
Expand All @@ -366,9 +366,9 @@ describe('@axe-core/webdriverio', () => {
it('properly isolates the call to axe.finishRun', async () => {
let error: Error | null = null;

await client.url(`${addr}/isolated-finish.html`)
await client.url(`${addr}/isolated-finish.html`);
try {
await new AxeBuilder({ client }).analyze()
await new AxeBuilder({ client }).analyze();
} catch (e) {
error = e;
}
Expand All @@ -379,17 +379,17 @@ describe('@axe-core/webdriverio', () => {
it('returns correct results metadata', async () => {
await client.url(`${addr}/index.html`);
const results = await new AxeBuilder({ client }).analyze();
assert.isDefined(results.testEngine.name)
assert.isDefined(results.testEngine.version)
assert.isDefined(results.testEnvironment.orientationAngle)
assert.isDefined(results.testEnvironment.orientationType)
assert.isDefined(results.testEnvironment.userAgent)
assert.isDefined(results.testEnvironment.windowHeight)
assert.isDefined(results.testEnvironment.windowWidth)
assert.isDefined(results.testRunner.name)
assert.isDefined(results.toolOptions.reporter)
assert.equal(results.url, `${addr}/index.html`)
})
assert.isDefined(results.testEngine.name);
assert.isDefined(results.testEngine.version);
assert.isDefined(results.testEnvironment.orientationAngle);
assert.isDefined(results.testEnvironment.orientationType);
assert.isDefined(results.testEnvironment.userAgent);
assert.isDefined(results.testEnvironment.windowHeight);
assert.isDefined(results.testEnvironment.windowWidth);
assert.isDefined(results.testRunner.name);
assert.isDefined(results.toolOptions.reporter);
assert.equal(results.url, `${addr}/index.html`);
});
});

describe('disableFrame', () => {
Expand Down
19 changes: 12 additions & 7 deletions packages/webdriverio/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,16 @@ export const axeSourceInject = async ({
axeSource
}: AxeSourceInjectParams): Promise<AxeSourceInjectResponse> => {
return promisify(
client.execute(`
// Had to use executeAsync() because we could not use multiline statements in client.execute()
// we were able to return a single boolean in a line but not when assigned to a variable.
client.executeAsync(`
var callback = arguments[arguments.length - 1];
${axeSource};
window.axe.configure({
branding: { application: 'webdriverio' }
});
var runPartial = typeof window.axe.runPartial === 'function';
return { runPartialSupported: runPartial };
callback(runPartial);
`)
);
};
Expand All @@ -130,15 +132,19 @@ export const axeGetFrameContext = ({
context
}: AxeGetFrameContextParams): Promise<any[]> => {
return promisify(
client.execute(`
// Had to use executeAsync() because we could not use multiline statements in client.execute()
// we were able to return a single boolean in a line but not when assigned to a variable.
client.executeAsync(`
var callback = arguments[arguments.length - 1];
var context = ${JSON.stringify(context)};
var frameContexts = window.axe.utils.getFrameContexts(context);
return frameContexts.map(function (frameContext) {
frameContexts = frameContexts.map(function (frameContext) {
return Object.assign(frameContext, {
href: window.location.href, // For debugging
frame: axe.utils.shadowSelect(frameContext.frameSelector)
});
});
callback(frameContexts)
`)
);
};
Expand All @@ -163,15 +169,14 @@ export const axeRunLegacy = ({
);
};

export const openAboutBlank = (client: BrowserObject) => {
export const openAboutBlank = (client: BrowserObject): Promise<void> => {
return promisify(
client.execute(`
window.open('about:blank', '_blank');
`)
);
};


export const axeFinishRun = ({
client,
axeSource,
Expand Down

0 comments on commit 0e4aa3a

Please sign in to comment.