I automate scenario where I need to verify that after click on button browser alert is visible for user and contains correct text. Usually I'm working with google chrome and my solutions is working for chrome, bet when I wanted to test scenario on firefox, console throw error TypeError: browser.isAlertOpen is not a function , after some googling I found that isAlertOpen is working only on Chrome browser, but I can't found nothing for firefox. Is there any solution how to do it that it will work for both browsers?
My code right now
Steps.js
Then(/^I verify that alert is visible with text $/, async () => {
await browser.waitUntil(() => browser.isAlertOpen());
const msg = await browser.getAlertText();
expect(msg).toEqual('Text on alert');
});
Yeah, that's true. Because the isAlertOpen method is non-official and undocumented Chromium command. Moreover, there's no similar method for Firefox. However, you can create a function to check the alert is present or not.
function isAlertPresent() {
return async function() {
try {
await this.getAlertText();
return true;
} catch (error) {
if (error.name === 'no such alert') {
return false;
} else {
throw error;
}
}
};
}
This is a simple function that returns another function that tries to get alert text and if it succeeds then it'll return true if an alert is not present it'll return false.
Then you can add it to your step or test:
Then(/^I verify that alert is visible with text $/, async () => {
// ↓ call function
await browser.waitUntil(isAlertPresent());
const msg = await browser.getAlertText();
expect(msg).toEqual('Text on alert');
});
Also, you can consider adding the isAlertPresent function as a custom command. Read more here.