使用selenium通过代理运行javascript

时间:2018-01-22 17:38:01

标签: javascript java selenium proxy browsermob-proxy

我在Java中使用Selenium 2运行BrowserMobProxyServer(在firefox上),因为我需要在浏览表单时捕获某些网络事件。我有一个javascript命令,我可以使用.executeScript(" script.test")通过JavascriptExecutor调用,但如果代理开启,这不起作用。代理关闭它可以正常工作 - 有没有办法让代理工作或通过代理无法进行javascript注入?

这是我使用代理创建的驱动程序。

             server = new BrowserMobProxyServer();
             server.start();
             int port = server.getPort();
             String proxy_address = "127.0.0.1:"+port+"";
             org.openqa.selenium.Proxy selenium_proxy = new org.openqa.selenium.Proxy();
             selenium_proxy.setHttpProxy(proxy_address)
                     .setFtpProxy(proxy_address)
                     .setSslProxy(proxy_address);
             Proxy proxy = ClientUtil.createSeleniumProxy(server);

             DesiredCapabilities seleniumCapabilities = new DesiredCapabilities();
             seleniumCapabilities.setCapability(CapabilityType.PROXY,selenium_proxy);
             seleniumCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
             seleniumCapabilities.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);
             driver = new FirefoxDriver(seleniumCapabilities);

我的javascript测试如下所示。

    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("script.test"); //Expect to have the form reach the result page
    Assert that an element on the result page is present

0 个答案:

没有答案