Skip to content

Commit

Permalink
Merge pull request webmetrics#54 from andyclark/feature/get-selenium-…
Browse files Browse the repository at this point in the history
…proxy-tests-working

Get Selenium Proxy tests working
  • Loading branch information
lightbody committed Jan 8, 2014
2 parents 401c8f1 + 495fdf9 commit 1f236f5
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 117 deletions.
65 changes: 52 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,35 @@
</execution>
</executions>
</plugin>
<plugin>
<!-- Fetch PhantomJS for us -->
<groupId>com.github.klieber</groupId>
<artifactId>phantomjs-maven-plugin</artifactId>
<version>0.2.1</version>
<executions>
<execution>
<id>fetch-phantom-js</id>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- Fetch this version of PhantomJS -->
<version>1.9.2</version>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<!-- Make downloaded PhantomJS available to tests -->
<systemPropertyVariables>
<phantomjs.binary.path>${phantomjs.binary}</phantomjs.binary.path>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
Expand Down Expand Up @@ -189,12 +218,6 @@
<version>4.2.3</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>

<dependency>
<groupId>net.sf.jopt-simple</groupId>
<artifactId>jopt-simple</artifactId>
Expand Down Expand Up @@ -245,27 +268,29 @@
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>2.37.1</version>
<version>2.39.0</version>
</dependency>

<dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>2.35.0</version>
<version>2.39.0</version>
<scope>test</scope>
</dependency>

</dependency>
<dependency>
<groupId>com.github.detro.ghostdriver</groupId>
<artifactId>phantomjsdriver</artifactId>
<version>1.0.3</version>
<version>1.0.4</version>
<scope>test</scope>
</dependency>



<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<version>4.11</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -295,4 +320,18 @@
</plugins>
</reporting>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>2.39.0</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
33 changes: 32 additions & 1 deletion src/main/java/net/lightbody/bmp/proxy/ProxyServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void start() throws Exception {
public org.openqa.selenium.Proxy seleniumProxy() throws UnknownHostException {
Proxy proxy = new Proxy();
proxy.setProxyType(Proxy.ProxyType.MANUAL);
String proxyStr = String.format("%s:%d", getLocalHost().getCanonicalHostName(), getPort());
String proxyStr = String.format("%s:%d", getConnectableLocalHost().getCanonicalHostName(), getPort());
proxy.setHttpProxy(proxyStr);
proxy.setSslProxy(proxyStr);

Expand All @@ -107,12 +107,43 @@ public void setPort(int port) {
this.port = port;
}

/**
* Get the the InetAddress that the Proxy server binds to when it starts.
*
* If not otherwise set via {@link #setLocalHost(InetAddress)}, defaults to
* 0.0.0.0 (i.e. bind to any interface).
*
* Note - just because we bound to the address, doesn't mean that it can be
* reached. E.g. trying to connect to 0.0.0.0 is going to fail. Use
* {@link #getConnectableLocalHost()} if you're looking for a host that can be
* connected to.
*/
public InetAddress getLocalHost() throws UnknownHostException {
if (localHost == null) {
localHost = InetAddress.getByName("0.0.0.0");
}
return localHost;
}

/**
* Return a plausible {@link InetAddress} that other processes can use to
* contact the proxy.
*
* In essence, this is the same as {@link #getLocalHost()}, but avoids
* returning 0.0.0.0. as no-one can connect to that. If no other host has
* been set via {@link #setLocalHost(InetAddress)}, will return
* {@link InetAddress#getLocalHost()}
*
* No attempt is made to check the address for reachability before it is
* returned.
*/
public InetAddress getConnectableLocalHost() throws UnknownHostException {
if (getLocalHost().equals(InetAddress.getByName("0.0.0.0"))) {
return InetAddress.getLocalHost();
} else {
return getLocalHost();
}
}

public void setLocalHost(InetAddress localHost) throws SocketException {
if (localHost.isAnyLocalAddress() ||
Expand Down
125 changes: 67 additions & 58 deletions src/test/java/net/lightbody/bmp/proxy/MailingListIssuesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,74 +351,83 @@ public void process(BrowserMobHttpRequest request, Har har) {
@Test
public void issue27() throws Exception{
// see: https://github.com/lightbody/browsermob-proxy/issues/27

WebDriver driver = null;
// start the proxy
ProxyServer server = new ProxyServer(4444);
server.start();
server.setCaptureHeaders(true);
server.setCaptureContent(true);

// get the selenium proxy object
Proxy proxy = server.seleniumProxy();
DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability(CapabilityType.PROXY, proxy);

// start the browser up
WebDriver driver = new FirefoxDriver(capabilities);

server.newHar("assertselenium.com");

driver.get("http://whatsmyuseragent.com");
//driver.get("https://google.com");

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

// make sure something came back in the har
Assert.assertTrue(!har.getLog().getEntries().isEmpty());

// show that we can capture the HTML of the root page
String text = har.getLog().getEntries().get(0).getResponse().getContent().getText();
Assert.assertTrue(text.contains("<title>Whats My User Agent?</title>"));

server.stop();
driver.quit();
try {
server.setCaptureHeaders(true);
server.setCaptureContent(true);

// get the selenium proxy object
Proxy proxy = server.seleniumProxy();
DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability(CapabilityType.PROXY, proxy);

// start the browser up
driver = new FirefoxDriver(capabilities);

server.newHar("assertselenium.com");

driver.get("http://whatsmyuseragent.com");
//driver.get("https://google.com");

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

// make sure something came back in the har
Assert.assertTrue(!har.getLog().getEntries().isEmpty());

// show that we can capture the HTML of the root page
String text = har.getLog().getEntries().get(0).getResponse().getContent().getText();
Assert.assertTrue(text.contains("<title>Whats My User Agent?</title>"));
} finally {
server.stop();
if (driver != null) {
driver.quit();
}
}
}

@Test
public void googleCaSslNotWorkingInFirefox() throws Exception{
WebDriver driver = null;
// start the proxy
ProxyServer server = new ProxyServer(4444);
server.start();
server.setCaptureHeaders(true);
server.setCaptureContent(true);

// get the selenium proxy object
Proxy proxy = server.seleniumProxy();
DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability(CapabilityType.PROXY, proxy);

// start the browser up
WebDriver driver = new FirefoxDriver(capabilities);

server.newHar("Google.ca");

driver.get("https://www.google.ca/");

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

// make sure something came back in the har
Assert.assertTrue(!har.getLog().getEntries().isEmpty());

// show that we can capture the HTML of the root page
String text = har.getLog().getEntries().get(0).getResponse().getContent().getText();
Assert.assertTrue(text.contains("<title>Google</title>"));

server.stop();
driver.quit();
try {
server.setCaptureHeaders(true);
server.setCaptureContent(true);

// get the selenium proxy object
Proxy proxy = server.seleniumProxy();
DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability(CapabilityType.PROXY, proxy);

// start the browser up
driver = new FirefoxDriver(capabilities);

server.newHar("Google.ca");

driver.get("https://www.google.ca/");

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

// make sure something came back in the har
Assert.assertTrue(!har.getLog().getEntries().isEmpty());

// show that we can capture the HTML of the root page
String text = har.getLog().getEntries().get(0).getResponse().getContent().getText();
Assert.assertTrue(text.contains("<title>Google</title>"));
} finally {
server.stop();
if (driver != null) {
driver.quit();
}
}
}


Expand Down
Loading

0 comments on commit 1f236f5

Please sign in to comment.