java - 如何使用 Webdriver 从页面捕获所有 Get/post 请求的响应状态

标签 java rest http selenium webdriver

当在浏览器中点击任何 url 时。提交了多个 get/post/delete 方法。我想捕获这些方法的状态。

尝试使用以下程序,但它给出了网页状态

   String url = "http://www.google.com/";
     WebClient webClient = new WebClient();
     HtmlPage htmlPage = webClient.getPage(url);
     try{
     //verify response
     Assert.assertEquals(200,htmlPage.getWebResponse().getStatusCode());
     System.out.println(true);
     }

最佳答案

我们有一个使用 HttpURLConnection 的实用程序类获取 GET 响应代码,但您可以使用相同的方法使用 setRequestMethod 方法获取其他方法。以下是我们如何获得 GET 响应:

private static int getResponseCode(String url) throws MalformedURLException, IOException{
    HttpURLConnection.setFollowRedirects(true);
    HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
    con.setConnectTimeout(connection_time_out);
    con.setRequestMethod("GET");
    int responseCode = con.getResponseCode();
    if(con != null){
        con.disconnect();
    }
    return responseCode;
}

关于java - 如何使用 Webdriver 从页面捕获所有 Get/post 请求的响应状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28323305/

相关文章:

java - NoClassDefFound错误: bea/jmapi/MethodProfileData when profiling app using JRockit Mission Control

从Spring @RestController收到的AngularJS显示PDF(字节[])

php - Zend 在我之前捕获所有异常?

ruby-on-rails-3 - 谷歌浏览器强制使用 HTTPS

javascript - Cesium KML 轮询数据

java - 从 apache 网站导入库

java - 字符串数组的索引

java - 使用 Java 从 mysql 数据库检索音频文件

java - 将简单的 JAVA 代码转换为 PHP(数组)

java - 使用 REST API 下载文件