Pages

How to handle alerts() in protractor ?

How to handle alerts() in protractor ?

>> In protractor, 
  •     we can accept browser alert using accept(),
  •     we can dismiss browser alert using dismiss() and
  •     we can get the text in alert using getText()

Please check below code script:

First write xpath with help of DOM

alert() in protractor
alerts() in protractor



Here we have a Click Me button, when we click this button it will display browser alert with Ok and Cancel buttons.

describe('Protractor - alerts()', function () {
it(
'Code script to handle alerts() in protractor', function () {
browser.
get("https://keeplearners.blogspot.com/2018/03/Angular-elements.html");
var clickButton = element(by.css("button[ng-click='clickMe(angularAlert)']"));

clickButton.click();
acceptAlert();


clickButton.click();
getAlertText();


dismissAlert();
});

function acceptAlert() {
var acceptAlert = browser.switchTo().alert();
acceptAlert.accept().then(function () {
console.log("Accepted alert");
});
}


function getAlertText() {
var getAlertTxt = browser.switchTo().alert();
getAlertTxt.getText().then(function (text) {
console.log("Text in alert=", text);
});
}


function dismissAlert() {
var dismissAlert = browser.switchTo().alert();
dismissAlert.dismiss().then(function () {
console.log("Dismissed alert");
});

}
});
Output:
It will return accept the alert, dismiss the alert and get text from alert.
protractor alert()
protractor alerts()
Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.