BrowserMob Proxy未在chrome中打开第二页(登陆到第二页后Internet连接关闭)

时间:2017-02-06 08:34:45

标签: selenium-webdriver selenium-chromedriver geckodriver har browsermob-proxy

我在ubuntu上运行Windows 10虚拟机,在VM上我有以下版本: -

Eclipse neon, Selenium Webdriver 3.0.1, Mozilla Firefox 51.0.1, Google Chrome 55, geckoDriver v0.11.1, Chromedriver v2.27, BrowserMob Proxy jar 2.1.4, jackson-all-1.7.3.jar, harlib-1.1.2.jar

我想阅读selenium中的浏览器流量,为此我选择了BrowserMob proxy。我在浏览器上有2页的通信,例如Authenticates url, Lands on the 1st page clicks a dropdown menu (link) and lands on the 2nd page

当第二页打开时,页面无法加载,但是调用仍在后面。我注意到的是,当第二页加载互联网连接时,虚拟机只在该页面上关闭(互联网标志停止闪烁)。

当我重新启动the VM & the host machine并执行代码时。它在重新启动系统后再次加载页面,然后再次,如果我执行相同的代码同样的问题仍然存在。

但是流量数据会记录在文件中。如果我尝试在geckodriver(Firefox)中执行相同的代码,那么该网页就会被加载但流量未被捕获。capturing only specific urls from the log data

我坚持两种方式。请在下面找到代码: -

第1课: -

public class BrowserMobExample{         

public static String sFileName = System.getProperty("user.dir") + "\\CaptureNetworkTraffic\\BrowserMob.har";

WebDriver driver = null;
BrowserMobProxy proxy = null;

@BeforeTest
public void setUp() throws Exception {

   // start the proxy
    proxy = new BrowserMobProxyServer();
    proxy.start(0);

    //get the Selenium proxy object - org.openqa.selenium.Proxy;
    Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);

    // configure it as a desired capability
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);

    //set chromedriver system property
    System.setProperty("webdriver.chrome.driver", "path to chromedriver");      
    driver = new ChromeDriver(capabilities);
    /*System.setProperty("webdriver.gecko.driver", "path to geckodriver");
    driver = new FirefoxDriver(capabilities);*/

    // enable more detailed HAR capture, if desired (see CaptureType for the complete list)
    proxy.enableHarCaptureTypes(CaptureType.REQUEST_HEADERS, CaptureType.RESPONSE_HEADERS);


    proxy.newHar("label_for_har");      


    driver.manage().window().maximize();
    driver.get("http://username:pswd@url.com");        
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    Robot rb = new Robot();
    rb.keyPress(KeyEvent.VK_ENTER);     

    //do something on page
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    WebElement citySelect = driver.findElement(By.xpath("dropdown_xpath"));
    Select dropdown= new Select(select_a_dropdown_menuitem);            
    dropdown.selectByVisibleText("Text_displayed");
    //driver.navigate().refresh();

}

@Test
public void testCaseOne() throws AWTException {
    /*driver.manage().window().maximize();
    driver.get("http://username:pswd@url.com");        
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    Robot rb = new Robot();
    rb.keyPress(KeyEvent.VK_ENTER);     

    //do something on page
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    WebElement citySelect = driver.findElement(By.xpath("dropdown_xpath"));
    Select dropdown= new Select(select_a_dropdown_menuitem);            
    dropdown.selectByVisibleText("Text_displayed");
    //driver.navigate().refresh();*/

}

@AfterTest
public void tearDown() {

    // get the HAR data
    Har har = proxy.getHar();

    // Write HAR Data in a File
    File harFile = new File(sFileName);
    try {
        har.writeTo(harFile);
        ReadHAR reading = new ReadHAR();
        reading.main(null);

    } catch (IOException ex) {
         System.out.println (ex.toString());
         System.out.println("Could not find file " + sFileName);
    }   

    /*if (driver != null) {
        proxy.stop();
        //driver.quit();
    }*/
}
 }

第2课: -

