1

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?

  • What kind of traffic you're looking to capture and what kind information about it you want to get? – Mikhail Jul 19 '17 at 20:03
  • @Mikhail First, I need to check if the page started to send requests to determined url (GET request) after some time (we have a video player inside page and after the video starts, it sends requests to our stats server, so we could analyze % of video played). Example of the request url: https://stats-...com/?pid=....&atype=0&cvprogress=10 Second, after click on button, I also need to check if the statistics request was send correctly. – Marina Rappoport Jul 20 '17 at 06:45

2 Answers2

3

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.

  • I use not standalone BrowserStack, but from my code: I added dependency to the pom: ` com.browserstack browserstack-local-java 1.0.2 ` – Marina Rappoport Jul 23 '17 at 12:07
  • When establish connection: `Local l = new Local(); Map options = new HashMap(); options.put("key", accessKey); options.put("forcelocal", "true"); options.put("forceproxy", "true"); options.put("proxyHost", "127.0.0.1"); options.put("proxyPort", String.valueOf(port)); l.start(options);' ` I get an exception: com.browserstack.local.LocalException: Could not connect to www.browserstack.com! – Marina Rappoport Jul 23 '17 at 12:16
  • It works without exception if I leave only: `Map options = new HashMap(); options.put("key", accessKey); options.put("forcelocal", "true"); l.start(options);` and delete other options (proxyHost, proxyPort...) – Marina Rappoport Jul 23 '17 at 12:16
  • 127.0.0.1 may not work with proxyHost, thats why they have --local-proxy-host (newer option). I have added steps, in my previous answer, to include new options via bindings. – Sanket Parlikar Jul 24 '17 at 11:53
  • I verified your code now, I would recommend to remove this Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy); capabilities.setCapability(CapabilityType.PROXY, seleniumProxy); Remote browsers wont understand your local proxies, that is why the binary is made aware of your local proxy. setting proxy works on remote browser when the proxy is public and you dont need binary connection. – Sanket Parlikar Jul 24 '17 at 12:46
  • exactly! I change the options to localProxyHost|Port and it works fine now. Thanks! – Marina Rappoport Jul 24 '17 at 12:47
  • cool! do mark this as answer if it worked for you. will help others! – Sanket Parlikar Jul 24 '17 at 14:30
1

I see 2 solutions for this problem

  1. BrowserMobProxy - there are 2 ways to run it: 1. from your code(adding library) 2. standalone proxy(controlled by REST API). In both cases you need to provide proxy to webdriver and control your proxy. One more improtant thing here to understand is that you need to redirect all the traffic from browsermob through the machine where proxy is located, please refer to this article for browserstack local execution. As I understand problem describes the case when proxy is being created on one machine and browserstack is simply not able to reach it. Using browsermob you can get ALL required information: like request, params, response code, response time, etc. And even wait for requests to finish.

  2. Examine performance logs. There is an option for ChromeDriver to capture performance logs. This option is easier since you don't need to care about proxy. However there are certain limitations of this approach as well: You won't be able to get request statistics like response time and response code and maybe some other that one may require. It would allow you to get only basic info like: request type, request url and of course you can parse params from url.

Mikhail
  • 605
  • 4
  • 13
  • Hi! thanks for your answer. I use BrowserMobProxy (embedded mode - from my code) and local testing feature of browserstack. I managed to establish connection behind proxy, so I get the url and can make screenshots... But the har file created by BrowserMobProxy contains only request to http://hub-cloud.browserstack.com/wd/hub/..., not the requests from the page itself. When using BrowserMobProxy without browserstack (I mean when I use local chromdriver, for example), I get har file with all request that I needed – Marina Rappoport Jul 23 '17 at 12:02
  • Have you tried approach with standalone proxy? Have you checked whether performance logs are sufficient for you or not? Also, I would recommend to add code samples in original post where you create WebDriver and set proxy – Mikhail Jul 23 '17 at 20:43
  • Just tried: with standalone proxy it seems to work fine, I get the har file (from link http://localhost:8080/proxy/8081/har) as I needed. I'm not sure about performance logs, because I need to check network for Firefox as well, not only Chrome. I'll add update with code in the post. Thanks – Marina Rappoport Jul 24 '17 at 09:27