I run tests with Playwright and the tests differs if it's mobile or not.
When mobile I need to run page.tap() but when desktop I need to run page.click().
I have a working code bellow but how should i prevent DRY code in lots of tests?
Example code
test("Example test", async ({ page, isMobile }) => {
async function tapOrClick(selector) {
if(isMobile){
await page.tap(selector);
}
else {
await page.click(selector);
}
}
await tapOrClick('click on the DOM')
await tapOrClick('click on the DOM 1')
await tapOrClick('click on the DOM 2')
});
Then similar tests will be in another test file.
test("Example test 2", async ({ page, isMobile }) => {
async function tapOrClick(selector) {
if(isMobile){
await page.tap(selector);
}
else {
await page.click(selector);
}
}
await tapOrClick('another click on the DOM')
await tapOrClick('another click on the DOM 1')
await tapOrClick('another click on the DOM 2')
});