Skip to content

Commit 1d4a404

Browse files
committed
updated readme, native support for Selenium 2, nice released artifact
1 parent c922ef4 commit 1d4a404

File tree

5 files changed

+157
-4
lines changed

5 files changed

+157
-4
lines changed

README

Whitespace-only changes.

README.md

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
BrowserMob Proxy
2+
================
3+
4+
BrowserMob Proxy is a simple utility that makes it easy to capture performance data from browsers, typically written using automation toolkits such as Selenium and Watir.
5+
6+
Features
7+
--------
8+
9+
The proxy is programmatically controlled via a REST interface or by being embedded directly inside Java-based programs and unit tests. It captures performance data the [HAR format](http://groups.google.com/group/http-archive-specification). It addition it also can actually control HTTP traffic, such as:
10+
11+
- blacklisting and whitelisting certain URL patterns
12+
- simulating various bandwidth and latency
13+
- remapping DNS lookups
14+
- flushing DNS caching
15+
- controlling DNS and request timeouts
16+
- automatic BASIC authorization
17+
18+
REST API
19+
--------
20+
21+
To get started, first start the proxy by running `browsermob-proxy` or `browsermob-proxy.bat` in the bin directory:
22+
23+
$ sh browsermob-proxy -port 9090
24+
INFO 05/31 03:12:48 o.b.p.Main - Starting up...
25+
2011-05-30 20:12:49.517:INFO::jetty-7.3.0.v20110203
26+
2011-05-30 20:12:49.689:INFO::started o.e.j.s.ServletContextHandler{/,null}
27+
2011-05-30 20:12:49.820:INFO::Started [email protected]:9090
28+
29+
Once started, there won't be an actual proxy running until you create a new proxy. You can do this by POSTing to /proxy:
30+
31+
[~]$ curl -X POST http://localhost:9090/proxy
32+
{"port":9091}
33+
34+
Once that is done, a new proxy will be available on the port returned. All you have to do is point a browser to that proxy on that port. The following additional APIs will then be available:
35+
36+
- DELETE /proxy/port - shuts down the proxy and closed the port
37+
- GET /proxy/port/har - returns the JSON/HAR content representing all the HTTP traffic passed through the proxy
38+
39+
*TODO*: Other REST APIs supporting all the BrowserMob Proxy features will be added soon.
40+
41+
Embedded Mode
42+
-------------
43+
44+
If you're using Java and Selenium, the easiest way to get started is to embed the project directly in your test. First, you'll need to make sure that all the dependencies are imported in to the project. You can find them in the *lib* directory. Or, if you're using Maven, you can add this to your pom:
45+
46+
<dependency>
47+
<groupId>org.browsermob</groupId>
48+
<artifactId>browsermob-proxy</artifactId>
49+
<version>2.0-SNAPSHOT</version>
50+
</dependency>
51+
52+
*TODO*: We haven't yet released the artifacts to Maven's central repository, but we are working on it. The above will work as soon as it's ready.
53+
54+
Once done, you can start a proxy using `org.browsermob.proxy.ProxyServer`:
55+
56+
ProxyServer server = new ProxyServer(9090);
57+
server.start();
58+
59+
This class supports every feature that the proxy supports. In fact, the REST API is a subset of the methods exposed here, so new features will show up here before they show up in the REST API. Consult the Javadocs for the full API.
60+
61+
Using With Selenium
62+
-------------------
63+
64+
You can use the REST API with Selenium however you want. But if you're writing your tests in Java and using Selenium 2, this is the easiest way to use it:
65+
66+
// start the proxy
67+
ProxyServer server = new ProxyServer(4444);
68+
server.start();
69+
70+
// get the Selenium proxy object
71+
Proxy proxy = server.seleniumProxy();
72+
73+
// configure it as a desired capability
74+
DesiredCapabilities capabilities = new DesiredCapabilities();
75+
capabilities.setCapability(CapabilityType.PROXY, proxy);
76+
77+
// start the browser up
78+
WebDriver driver = new FirefoxDriver(capabilities);
79+
80+
// create a new HAR with the label "yahoo.com"
81+
server.newHar("yahoo.com");
82+
83+
// open yahoo.com
84+
driver.get("http://yahoo.com");
85+
86+
// get the HAR data
87+
Har har = server.getHar();
88+
89+
90+
HTTP Request Manipulation
91+
-------------------
92+
93+
While not yet available via the REST interface, you can manipulate the requests like so:
94+
95+
server.addRequestInterceptor(new HttpRequestInterceptor() {
96+
@Override
97+
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
98+
request.removeHeaders("User-Agent");
99+
request.addHeader("User-Agent", "Bananabot/1.0");
100+
}
101+
});
102+
103+
The interceptor is the type `org.apache.http.HttpRequestInterceptor`, which is part of the [Apache HTTP Client](http://hc.apache.org/httpcomponents-client-ga/) project. You can consult the API docs for the full set of options available to you in the interceptor.
104+
105+
We will soon be adding support for this advanced capability in the REST interface as well, using JavaScript snippets that can be posted as the interceptor code.
106+
107+
SSL Support
108+
-----------
109+
110+
While the proxy supports SSL, it requires that a Certificate Authority be installed in to the browser. This allows the browser to trust all the SSL traffic coming from the proxy, which will be proxied using a classic man-in-the-middle technique. IT IS CRITICAL THAT YOU NOT INSTALL THIS CERTIFICATE AUTHORITY ON A BROWSER THAT IS USED FOR ANYTHING OTHER THAN TESTING.
111+
112+
If you're doing testing with Selenium, you'll want to make sure that the browser profile that gets set up by Selenium not only has the proxy configured, but also has the CA installed. Unfortuantely, there is no API for doing this in Selenium, so you'll have to solve it uniquely for each browser type. We hope to make this easier in upcoming releases.

pom.xml

+23-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
<packaging>jar</packaging>
1111

1212
<parent>
13-
<groupId>org.sonatype.oss</groupId>
14-
<artifactId>oss-parent</artifactId>
15-
<version>7</version>
13+
<groupId>org.sonatype.oss</groupId>
14+
<artifactId>oss-parent</artifactId>
15+
<version>7</version>
1616
</parent>
1717

1818
<licenses>
@@ -126,6 +126,20 @@
126126
</execution>
127127
</executions>
128128
</plugin>
129+
<plugin>
130+
<groupId>org.apache.maven.plugins</groupId>
131+
<artifactId>maven-javadoc-plugin</artifactId>
132+
<version>2.7</version>
133+
<executions>
134+
<execution>
135+
<id>attach-javadocs</id>
136+
<phase>package</phase>
137+
<goals>
138+
<goal>jar</goal>
139+
</goals>
140+
</execution>
141+
</executions>
142+
</plugin>
129143
</plugins>
130144
</build>
131145
<dependencies>
@@ -229,6 +243,12 @@
229243
<artifactId>jcip-annotations</artifactId>
230244
<version>1.0</version>
231245
</dependency>
246+
247+
<dependency>
248+
<groupId>org.seleniumhq.selenium</groupId>
249+
<artifactId>selenium-api</artifactId>
250+
<version>2.0b3</version>
251+
</dependency>
232252
</dependencies>
233253

234254
<reporting>

src/main/assembly.xml

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,24 @@
33
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
44
<id>bin</id>
55
<formats>
6-
<format>tar.gz</format>
76
<format>zip</format>
87
</formats>
98
<fileSets>
109
<fileSet>
1110
<directory>${project.basedir}/target/appassembler</directory>
1211
<outputDirectory>/</outputDirectory>
1312
</fileSet>
13+
<fileSet>
14+
<directory>${project.basedir}/src/main/resources/sslSupport</directory>
15+
<outputDirectory>/ssl-support</outputDirectory>
16+
</fileSet>
17+
<fileSet>
18+
<directory>${project.basedir}/target</directory>
19+
<includes>
20+
<include>browsermob-proxy-${project.version}-sources.jar</include>
21+
<include>browsermob-proxy-${project.version}-javadoc.jar</include>
22+
</includes>
23+
<outputDirectory>/</outputDirectory>
24+
</fileSet>
1425
</fileSets>
1526
</assembly>

src/main/java/org/browsermob/proxy/ProxyServer.java

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.browsermob.proxy.jetty.http.SocketListener;
1212
import org.browsermob.proxy.jetty.jetty.Server;
1313
import org.browsermob.proxy.jetty.util.InetAddrPort;
14+
import org.openqa.selenium.Proxy;
1415

1516
import java.util.Date;
1617

@@ -54,6 +55,15 @@ public void start() throws Exception {
5455
server.start();
5556
}
5657

58+
public org.openqa.selenium.Proxy seleniumProxy() {
59+
Proxy proxy = new Proxy();
60+
proxy.setProxyType(Proxy.ProxyType.MANUAL);
61+
proxy.setHttpProxy("localhost:4444");
62+
proxy.setSslProxy("localhost:4444");
63+
64+
return proxy;
65+
}
66+
5767
public void cleanup() {
5868
handler.cleanup();
5969
}

0 commit comments

Comments
 (0)