Selenium Trick: Using a different DNS

I love Selenium. It’s a great tool, that does a damn fine job. But one thing I’ve been wanting to do for a while is to get it to use a different DNS server to the box it’s running on.

Here’s how you do it:

java -Dsun.net.spi.nameservice.provider.1=dns,sun -Dsun.net.spi.nameservice.nameservers= -jar selenium-server.jar

(Caveat: may not work on non-Sun JVMs. Does work on OSX, though…)

I wanted this because we use VM-based test environments, each with a DNS server. To date when we’ve been running tests against these environments, we’ve manually configured the test client to use the appropriate DNS sever. Now, however, I can make the environment name an argument to the test scripts.

For more details on these command-line arguments, see the official documentation.


Update: It turns out that there were some extra subtleties to take into account.

  1. By default, Selenium doesn’t configure the client browser to use itself as a proxy server; it normally runs in the Heightened Privileges mode.
  2. The proxy injection mode changes the behaviour of Selenium, and in my case didn’t work*
  3. I could force the use of the proxy by telling my Selenium client to add custom request headers (see below). That worked – except for SSL.
  4. SSL didn’t work because Selenium creates an SSL tunnel (to deal with certificates, I guess). To do that, it calls out to InetAddress.getLocalHost(). If your machine name isn’t ‘localhost’, this will try to do a DNS lookup – out to the server you configured for your DNS, which in my case didn’t know my machine.

Still, it did work and do the job that I wanted. Problem solved. 🙂 At least for Firefox – different browsers seem to only be able to use a system proxy (IE/Safari), or always use Selenium as the proxy (Google Chrome). This area is more than a little “bleeding edge”, and in hindsight it would probably have been easier just to hack my hosts file.

Here’s how I told my selenium-client (in Ruby) to use custom request headers:

@browser.start_new_browser_session(:addCustomRequestHeaders => true)

Having an explicit “use proxy” mode would have been nicer, though.

* The problem with using proxyInjection with FireFox is probably just mine; I got a clash about the version of libsqllite3.0.dylib

Advertisements

Author: Robert Watkins

My name is Robert Watkins. I am a software developer and have been for over 18 years now. I currently work for people, but my opinions here are in no way endorsed by them (which is cool; their opinions aren’t endorsed by me either). My main professional interests are in Java development, using Agile methods, with a historical focus on building web based applications. I’m also a Mac-fan and love my iPhone, which I’m currently learning how to code for. I live and work in Brisbane, Australia, but I grew up in the Northern Territory, and still find Brisbane too cold (after 16 years here). I’m married, with two children and one cat. My politics are socialist in tendency, my religious affiliation is atheist (aka “none of the above”), my attitude is condescending and my moral standing is lying down.

1 thought on “Selenium Trick: Using a different DNS”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s