2 Replies Latest reply on Aug 7, 2019 8:19 AM by nscheeren

    Extension for Arquillian Drone to create HAR-File

    nscheeren

      Hi all,

       

      i am currently working on an Extension for Drone. It is supposed to create a HAR-File for each test method. Those files then will be used for creating loadttest-szenarios in gattling for example (a lot of loadtest-tools have import interfaces for this type of file).

       

      my status:

      i wrote the extension and HAR-Files are created for each method, but somehow they do not contain http-requests.

       

      my code:

      Observer:

      public class PerformanceHARHandling {
      
      
      private BrowserMobProxyServer server = PerformanceDroneInstatiator.getServerIntance();
      private GeckoDriverService geckoService = PerformanceDroneInstatiator.getGeckoServiceIntance();
      private String harFileName;
      
      
      public void prepareHAR(@Observes EventContext context) throws IOException {
      harFileName = "arquilliantest" + System.currentTimeMillis() + context.getEvent().getTestMethod().getName();
      server.newHar(harFileName);
      if (server.getHar() != null) {
      System.err.print(harFileName + " is prepared");
      } else {
      throw new RuntimeException("HAR was not created!");
      }
      context.proceed();
      
      
      }
      
      
      public void writeHAR(@Observes EventContext context) throws IOException {
      Har har = server.getHar();
      String pfad = System.getProperty("user.dir") + File.separator + harFileName + ".har";
      if (!server.getHar().getLog().getEntries().isEmpty()) {
      File harFile = new File(pfad);
      har.writeTo(harFile);
      System.err.print(pfad + " is saved");
      
      
      List entries = har.getLog().getEntries();
      for (HarEntry entry : entries) {
      System.err.println("Request URL: " + entry.getRequest().getUrl());
      System.err.println("Entry response status: " + entry.getResponse().getStatus());
      System.err.println("Entry response text: " + entry.getResponse().getStatusText());
      
      
      }
      
      
      } else {
      throw new RuntimeException("HAR is empty!");
      }
      
      
      context.proceed();
      
      
      }
      
      
      public void closeServer(@Observes EventContext context) throws IOException {
      server.endHar();
      server.stop();
      geckoService.stop();
      
      
      context.proceed();
      
      
      }
      
      
      }
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      

       

      Instantiator for Drone

      public class PerformanceDroneInstatiator implements Instantiator<firefoxdriver, webdriverconfiguration=""> {
      
      
      private static volatile BrowserMobProxyServer server = new BrowserMobProxyServer();
      private Proxy seleniumProxy;
      private static GeckoDriverService geckoService;
      private FirefoxOptions options = new FirefoxOptions();
      private String hostIp;
      
      
      // Create and Start Server, Create Proxy in the Server and include this Proxy in
      // FirefoxOptions, that are used in the new FirefoxDriver Instance.
      @Override
      public FirefoxDriver createInstance(WebDriverConfiguration arg0) {
      server.setTrustAllServers(true);
      server.setHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
      server.start();
      
      
      System.err.println("BrowserMob Proxy running on port: " + server.getPort());
      
      
      seleniumProxy = ClientUtil.createSeleniumProxy(server);
      
      
      int port = server.getPort();
      
      
      try {
      hostIp = Inet4Address.getLocalHost().getHostAddress();
      seleniumProxy.setHttpProxy(hostIp + ":" + port);
      seleniumProxy.setSslProxy(hostIp + ":" + port);
      } catch (UnknownHostException e1) {
      e1.printStackTrace();
      System.err.println("invalid Host Address");
      }
      
      
      options.setCapability(CapabilityType.PROXY, seleniumProxy);
      options.setAcceptInsecureCerts(true);
      options.addPreference("network.proxy.type", 1);
      options.addPreference("network.proxy.socks", "127.0.0.1");
      options.addPreference("network.proxy.socks_port", port);
      
      
      geckoService = new GeckoDriverService.Builder()
      .usingDriverExecutable(new File("C:/Program Files/GeckoDriver/geckodriver.exe")).usingAnyFreePort()
      .build();
      
      
      try {
      geckoService.start();
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      
      
      return new FirefoxDriver(geckoService, options);
      
      
      }
      
      
      public static BrowserMobProxyServer getServerIntance() {
      return server;
      }
      
      
      public static GeckoDriverService getGeckoServiceIntance() {
      return geckoService;
      }
      
      
      @Override
      public int getPrecedence() {
      return 0;
      }
      
      
      }
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      

       

       

      I use the following versions:

      • Browsermob Core: 2.1.5
      • Arquillian : 1.4.1-Final
      • Drone: 2.5.1
      • Selenium: 3.14.0
      • Firefox 67.0
      • GeckoDriver: 0.24.0

       

      The Stackoverflow-Ticket (java - HAR incomplete when using BrowserMob Proxy in an Extension for Arquillian Drone - Stack Overflow )  i created for this topic.

       

      Any ideas would be much appreciated

       

      edit: i think the problem is the tracking protection function of firefox. I tried creating a FirefoxProfile (that has this preference set to false) in my code, but somehow my configurations don´t work. (there is no change in the settings) Is there something i need to consider when changing settings in firefox?

       

      Nachricht geändert durch Nicole Scheeren

        • 1. Re: Extension for Arquillian Drone to create HAR-File
          nscheeren

          I changed my browser to Firefox Developer Edition (the tracking should not be a problem here) , but i still only get the following requests:

           

              Request URL: http://detectportal.firefox.com/success.txt

              Entry response status: 200

              Entry response text: OK

              Request URL: http://detectportal.firefox.com/success.txt?ipv6

              Entry response status: 200

              Entry response text: OK

              Request URL: http://detectportal.firefox.com/success.txt?ipv4

              Entry response status: 200

              Entry response text: OK

           

          as HAR (arquilliantest1562602452712testCreateKalenderJahre is the name of the har-File):

           

          {"log":{"version":"1.2","creator":{"name":"BrowserMob Proxy","version":"2.1.5","comment":""},"pages":[{"id":"arquilliantest1562602452712testCreateKalenderJahre","startedDateTime":"2019-07-08T16:14:12.901Z","title":"arquilliantest1562602452712testCreateKalenderJahre","pageTimings":{"comment":""},"comment":""}],"entries":[{"pageref":"arquilliantest1562602452712testCreateKalenderJahre","startedDateTime":"2019-07-08T16:14:15.530Z","request":{"method":"GET","url":"http://detectportal.firefox.com/success.txt","httpVersion":"HTTP/1.1","cookies":[],"headers":[],"queryString":[],"headersSize":340,"bodySize":0,"comment":""},"response":{"status":200,"statusText":"OK","httpVersion":"HTTP/1.1","cookies":[],"headers":[],"content":{"size":8,"mimeType":"text/plain","text":"success\n","comment":""},"redirectURL":"","headersSize":378,"bodySize":8,"comment":""},"cache":{},"timings":{"comment":"","ssl":-1,"dns":5,"wait":12,"connect":26,"blocked":0,"receive":4,"send":4},"serverIPAddress":"2.16.64.137","comment":"","time":53},{"pageref":"arquilliantest1562602452712testCreateKalenderJahre","startedDateTime":"2019-07-08T16:14:15.611Z","request":{"method":"GET","url":"http://detectportal.firefox.com/success.txt?ipv6","httpVersion":"HTTP/1.1","cookies":[],"headers":[],"queryString":[{"name":"ipv6","value":""}],"headersSize":345,"bodySize":0,"comment":""},"response":{"status":200,"statusText":"OK","httpVersion":"HTTP/1.1","cookies":[],"headers":[],"content":{"size":8,"mimeType":"text/plain","text":"success\n","comment":""},"redirectURL":"","headersSize":378,"bodySize":8,"comment":""},"cache":{},"timings":{"comment":"","ssl":-1,"dns":2,"wait":13,"connect":17,"blocked":0,"receive":0,"send":0},"serverIPAddress":"2.16.64.137","comment":"","time":35},{"pageref":"arquilliantest1562602452712testCreateKalenderJahre","startedDateTime":"2019-07-08T16:14:15.611Z","request":{"method":"GET","url":"http://detectportal.firefox.com/success.txt?ipv4","httpVersion":"HTTP/1.1","cookies":[],"headers":[],"queryString":[{"name":"ipv4","value":""}],"headersSize":345,"bodySize":0,"comment":""},"response":{"status":200,"statusText":"OK","httpVersion":"HTTP/1.1","cookies":[],"headers":[],"content":{"size":8,"mimeType":"text/plain","text":"success\n","comment":""},"redirectURL":"","headersSize":378,"bodySize":8,"comment":""},"cache":{},"timings":{"comment":"","ssl":-1,"dns":2,"wait":13,"connect":14,"blocked":0,"receive":1,"send":0},"serverIPAddress":"2.16.64.137","comment":"","time":32}],"comment":""}}

          • 2. Re: Extension for Arquillian Drone to create HAR-File
            nscheeren

            I have found a solution to my Problem now and i thought i might share it here:

            first: the BrowserMob (or BrowserUp)  Proxy needed to be initialized and startet at an Event before the Drone Initializer creates the FirefoxDriver instance. (I used ManagerStarted for this)

            second: Firefox in automation Mode automatically sets an proxy exception for localhost requests. so i had to add "options.addPreference("network.proxy.allow_hijacking_localhost", true);" for my FirefoxOptions in my Instantiator