我需要通過WebDriver驗證測試請求。不幸的是,沒有簡單的方法來做到這一點,因爲沒有本地支持。看來我應該可以使用HtmlUnit來獲取請求,但我只能得到響應。有沒有辦法做到這一點與HtmlUnit或者我需要設置其他像Browsermob代理?我正在使用Java來做到這一點。使用WebDriver/HtmlUnit獲取所有HTTP請求
謝謝!
我需要通過WebDriver驗證測試請求。不幸的是,沒有簡單的方法來做到這一點,因爲沒有本地支持。看來我應該可以使用HtmlUnit來獲取請求,但我只能得到響應。有沒有辦法做到這一點與HtmlUnit或者我需要設置其他像Browsermob代理?我正在使用Java來做到這一點。使用WebDriver/HtmlUnit獲取所有HTTP請求
謝謝!
如果我正確理解您的問題,您希望查看HTMLUnit所做的每個請求和響應。
如果您使用的是Windows下載Fiddler http://www.telerik.com/fiddler
設置HTTP代理服務器設置到的HtmlUnit使用Fiddler作爲代理。
BrowserVersion bv = BrowserVersion.CHROME;
bv.setUserAgent("Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36");
webClient = new WebClient(bv, "127.0.0.1", 8888);
本身以上將用於如果你想捕捉HTTPS不使用HTTPS
交通在項目中創建以下類
import java.security.AccessController;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.PrivilegedAction;
import java.security.Security;
import java.security.cert.X509Certificate;
import javax.net.ssl.ManagerFactoryParameters;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactorySpi;
import javax.net.ssl.X509TrustManager;
public final class XTrustProvider extends java.security.Provider
{
/**
*
*/
private static final long serialVersionUID = 1L;
private final static String NAME = "XTrustJSSE";
private final static String INFO = "XTrust JSSE Provider (implements trust factory with truststore validation disabled)";
private final static double VERSION = 1.0D;
@SuppressWarnings({ "unchecked", "rawtypes" })
public XTrustProvider()
{
super(NAME, VERSION, INFO);
AccessController.doPrivileged(new PrivilegedAction()
{
public Object run()
{
put("TrustManagerFactory." + TrustManagerFactoryImpl.getAlgorithm(), TrustManagerFactoryImpl.class.getName());
return null;
}
});
}
public static void install()
{
if (Security.getProvider(NAME) == null)
{
Security.insertProviderAt(new XTrustProvider(), 2);
Security.setProperty("ssl.TrustManagerFactory.algorithm", TrustManagerFactoryImpl.getAlgorithm());
}
}
public final static class TrustManagerFactoryImpl extends TrustManagerFactorySpi
{
public TrustManagerFactoryImpl()
{
}
public static String getAlgorithm()
{
return "XTrust509";
}
protected void engineInit(KeyStore keystore) throws KeyStoreException
{
}
protected void engineInit(ManagerFactoryParameters mgrparams) throws InvalidAlgorithmParameterException
{
throw new InvalidAlgorithmParameterException(XTrustProvider.NAME + " does not use ManagerFactoryParameters");
}
protected TrustManager[] engineGetTrustManagers()
{
return new TrustManager[]
{ new X509TrustManager()
{
public X509Certificate[] getAcceptedIssuers()
{
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType)
{
}
public void checkServerTrusted(X509Certificate[] certs, String authType)
{
}
} };
}
}
}
調用安裝方法的任何現場工作類別
XTrustProvider.install();
確保在HTMLUnit創建之前調用上述方法任何http請求。
現在您將捕獲HTMLUnit發出的所有請求,包括https請求。
如果您遇到任何問題的評論,我會幫助。
我用下面提供的HtmlUnit一個例子:
final WebClient webClient = new WebClient(BrowserVersion.CHROME);
final HtmlPage loginPage = webClient.getPage("http://www.stackoverflow.com");
WebResponse response = loginPage.getWebResponse(); // the response loaded to create this page
WebRequest request = response.getWebRequest(); // the request used to load this page
如何只是這樣的事情:
HtmlUnitDriver driver = new HtmlUnitDriver() {
public Set<WebRequest> requests = new HashSet<>();
@Override
protected WebClient modifyWebClient(WebClient originalClient) {
return new WebClient() {
@Override
public WebResponse getPage(WebWindow window, WebRequest request) {
requests.add(request);
return super.getPage(window, request)
}
@Override
public WebResponse loadWebResponse(WebRequest request) {
requests.add(request);
return super.loadWebResponse(request);
}
// If it's really necessary for your use case, you can also override the "download" method in a similar way, but note that this is an internal API
}
}
};
driver.open("http://www.example.com/");
Set<WebRequest> requests = (Set<WebRequest>) driver.getClass().getField("requests").get(driver);
for (WebRequest request : requests) {
System.out.println(request.getUrl().toString()); // or whatever you want
}
當然,如果順序很重要,你可以使用一個List
代替的Set
,但是你必須檢查請求是否已經存在以避免重複。
使用什麼語言? – Arran
Java,剛更新了帖子。 – johnsoe
你是什麼意思「獲取所有HTTP請求」?你認爲這是一個像小提琴手一樣的代理嗎? –