Skip to content

Commit

Permalink
feat: implement context click (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastiaanNijland authored Oct 26, 2022
1 parent fcc3569 commit 31d2987
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ export default class DullahanAdapterPlaywright extends DullahanAdapter<DullahanA
});
}

public async click(selector: string): Promise<void> {
public async click(selector: string, button: 'left' | 'right' = 'left'): Promise<void> {
const {page} = this;

if (!page) {
Expand All @@ -527,7 +527,7 @@ export default class DullahanAdapterPlaywright extends DullahanAdapter<DullahanA
throw new AdapterError(DullahanErrorMessage.findElementResult(findOptions));
}

await element.click();
await element.click({ button });
}

public async clickAt(x: number, y: number): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ export default class DullahanAdapterPuppeteer extends DullahanAdapter<DullahanAd
});
}

public async click(selector: string): Promise<void> {
public async click(selector: string, button: 'left' | 'right' = 'left'): Promise<void> {
const {page, options: {useTouch}} = this;

if (!page) {
Expand Down Expand Up @@ -541,7 +541,7 @@ export default class DullahanAdapterPuppeteer extends DullahanAdapter<DullahanAd
return element.tap();
}

await element.click();
await element.click({ button });
}

public async clickAt(x: number, y: number): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ export default class DullahanAdapterSelenium4 extends DullahanAdapter<DullahanAd
}), timeout);
}

public async click(selector: string): Promise<void> {
public async click(selector: string, button: 'left' | 'right' = 'left'): Promise<void> {
const {driver, supportsPromises, options:{useActions}} = this;

if (!driver) {
Expand Down Expand Up @@ -652,13 +652,21 @@ export default class DullahanAdapterSelenium4 extends DullahanAdapter<DullahanAd
await driver.executeScript<void>(emitFakeEvent, 'mousemove', x, y, element);
await driver.executeScript<void>(emitFakeEvent, 'mousedown', x, y, element);
await driver.executeScript<void>(emitFakeEvent, 'mouseup', x, y, element);
if (button === 'right') {
return driver.executeScript<void>(emitFakeEvent, 'contextmenu', x, y, element);
}
return driver.executeScript<void>(emitFakeEvent, 'click', x, y, element);
}

await driver.actions().move({
let intermediate = driver.actions().move({
origin: element,
duration: 0
}).click().perform();
});
if (button === 'right') {
await intermediate.contextClick().perform();
} else {
await intermediate.click().perform();
}
}

public async clickAt(x: number, y: number): Promise<void> {
Expand Down Expand Up @@ -1109,12 +1117,12 @@ export default class DullahanAdapterSelenium4 extends DullahanAdapter<DullahanAd

if (seleniumRemoteUrl) {
const builder = new Builder().forBrowser(browserName, browserVersion).usingServer(seleniumRemoteUrl);

builder.usingHttpAgent(new Agent({
keepAlive: true,
keepAliveMsecs: 30 * 1000
}));

builder.withCapabilities(builder.getCapabilities().merge({
browser: browserName,
browser_version: browserVersion
Expand Down
2 changes: 1 addition & 1 deletion packages/dullahan/src/adapter/DullahanAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export abstract class DullahanAdapter<

public abstract clearText(selector: string, count: number): Promise<void>;

public abstract click(selector: string): Promise<void>;
public abstract click(selector: string, button?: 'left' | 'right'): Promise<void>;

public abstract clickAt(x: number, y: number): Promise<void>;

Expand Down
2 changes: 1 addition & 1 deletion packages/dullahan/src/browser_helpers/emitFakeEvent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type MouseEvents = 'mousedown' | 'mouseup' | 'click' | 'mousemove';
type MouseEvents = 'mousedown' | 'mouseup' | 'click' | 'mousemove' | 'contextmenu';

export type EmitFakeEventOptions = {
type: MouseEvents;
Expand Down

0 comments on commit 31d2987

Please sign in to comment.