Selenium is a browser automation framework, primarily used for testing—if you're already doing end-to-end tests in your application, it's a good bet you're using Selenium. Using Selenium and PhantomJS, you can automate your WonderProxy-driven GeoIP testing right on your local machine.
Step 1: Set up your local Selenium environment
Initializing a local Selenium server is nice and easy, so we'll do that first.
Go get one of the stand-alone Java archives (JARs) from
$ wget https://selenium-release.storage.googleapis.com/3.4/selenium-server-standalone-3.4.0.jar
Spin it up with your local Java installation:
It starts with some sensible defaults, but you can configure them to your heart's content! Run the server with a
$ java -jar selenium-server-standalone-3.4.0.jar
-hflag for details.
Step 2: Get PhantomJS
PhantomJS is a "headless" browser engine that acts just like a regular browser without the graphical user interface. It's ideal for testing website behavior and content when you're not overly concerned with styling and display.
- Follow the official download instructions. (On bigger Linux distributions, you can generally use the native package manager.)
Make sure the
phantomjsexecutable is in your
$ sudo ln -s /path/to/phantomjs /usr/local/bin/phantomjs
Step 3: Configure Selenium to use WonderProxy
Writing Selenium tests is extensively covered in books,
so we won't go in depth on the topic here. Instead, we'll focus
on integrating WonderProxy using Selenium's
DesiredCapabilities allows testers to
customize browser configurations; in our case, we will
customize PhantomJS to proxy its connections through a WonderProxy
The PHP and Python demo tests read your WonderProxy credentials from the local environment, so set that up first:
$ export WONDERPROXY_USER=yourusername $ export WONDERPROXY_PASS=yourpassword
In the code samples below, we pass a proxy server (like
denver.wonderproxy.com:11000) into the
proxied() method as an argument. Then,
proxied() plugs our proxy and proxy credentials into
Selenium's PhantomJS driver, using
proxied() returns the customized driver,
which will direct all its requests through the proxy server.
Step 4: Get testing!
Now that we've configured Selenium's WebDriver to shunt connections through WonderProxy, we can write some tests using the proxied driver.
The PHPUnit testing framework supports "parameterized" tests (tests
that can be run multiple times with varied input) out of the box
@dataProvider notation. In this sample, the
userLocations() method will be the input source for our
test. It include three sets of input, each containing the proxy to
be tested and the expected output.
The test itself grabs a proxied driver, then loads up a web page
and checks the
The element should read
Toronto for the
Vancouver for the
framework does not include parameterized test
support, but the gist is the same as above: The test creates a
proxied driver, then checks the
against the proxy.
These code samples are part of a working demo, which is available on Github.