Skip to content

Instantly share code, notes, and snippets.

@And93
Forked from Xotabu4/index.d.ts
Created March 31, 2021 11:21
Show Gist options
  • Save And93/351f82cb8def1f1c62710fd393feedc9 to your computer and use it in GitHub Desktop.
Save And93/351f82cb8def1f1c62710fd393feedc9 to your computer and use it in GitHub Desktop.
Adding waits into webdriverio clicks
/**
* Override original WDIO click, with automatic wait, scrolling, and retrying logic.
* $(...).click()
*/
browser.overwriteCommand(
'click',
function (originalClick, options: WebdriverIO.ClickOptions = { waitOptions: {} }) {
const {
retries = 2,
scroll = true,
scrollOptions,
waitDisplayed = true,
waitOptions = {
timeoutMsg: `Element is not visible, so cannot be clicked: ${this.selector}`,
...options.waitOptions
}
} = options;
let attempt = 0;
let error = null;
do {
attempt++;
try {
if (waitDisplayed) {
this.waitForDisplayed(waitOptions);
}
if (scroll) {
this.scrollIntoView(scrollOptions);
}
originalClick({ button: options.button, x: options.x, y: options.y });
return;
} catch (err) {
error = err;
browser.pause(100);
}
} while (attempt < retries);
throw error;
},
true
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment