Get all HTTP requests using WebDriver / HtmlUnit

I need to check a test request through WebDriver. Unfortunately, there is no easy way to do this, since there is no built-in support. It looks like I should use HtmlUnit to get the requests, but I was able to get the answers. Is there a way to do this using HtmlUnit or do I need to configure something else, like Browsermob Proxy? I am using Java for this.

Thank!

+3
source share
3 answers

The following is an example of using 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
+1
source

If I understand your question correctly, you want to see every request and response that was made using HTMLUnit.

, Fiddler http://www.telerik.com/fiddler

http proxy 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.

, .

0

- :

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 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, , , .

0

All Articles