java 使用 Selenium 捕获 404 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17657037/
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
Catching a 404 error with Selenium
提问by user2572022
I'm using Selenium and JUnit with Java and I want to know if a 404 error occurs when opening a new popup by clicking on a link (i'm searching for a thing like assertTrue(selenium.no404error()).
我正在将 Selenium 和 JUnit 与 Java 一起使用,我想知道在通过单击链接打开新弹出窗口时是否发生 404 错误(我正在搜索诸如 assertTrue(selenium.no404error())之类的东西。
How can I do that ?
我怎样才能做到这一点 ?
回答by LaurentG
You can't test if the HTTP status code is 404. This is explained in this other thread. The only way is to test for something in the page which can only be in your 404 error page. For instance:
您无法测试 HTTP 状态代码是否为 404。这在另一个线程中进行了解释。唯一的方法是测试页面中只能出现在 404 错误页面中的内容。例如:
assertTrue(driver.getTitle().contains("404"));
If you're not sure that your page contains something specific like 404or Page not found. You could generate from the server a special tag code (e.g. a <meta>
tag in the <head>
section) and test for it with the WebDriver
.
如果您不确定您的页面是否包含特定内容,例如404或Page not found。您可以从服务器生成一个特殊的标记代码(例如部分中的<meta>
标记<head>
)并使用WebDriver
.