Search code examples
javaselenium-webdriverbrowserstackbrowsermob

Is there a way to capture network using BrowserStack with Selenium?


I use BrowserStack with Selenium-webdriver to run tests on different types of devices and browsers. So actually tests are running by RemoteWebDriver. I know that it's possible to capture network within Selenium tests using BrowserMobProxy, but as i understand it's working only if test is running on local machine.
Is there a way to capture network while running test on cross-platform base like BrowserStack?

UPDATE
I managed to get capture of network in har file (from link "localhost:8080/proxy/8081/har"), using standalone BrowserMobProxy and standalone local BrowserStack, as I was advised.

I tried to do the same automatically from code:

    BrowserMobProxy proxy = new BrowserMobProxyServer();
    proxy.start(8080);
    System.out.println("Proxy port: " + port);
    System.setProperty("java.net.useSystemProxies", "true");
    System.setProperty("http.proxyHost", "127.0.0.1");
    System.setProperty("http.proxyPort", "8080");
    System.setProperty("https.proxyHost", "127.0.0.1");
    System.setProperty("https.proxyPort", "8080");

    Local l = new Local();
        Map<String, String> options = new HashMap<String, String>();
        options.put("key", accessKey);
        options.put("forcelocal", "true");`

    //when I uncomment it i get an exception: 
    //com.browserstack.local.LocalException: Could not connect to www.browserstack.com! 

    //            options.put("forceproxy", "true");
    //            options.put("proxyHost", "localhost");
    //            options.put("proxyPort", "8080");
        l.start(options);
    }
    Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
    capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
    proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
    driver = new RemoteWebDriver(new URL("http://"+username+":"+accessKey+"@"+config.get("server")+"/wd/hub"), capabilities);'

    proxy.newHar("testHar.com");

    driver.get(testUrl);
    Thread.sleep(15000);
    Har har = proxy.getHar();
    FileOutputStream fos = new FileOutputStream("C:\\LoadingPage\\network\\testHar.har");
    har.writeTo(fos);

The connection to the url is working, I could see it and make screenshouts. BUT! In the har file I see only request to "hub-cloud.browserstack.com/wd/hub/...", not the requests from page itself.

How to get correct har from code? What in the code is not correct?


Solution

  • From my experience I would like to add small modification to the binary command given in BrowserStack link (Shared by Mikhail). The cmd given in doc should work well for private URLs but may not work for public ones.


    • Steps for Standalone binary:

    1 - Download the BrowserStackLocal binary from 'https://www.browserstack.com/local-testing#command-line'.

    Launch the binary by running the below command to enable your proxy to monitor the traffic.

    - BrowserStackLocal.exe --key --local-proxy-host --local-proxy-port --local-proxy-user --local-proxy-pass --force-proxy --force-local

    More details on all the modifiers are available at 'https://www.browserstack.com/local-testing#modifiers'.

    2 - Include "browserstack.local" capability in your test script.

    "browserstack.local" = true



    • Steps for Java (BrowserStack Local) bindings:

    1 - Follow these steps for using local bindings.

    2 - Using this you can use newer options available in latest versions of the binary. For instance if you wish to add --local-proxy-* options, for which there is no existing wrapper (like this which is internally mapped to this), try using below:

    bsLocalArgs.put("-local-proxy-host", "Your BrowserMob proxy IP");
    
    bsLocalArgs.put("-local-proxy-port", "Your BrowserMob proxy Port");
    
    bsLocalArgs.put("-local-proxy-user", "Your BrowserMob proxy Username");
    
    bsLocalArgs.put("-local-proxy-pass", "Your BrowserMob proxy Password");
    

    3 - Include "browserstack.local" capability in your test script.

    "browserstack.local" = true


    How it works: BrowserStack, by default, will resolve all the public URLs from their network.

    Using --force-local option will force the binary to resolve all the traffic (even public URLs) via your network and not from BrowserStack's network.

    Adding --local-proxy-* options will let the binary know that the traffic needs to be routed via your local proxy as well.

    Now your local BrowserMob can capture all the traffic in HAR.