Skip to content

Commit

Permalink
feat: emulate automation mode (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Mar 1, 2022
1 parent da013bd commit 6caa57a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"mime": "3.0.0",
"mocha": "9.2.0",
"prettier": "2.5.1",
"puppeteer": "13.1.3",
"puppeteer": "13.4.1",
"rimraf": "3.0.2",
"standard-version": "git+https://github.com:conventional-changelog/standard-version.git#v9.4.0",
"ts-node": "10.4.0",
Expand Down
18 changes: 18 additions & 0 deletions src/PuppeteerRunnerExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ export class PuppeteerRunnerExtension extends RunnerExtension {
this.timeout = opts?.timeout || 5000;
}

async #ensureAutomationEmulatation(pageOrFrame: Page | Frame) {
try {
await pageOrFrame
.client()
.send('Emulation.setAutomationOverride', { enabled: true });
} catch {
// ignore errors as not all versions support this command.
}
}

async runStep(step: Step, flow: UserFlow): Promise<void> {
const timeout = step.timeout || this.timeout;
const page = this.page;
Expand All @@ -58,6 +68,8 @@ export class PuppeteerRunnerExtension extends RunnerExtension {
throw new Error('Target is not found for step: ' + JSON.stringify(step));
}

await this.#ensureAutomationEmulatation(pageOrFrame);

const frame = await getFrame(pageOrFrame, step);

const assertedEventsPromise = waitForEvents(pageOrFrame, step, timeout);
Expand Down Expand Up @@ -568,6 +580,10 @@ interface ElementHandle<ElementType extends Element>
asElement(): ElementHandle<ElementType> | null;
}

interface CDPSession {
send(command: string, opts: { enabled: boolean }): Promise<unknown>;
}

interface Page {
setDefaultTimeout(timeout: number): void;
frames(): Frame[];
Expand All @@ -586,6 +602,7 @@ interface Page {
selector: string
): Promise<Array<ElementHandle<T>>>;
waitForFrame(url: string, opts: { timeout: number }): Promise<Frame>;
client(): CDPSession;
}

interface Frame {
Expand All @@ -606,4 +623,5 @@ interface Frame {
$$<T extends Element = Element>(
selector: string
): Promise<Array<ElementHandle<T>>>;
client(): CDPSession;
}

0 comments on commit 6caa57a

Please sign in to comment.