javascript 如何使用量角器在警告框中单击确定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31667573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:09:35  来源:igfitidea点击:

How to click OK in alert box using protractor

javascriptangularjstestingselenium-webdriverprotractor

提问by Prateek Choudhury

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

我正在使用 AngularJS 并且我想删除一个链接,在这种情况下,会出现一个警告框以确认删除。

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

我正在尝试使用量角器进行 e2e 测试,如何在警告框中确认?

I tried:

我试过:

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

but it doesn't seem to work.

但它似乎不起作用。

Is there a provision in protractor for handling alert boxes?

量角器中是否有处理警报框的规定?

回答by alecxe

Wait for alert to become present:

等待警报出现

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

回答by alecxe

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

或:browser.switchTo().alert() 在量角器中不起作用

回答by user2020347

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);

回答by bhargava krishna

This will work fine:

这将正常工作:

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

回答by bhargava krishna

This thing is wroking fine i have tried it

这东西很好用,我试过了



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