public class ReadHAR {
public static void main(String[] args) {
String filename = new String();
filename = BrowserMobExample.sFileName;
 // System.out.println("This is the file location " + filename);

File f = new File(filename);
HarFileReader r = new HarFileReader();
HarFileWriter w = new HarFileWriter();

try
{
  System.out.println("Reading " + filename);
  HarLog log = r.readHarFile(f);

  // Access all elements as objects
  HarBrowser browser = log.getBrowser();
  HarEntries entries = log.getEntries();

  // Used for loops
  List<HarPage> pages = log.getPages().getPages();
  List<HarEntry> hentry = entries.getEntries();
  String string1 = "p_still_media";
  String string2 = "m_still_media";
  String string3 = "T-Map";


   /* for (HarPage page : pages)
  {
    System.out.println("page start time: "
        + ISO8601DateFormatter.format(page.getStartedDateTime()));
    System.out.println("page id: " + page.getId());
    System.out.println("page title: " + page.getTitle());

    }*/

  File varTmpDir = new File(System.getProperty("user.dir") + "\\CaptureNetworkTraffic\\results.txt");
  boolean exists = varTmpDir.exists();
  System.out.println("  result.txt : " +exists);
  System.out.println(" file path:"+ varTmpDir);
  if(exists){
      varTmpDir.delete();
  }
  File result = new File(System.getProperty("user.dir") + "\\CaptureNetworkTraffic\\results.txt");

 // result.createNewFile();
  result.setWritable(true);
  //FileWriter fw = new FileWriter(result, false);
 // PrintWriter out = new PrintWriter(fw);



  //Output "response" code of entries.
  for (HarEntry entry : hentry)
  {
     // System.out.println("IP is : " + entry.getServerIPAddress());

      if(entry.getRequest().getUrl().contains(string2)/* & entry.getResponse().getStatus() !=200*/){
      System.out.println("    Url is : " + entry.getRequest().getUrl() + "    response code: " + entry.getResponse().getStatus()); //Output url of request
     // Files.newBufferedWriter(result, StandardOpenOption.APPEND);
    //  out.write("    Url is : " + entry.getRequest().getUrl() + "    response code: " + entry.getResponse().getStatus());
     // BufferedWriter bw = new BufferedWriter(fw);
     System.out.println("IP is :- "+entry.getServerIPAddress());
      FileWriter fw = new FileWriter(result, true);
      BufferedWriter out = new BufferedWriter(fw);

      out.write("    Url is : " + entry.getRequest().getUrl() + "    response code: " + entry.getResponse().getStatus());
      out.newLine();
      out.close();
      fw.close();

      }
      else if(entry.getRequest().getUrl().contains(string3) /*& entry.getResponse().getStatus() !=200*/){
         // System.out.println("request code: " + entry.getRequest().getMethod()); //Output request type
          System.out.println("    Url is: " + entry.getRequest().getUrl() + "    response code: " + entry.getResponse().getStatus()); //Output url of request
         // out.write("    Url is : " + entry.getRequest().getUrl() + "    response code: " + entry.getResponse().getStatus());
          FileWriter fw = new FileWriter(result, true);
          BufferedWriter out = new BufferedWriter(fw);

          out.write("    Url is : " + entry.getRequest().getUrl() + "    response code: " + entry.getResponse().getStatus());
          out.newLine();
          out.close();
          fw.close();

      }
      else if(entry.getRequest().getUrl().contains(string1) /*& entry.getResponse().getStatus() !=200*/){
         // System.out.println("request code: " + entry.getRequest().getMethod()); //Output request type
           System.out.println("    Url is: " + entry.getRequest().getUrl() + "    response code: " + entry.getResponse().getStatus()); //Output url of request
           //out.write("    Url is : " + entry.getRequest().getUrl() + "    response code: " + entry.getResponse().getStatus());
         //  out.close();
           //System.out.println("    response code: " + entry.getResponse().getStatus()); // Output the
           FileWriter fw = new FileWriter(result, true);
           BufferedWriter out = new BufferedWriter(fw);

           out.write("    Url is : " + entry.getRequest().getUrl() + "    response code: " + entry.getResponse().getStatus());
           out.newLine();
           out.close();
           fw.close();

      }

  }

  /*
  // Once you are done manipulating the objects, write back to a file
  System.out.println("Writing " + "fileName" + ".test");
  File f2 = new File("fileName" + ".test");
  w.writeHarFile(log, f2);
  */
}
catch (JsonParseException e)
{
  e.printStackTrace();
  //fail("Parsing error during test");
}
catch (IOException e)
{
  e.printStackTrace();
  //fail("IO exception during test");
}
}

}

生成ChromeDriver日志但是页面未加载。随着firefox页面加载但没有捕获日志(我认为geckodriver browsermob代理不兼容或此功能尚不可用)

我错过了什么或者问题是什么?一个多星期以来坚持这一点。在ubuntu上尝试了上述内容,但面临同样的问题。

非常感谢有关此问题的任何帮助。

0 个答案:

没有答案