Reputation: 97
I'm using the latest Appium and webdriverion versions to test a native app, and what I need to do in my script is Click on a Button if it exists, but continue if it doesn't.
So, if the Accept and Close
button is present it will be clicked, and then the subsequent Allow
button will be clicked.
But, if the Accept and Close
button isn't present, then it will go straight to the next command and click the Allow
button straight away.
My code currently looks like this;
if (await expect(await closeCMP).isExisting()) {
await closeCMP.click()
} else {
var allow = await $('~Allow');
await allow.click();
}
});
});
However, if the Accept and Close
button isn't present then the test fails (with a timeout error, as the Accept and Close
element cannot be found) rather than perform the next command (which is to click on the Allow
button).
Is there an obvious reason for this?
Also, do I need the else
part of the loop, or will just the if
part of the loop suffice?
I appreciate that some may perceive this as bad practice, but I'm not able to alter anything on the DOM, or do anything with Cookies so I'm not sure what else I can do to get this to work.
Any help would be greatly appreciated.
Upvotes: 2
Views: 1982
Reputation: 51
There's more than one way to do this, but I generally use try/catch
blocks to prevent tests from failing when evaluating conditionals.
Here's an example of how it could look using this approach.
try {
await expect(await closeCMP).isExisting();
await closeCMP.click();
} catch {
console.warn('one of the previous lines threw an error');
}
const allow = await $('~Allow');
await allow.click();
Upvotes: 2