简体   繁体   中英

How to click OK in alert box using protractor

I am using AngularJS and I want to delete a link, in such cases, an alert box appears to confirm the delete.

I am trying to do e2e test using protractor, how do I confirm in an alert box?

I tried:

browser.switchTo().alert().accept()

but it doesn't seem to work.

Is there a provision in protractor for handling alert boxes?

Wait for alert to become present :

var EC = protractor.ExpectedConditions;
browser.wait(EC.alertIsPresent(), 5000, "Alert is not getting present :(")

try

browser.driver.get('URL');
browser.switchTo().alert().accept();

or

browser.ignoreSynchronization = true
browser.get('URL');
browser.switchTo().alert().accept();

or : browser.switchTo().alert() not working in protractor

Set up a promise to wait for the alert to be present:

function getAlertAndClose(element) {
    return element.click().then(function (alertText) {
        //Wait for alert to pop up
        browser.wait(function () {
            return browser.switchTo().alert().then(
                function () {return true;},
                function () {return false;}
            );
        }, 3000); // Wait timeout

        // Test alert is what you expect
        var popupAlert = browser.switchTo().alert();
        alertText = popupAlert.getText();
        expect(alertText).toMatch('Are you sure you want to delete this?');

        // Close alert
        popupAlert.dismiss();
    })
}

var saveButton = $('.saveBtn');
getAlertAndClose(saveButton);

这将正常工作:

await browser.switchTo().alert().accept();

这东西很好用,我试过了


 await browser.switchTo().alert().accept();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM