0

I am unable to create a BMP to capture traffic from my Java tests being run in Sauce labs. Has anyone ever completed this task successfully?

Here is my exmaple code:

    public class AppiumTestBase {

    BrowserMobProxy proxy;

    public RemoteWebDriver createSauceDriver() throws Exception {
    String sauceUserName = "userName";
    String sauceAccessKey = "myKey";

    proxy = new BrowserMobProxyServer();
    proxy.start();

    int port = proxy.getPort();


    DesiredCapabilities caps = DesiredCapabilities.android();
    caps.setCapability("deviceName", "Android Emulator");
    caps.setCapability("deviceType", "phone");
    caps.setCapability("deviceOrientation", "portrait");
    caps.setCapability("browserName", "");
    caps.setCapability("platformVersion", "5.1");
    caps.setCapability("platformName", "Android");
    caps.setCapability("app", "sauce-storage:app.zip");
    caps.setCapability("app-package", "myPackage");
    caps.setCapability("app-activity", "myOpenActivity");

    //get/set test name
    StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    caps.setCapability("name", stackTrace[2].getMethodName());


    RemoteWebDriver rd = new RemoteWebDriver(new URL(MessageFormat.format("http://{0}:{1}@ondemand.saucelabs.com:"+port+"/wd/hub", sauceUserName, sauceAccessKey)),
            caps);

    return rd;
}

@Test
public void sauceIt() throws Exception {

    RemoteWebDriver d = createSauceDriver();

    proxy.newHar("New HAR");

    d.findElement(By.id("main_button")).click();

    Thread.sleep(4000);
    Har h = proxy.getHar();
    File f = new File("/Users/me/Desktop/sauceExample");
    h.writeTo(f);

    d.quit();

}

org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.

The test will actually run when I start the RemoteWebDriver on port 80, but will not capture any traffic.

e.g.

     RemoteWebDriver rd = new RemoteWebDriver(new URL(MessageFormat.format("http://{0}:{1}@ondemand.saucelabs.com:80/wd/hub", sauceUserName, sauceAccessKey)),
            caps);
Zach David
  • 41
  • 5

2 Answers2

0

In case anyone comes across this in search for an answer - you must start sauce connect through the BMP and then access the proxy through the API

@Test
public void sauceIt() throws Exception {

    RemoteWebDriver d = createSauceDriver();

    //start new har
    put("http://localhost:9000/proxy/9091/har");

    d.findElement(By.id("menu_footer")).click();

    Response har = get("http://localhost:9000/proxy/9091/har");
    String harStr = har.asString();
    System.out.println(harStr);

    d.quit();
Zach David
  • 41
  • 5
  • Hi, I'm looking at this example and wondering what this means: //start new har put("http://localhost:9000/proxy/9091/har"); Is it d.put? Also do you have sauceconnect and bmp running in your console? Thanks – Aqua267 May 19 '17 at 17:51
  • Hello - it is not. 'd' is the instance of the remote webdriver (mobile app) where the PUT is an HTTP request to the proxy that starts the har "recording." Please let me know if I can further clarify – Zach David May 23 '17 at 17:48
  • Thanks. I am still looking for help. Are you starting sauce connect in port 443? http://{0}:{1}@ondemand.saucelabs.com:"+port+"/wd/hub or 9091? – Aqua267 Jun 15 '17 at 22:49
  • I've pasted my code in a comment above. My question is 1) what port did you start browser mob proxy in? say 9090 or 9091?? 2) what port# to pass while starting sauceconnect proxy. Its default is 443 3) Do you have anything seperately running in console or is everything started by the java code? – Aqua267 Jun 15 '17 at 23:23
  • Sorry for many questions, How do I capture har response, for example I am using this call when I started BMP in console curl -X GET http://localhost:9090/proxy/9091/har but now I can't figure out what port to use to capture network traffic – Aqua267 Jun 15 '17 at 23:37
0
`public RemoteWebDriver createSauceDriver() throws Exception {
    String sauceUserName = "uname";
    String sauceAccessKey = "key";

    proxy = new BrowserMobProxyServer();
    proxy.start(9091);

    int port = proxy.getPort();

    System.out.println("bmp listening on port is " + port);

    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("platformName", "iOS");
    capabilities.setCapability("deviceName", "iPhone 7");
    capabilities.setCapability("platformVersion", "10.2");

    capabilities.setCapability("app", "sauce-storage:TestApp.app.zip");
    capabilities.setCapability("browserName", "");
    capabilities.setCapability("deviceOrientation", "portrait");
    capabilities.setCapability("autoAcceptAlerts", true);
    capabilities.setCapability("appiumVersion", "1.6.3");
    capabilities.setCapability("name", "SDK_Sauce");

    RemoteWebDriver rd = new RemoteWebDriver(new URL(SauceConnectProxy.URL), capabilities);

    return rd;
  }

  @Test
  public void sauceIt() throws Exception {

    RemoteWebDriver d = createSauceDriver();
    sdkDemoApp = new iOSSdkDemoAppPage(d);
    //start new har
    SauceConnectProxy.SavePutCall();

    demoApp.click();
    demoApp.rotate();

    String response = SauceConnectProxy.SaveGetCall();
    System.out.println(response);

    d.quit();

  }
Aqua267
  • 793
  • 2
  • 9
  • 28