使用Selenium捕获404错误 [英] Catching a 404 error with Selenium

查看:1159
本文介绍了使用Selenium捕获404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium和JUnit与Java,我想知道当通过点击链接打开一个新的弹出窗口时是否发生404错误(我正在搜索像assertTrue(selenium.no404error())之类的东西。

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()).

我该怎么做?

推荐答案

你不能测试HTTP状态代码是否为404.这在此其他主题中有说明。唯一的方法是测试页面中只能出现在404错误页面中的内容。例如:

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:

assertTrue(driver.getTitle().contains("404"));

如果你不确定你的页面包含特定内容,例如 404 找不到页面。您可以从服务器生成特殊标记代码(例如< meta> < head> 部分中的code>标记,并使用 WebDriver 进行测试。

If you're not sure that your page contains something specific like 404 or 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.

这篇关于使用Selenium捕获404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