-
-
Save And93/351f82cb8def1f1c62710fd393feedc9 to your computer and use it in GitHub Desktop.
Adding waits into webdriverio clicks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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