javascript 使用 Protractor 测试确认对话框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23891698/
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 01:44:51  来源:igfitidea点击:

Testing a confirm dialog with Protractor

javascriptangularjstestingprotractorend-to-end

提问by redwulf

This seems to be a pretty simple question, but I really can't find an answer online and I was not able to find an answer myself.

这似乎是一个非常简单的问题,但我真的无法在网上找到答案,而且我自己也无法找到答案。

I'm using AngularJS for my application and at some point, I have a native JavaScript confirmation box/dialog which asks the user if he/she wants to accept or cancel a change.

我正在将 AngularJS 用于我的应用程序,并且在某些时候,我有一个原生 JavaScript 确认框/对话框,询问用户他/她是要接受还是取消更改。

How can I simulate the selected option in my tests (with Protractor)? Is it possible to access the confirmation box and "click" either Ok or Cancel and act accordingly in my test? I'm guessing something like

如何在我的测试中模拟选定的选项(使用量角器)?是否可以访问确认框并“单击”确定或取消并在我的测试中采取相应措施?我猜是这样的

ptor.switchTo().<something>

would be possible, but I can't seem to find an answer.

有可能,但我似乎找不到答案。

回答by redwulf

I guess I can answer my own question as this may be of use to someone else.

我想我可以回答我自己的问题,因为这可能对其他人有用。

First, you need to get your Protractor instance:

首先,您需要获取 Protractor 实例:

var ptor = protractor.getInstance();

Confirmation dialogs are handled the same way as alerts, so something like this did the trick:

确认对话框的处理方式与警报的处理方式相同,所以像这样的事情就成功了:

var alertDialog = ptor.switchTo().alert();
alertDialog.accept();  // Use to accept (simulate clicking ok)
alertDialog.dismiss(); // Use to simulate cancel button

So simple and elegant, yet hard to find an answer. Hope this helps someone else

如此简单和优雅,但很难找到答案。希望这对其他人有帮助

回答by z0d14c

Copied from a comment above, but I had to user browser.switchTo().alert() instead of grabbing my Protractor instance.

复制自上面的评论,但我必须使用 browser.switchTo().alert() 而不是抓取我的 Protractor 实例。

Ended up using:

最终使用:

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

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

to answer an alert dialog.

回答警告对话框。

回答by Henry

browser.switchTo().alert()

browser.switchTo().alert()

this worked for me. The protractor.getInstance method didn't work for me.

这对我有用。protractor.getInstance 方法对我不起作用。