lightbody / browsermob-proxy

A free utility to help web developers watch and manipulate network traffic from their AJAX applications.
http://bmp.lightbody.net
Apache License 2.0
2.17k stars 659 forks source link

can browsermob-proxy support seleniume grid? #865

Open ChrisD1990 opened 4 years ago

ChrisD1990 commented 4 years ago

hi,

i meet a issue, as our automation is seleniume grid, i have 1 hub node and 9 salve node, now i want to use the browsermorb-proxy, I don’t know which salve node it will run automation case, so can you help tell me should i to do? thanks

Below is my code: public void startproxy(String url) { BrowserMobProxy proxy = new BrowserMobProxyServer(); proxy.start(0); // get the Selenium proxy object RemoteWebDriver rDriver = (RemoteWebDriver) ((WebDriverFacade) getDriver()).getProxiedDriver(); String browser = rDriver.getCapabilities().getBrowserName().toLowerCase(); // String browser = ConfigParser.getBrowser(); Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);

    String ipAddress = new NetworkUtils().getIp4NonLoopbackAddressOfThisMachine().getHostAddress();
    int port = proxy.getPort();
    seleniumProxy.setHttpProxy(ipAddress + ":" + port);

    if (browser.equalsIgnoreCase("chrome")) {
        DesiredCapabilities chromeCapabilities = DesiredCapabilities.chrome();
        chromeCapabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
        String chromeBinary = System.getProperty(" ");
        if (chromeBinary == null || chromeBinary.equals("")) {
            String os = System.getProperty("os.name").toLowerCase().substring(0, 3);
            chromeBinary = "C:\\Automation\\SeleniumGrid\\chromedriver"+ ".exe";
            System.setProperty("webdriver.chrome.driver", chromeBinary);
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--start-maximized");
        }
        proxy.enableHarCaptureTypes(CaptureType.getAllContentCaptureTypes());
        driver = new RemoteWebDriver(chromeCapabilities);

// driver = new ChromeDriver(chromeCapabilities); } else { System.out.println("Using default browser as firefox"); DesiredCapabilities capabilities = DesiredCapabilities.firefox(); capabilities.setCapability("marionette", true); driver = new FirefoxDriver(capabilities); } try { proxy.newHar("swtestacademy"); driver.get(url); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } Har har = proxy.getHar(); File harFile = new File("academy.har"); har.writeTo(harFile); } catch (IOException ioe) { ioe.printStackTrace(); } // driver.quit(); // proxy.stop(); }

jjqi92 commented 3 years ago

while open page , always show cannot connect network, can you help me see see?

jjqi92 commented 3 years ago

![Uploading image.png…]()

jjqi92 commented 3 years ago
private static DesiredCapabilities startBrowserMobProxy() {
    DesiredCapabilities capabilities = new DesiredCapabilities();
    BrowserMobProxy browserMobProxy = new BrowserMobProxyServer();
    browserMobProxy.setTrustAllServers(true);
    browserMobProxy.start(9092);
    browserMobProxy.enableHarCaptureTypes(CaptureType.getAllContentCaptureTypes());

    String hostIp = new NetworkUtils().getIp4NonLoopbackAddressOfThisMachine().getHostAddress();
    Proxy proxy = ClientUtil.createSeleniumProxy(new InetSocketAddress(hostIp, browserMobProxy.getPort()));

    proxy.setHttpProxy(hostIp + ":" + browserMobProxy.getPort());
    proxy.setSslProxy(hostIp + ":" + browserMobProxy.getPort());

    capabilities.setCapability(CapabilityType.PROXY, proxy);
    capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    capabilities.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);

    browserMobProxyThreadLocal.set(browserMobProxy);
    System.out.println("jvm-assign port for bmp:" + browserMobProxy.getPort());
    return capabilities;
}