Cypress can work with alerts by default. The pop-up can be an alert or confirmation popup. Cypress is designed in such a way that it shall always click on the OK button on the pop-up. Moreover, Cypress has the ability to fire the browser events.
An alert is triggered byΒ the window:alert event. This is by default handled by Cypress and the OK button on the alert gets clicked, without being visible during execution.
However, the execution logs will show the presence of the alert.
Implementation Alerts
The implementation of alerts in Cypress is given below β
describe('Adglob Test', function () {
// test case
it('Scenario 1', function (){
// launch url
cy.visit("https://register.rediff.com/register/register.php");
// click submit
cy.get('input[type="submit"]').click();
});
});
The alert message gets displayed on the Cypress execution logs.
Cypress has the ability to fire the window:alert event by utilizing the method on. Then, we can verify the alert text.
However, this event shall happen in the back end and will not be visible during the execution.
Implementation Alert text verification
Given below is the implementation for the alert text verification in Cypress β
describe('Adglob Test', function () {
// test case
it('Scenario 1', function (){
// launch url
cy.visit("https://register.rediff.com/register/register.php");
// click submit
cy.get('input[type="submit"]').click();
// fire event with method on
cy.on('window:alert',(t)=>{
//assertions
expect(t).to.contains('Your full name');
})
});
});
The output logs show the successful verification of the alert text, produced by firing the alert event by Cypress.
For a confirmation pop-up, the browser event window:confirm is triggered. Just like alert pop-ups, Cypress can fire this event with the method on and clicks on the OK button by default.
Example
Let us have a look at the below example. Here, on clicking the Click for JS Confirm button, a confirmation pop-up gets displayed.
The following confirmation pop-up with OK and Cancel buttons getting displayed.
On clicking the OK button, the following is displayed β
You clicked: Ok
An image like the one given below will be displayed β
On clicking the Cancel button, the following is displayed below Result β
You clicked: Cancel
An image like the one given below will be displayed β
Implementation Confirmation verification
Given below is an implementation for the confirmation verification of alerts in Cypress β
describe('Adglob Test', function () {
// test case
it("Scenario 1", function () {
//URL launched
cy.visit("https://the-internet.herokuapp.com/javascript_alerts")
//fire confirm browser event and accept
cy.get(':nth-child(2) > button').click()
cy.on("window:confirm", (t) => {
//verify text on pop-up
expect(t).to.equal("I am a JS Confirm");
});
});
});
Execution Results
The output is stated below β
Implementation Confirmation verification
Given below is an implementation for the confirmation verification of alerts in Cypress β
describe('Adglob Test', function () {
// test case
it("Scenario 1", function () {
//URL launched
cy.visit("https://the-internet.herokuapp.com/javascript_alerts")
//fire confirm browser event and accept
cy.get(':nth-child(2) > button').click()
cy.on("window:confirm", (t) => {
//verify text on pop-up
expect(t).to.equal("I am a JS Confirm");
});
});
});
Execution Results
The output is stated below β
The output logs show the successful verification of the confirmation text, produced by firing the confirm event by Cypress.
Implementation Cancel click
The implementation of the cancel click on confirmation pop up in Cypress is as follows β
describe('Adglob Test', function () {
// test case
it("Scenario 1", function () {
// URL launched
cy.visit("https://the-internet.herokuapp.com/javascript_alerts")
//fire confirm browser event
cy.on("window:confirm", (s) => {
return false;
});
// click on Click for JS Confirm button
cy.get(':nth-child(2) > button').click()
// verify application message on Cancel button click
cy.get('#result').should('have.text', 'You clicked: Cancel')
});
});
Execution Results
The output is given below β
The output logs show the successful verification of the textΒ You clicked: Cancel, which is produced by clicking the Cancel button on the confirmation pop-up.
Good day! This is my 1st comment here so I just wanted to give a quick shout
out and tell you I really enjoy reading your articles. Can you suggest any other blogs/websites/forums
that cover the same topics? Appreciate it!