Selenium自动化获取Http报文信息并判断当前API状态

    public int loadingFinishedCount(WebDriver driver){
LogEntries logs = driver.manage().logs().get("performance");
// System.out.println("\nList of log entries:\n");
int m=0;
for(Iterator<LogEntry> it = logs.iterator(); it.hasNext();){
LogEntry entry = it.next();
try{
JSONObject json = new JSONObject(entry.getMessage());
JSONObject message = json.getJSONObject("message");
// System.out.println(message);
String method = message.getString("method");
if(method!=null&&"Network.loadingFinished".equals(method)){
++m;
}
}catch (JSONException e){
e.printStackTrace();
}
}
return m;
}

public void pageLoadingFinished(WebDriver driver, int loadingFinishedCount){
loadingFinishedCount(driver);
// System.out.println(m);
for(double i=0; i<=1000; i=i+0.1) {
int n = loadingFinishedCount(driver);
// System.out.println(n);
if (n >=loadingFinishedCount) {
break;
}
}
}

reference:https://stackoverflow.com/questions/6509628/how-to-get-http-response-code-using-selenium-webdriver-with-java

posted on 2018-03-29 16:20  BruceSue  阅读(1321)  评论(0编辑  收藏  举报

导航