我正在撰寫一個簡單的腳本,我希望 selenium 使腳本失敗或至少記錄 http 回應中出現的錯誤。
uj5u.com熱心網友回復:
穩定的 Selenium 版本(低于 4)不支持此類功能。
它無法讀取 HTTP 回應。
像 Rest Assured 這樣的工具用于此類事情。
uj5u.com熱心網友回復:
您可以使用browsermob-proxy工具來實作這一點。您只應該了解,當您呼叫一個頁面時,實際上會導致對(不同)服務器的大量請求,并且每個服務器都使用自己的代碼進行回應。
例如,代碼可能如下所示:
@BeforeTest
public void setUp(){
/*
Create and start proxy instance here
Configure driver to use that created proxy here
*/
}
@Test
public void testHeaders(){
// Create a map that would be storing url-to-status association
// Use synchronized version of a hash map because
// proxy handles connections in parallel
Map<String, Integer> urlToStatuses = new ConcurrentHashMap<>();
// Configure handler that would be listening for responses
// and fill the map
browserMobProxy.addResponseFilter((response, contents, messageInfo) -> {
urlToStatuses.put(messageInfo.getOriginalUrl(), response.status().code());
});
// Call a page
driver.get("https://google.com");
// Test the code of required URL
Assert.assertEquals(urlToStatuses.get("https://google.com").intValue(), 200);
// Do no forget to clear the map before the next assertion
urlToStatuses.clear();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/339772.html
上一篇:PowerShell-從CSV轉換為XML并更新XML記錄
下一篇:如何從Udemy中獲取價格?
