eclipse SeleniumException:错误:出现意外警报!

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

SeleniumException: ERROR: There was an unexpected Alert!

javaeclipseinternet-explorer-7junitselenium

提问by Afamee

I get this error while running selenium RC tests against IE7.

对 IE7 运行 selenium RC 测试时出现此错误。

com.thoughtworks.selenium.SeleniumException: ERROR: There was an unexpected Alert! [error:[object Error]]

com.thoughtworks.selenium.SeleniumException:错误:出现意外警报![错误:[对象错误]]

It happens whenever I attempt to click a link or a tab or anything clickable. Any Ideas guys. This is really frustrating.

每当我尝试单击链接或选项卡或任何可单击的内容时都会发生这种情况。任何想法家伙。这真的很令人沮丧。

回答by matt b

Checking the Google search results for your error message, and the selenium development mailing list, it sounds as if this is not a bug in Selenium but rather it is Selenium telling you that an unexpected window.alert() function was called in your web application, and that Selenium does not know how to handle it because you did not tell it how to.

检查Google 搜索结果中的错误消息selenium 开发邮件列表,听起来好像这不是 Selenium 中的错误,而是 Selenium 告诉您在您的 Web 应用程序中调用了意外的 window.alert() 函数,并且 Selenium 不知道如何处理它,因为您没有告诉它如何处理。

回答by Dave Hunt

I'd be surprised if the same issue isn't occurring in other browsers. Perhaps your application behaves differently depending on the browser? Basically Selenium is reporting that it can't move onto further commands after you are clicking on these links or tabs because it has detected an unexpected JavaScript alert. You wont see these alerts because Selenium consumes them. There are a few things you can do to work out what's going on.

如果在其他浏览器中没有发生同样的问题,我会感到惊讶。也许您的应用程序的行为取决于浏览器?基本上,Selenium 报告说,在您单击这些链接或选项卡后,它无法继续执行其他命令,因为它检测到了意外的 JavaScript 警报。您不会看到这些警报,因为 Selenium 会消耗它们。你可以做一些事情来弄清楚发生了什么。

The first thing I'd suggest is to just check your application in IE7. Manually complete the steps of your tests - do you see the JavaScript alerts? If so, you will need to add the appropriate commands to your Selenium test.

我建议的第一件事就是在 IE7 中检查您的应用程序。手动完成您的测试步骤 - 您看到 JavaScript 警报了吗?如果是这样,您需要将适当的命令添加到 Selenium 测试中。

If for some reason you can't replicate the alerts manually you can either dismiss the alert by using the the getAlert command, or use the response from getAlert to find out the text of the unexpected alert.

如果由于某种原因您无法手动复制警报,您可以使用 getAlert 命令关闭警报,或者使用 getAlert 的响应来找出意外警报的文本。

Java/TestNG example for finding out the message of the alert:

用于查找警报消息的 Java/TestNG 示例:

assertEquals(selenium.getAlert(), "Hello World");

The above will still cause your test to fail (unless the alert really does say 'Hello World'), but will fail with a message similar to "expected 'Hello World' but was 'Your unexpected alert message'".

以上仍然会导致您的测试失败(除非警报确实说'Hello World'),但会失败并显示类似于“预期的'Hello World'但是是'您的意外警报消息'”的消息。

回答by DazManCat

I'm getting a similar thing.
No error in IE, JS error popup in Chrome, nothing in FF...until I run my Selenium script and it reports an unexpected error. Amusingly when I do selenium.IsAlertPresent() it returns false, even though it says there was one.

我得到了类似的东西。
IE 中没有错误,Chrome 中出现 JS 错误弹出窗口,FF 中没有任何错误……直到我运行 Selenium 脚本并报告意外错误。有趣的是,当我执行 selenium.IsAlertPresent() 时它返回 false,即使它说有一个。

回答by shafi jourabchi

I've seen this error also. In my case the issue was the latency in an Ajax response object was causing Selenium IDE error. The solution that worked for me was to add a pause command right after any Ajax calls in the test script.

我也见过这个错误。就我而言,问题是 Ajax 响应对象中的延迟导致 Selenium IDE 错误。对我有用的解决方案是在测试脚本中的任何 Ajax 调用之后立即添加暂停命令。

The call below was taking few seconds to return with valid Ajax response click css=#form3 > div.addButton > button.btn.btn-primary

下面的调用需要几秒钟才能返回有效的 Ajax 响应单击 css=#form3 > div.addButton > button.btn.btn-primary

By adding a pause clause, the "Unexpected alert" issue was resolved pause 5000

通过添加 pause 子句,解决了“意外警报”问题 pause 5000

回答by Arjun Nagathankandy

I Suggest you to test manually those steps which are there in your selenium test.If it is not reproducible then it might not be the problem in the page. please debug your selenium script with assert function for identifying where is it finding the unexpected alert.

我建议您手动测试 selenium 测试中的那些步骤。如果它无法重现,那么它可能不是页面中的问题。请使用断言功能调试您的硒脚本,以识别它在哪里找到意外警报。

 if (selenium.isAlertPresent()) {
assertEquals(selenium.getAlert(), "Hello World");

}

}

which will help you identify the problem. I suggest you to repeat the test because you may find that assertion occurring in different part of your code.

这将帮助您确定问题。我建议您重复测试,因为您可能会发现断言发生在代码的不同部分。

回答by Altonymous

I'm seeing the same error. Normally we run our test using C#, but in this case we are setting up some simple tests using the Selenium IDE.

我看到了同样的错误。通常我们使用 C# 运行我们的测试,但在这种情况下,我们使用 Selenium IDE 设置一些简单的测试。

Since Selenium IDE records your events and then allows you to play them back you would expect it to handle the alert. When looking at the events it records it appears it's capturing the alert. However, when you play it back the events are out of order.

由于 Selenium IDE 会记录您的事件,然后允许您回放它们,因此您希望它能够处理警报。查看它记录的事件时,它似乎正在捕获警报。但是,当您播放它时,事件是乱序的。

First it captures the click event. Then is uses assertAlert to make sure the alert happened.

首先它捕获点击事件。然后使用 assertAlert 来确保警报发生。

The problem occurs when you play it back. It does the click event and then errors out. If you move the assertAlert statement before the click event it doesn't work. If you change the assertAlert to waitForAlert that doesn't work either. I can't seem to figure out anyway to get the playback working in the Selenium IDE. If I didn't need this runnable for a non-techy I'd just put it into my regular tests and be done with it.

播放时出现问题。它执行点击事件,然后出错。如果在单击事件之前移动 assertAlert 语句,则它不起作用。如果您将 assertAlert 更改为 waitForAlert 也不起作用。我似乎无法弄清楚如何在 Selenium IDE 中进行播放。如果我不需要这个可运行的非技术人员,我只需将它放入我的常规测试中并完成它。

Edit: After further review it appears the alert message isn't matching. Even though it grabbed the alert itself when doing the record. Visual inspection doesn't do anything to help me determine what it considers different.

编辑:进一步后,似乎警报消息不匹配。即使它在做记录时自己抓住了警报。目视检查并不能帮助我确定它认为不同的地方。