1
votes

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?

2
What kind of traffic you're looking to capture and what kind information about it you want to get?Mikhail
@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: 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

2 Answers

3
votes

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.

1
votes

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.