If you really must write your Selenium framework in Java…

First, why? Just because your application is written in Java does not mean you have to write your Selenium framework in it. JRuby or Jython are far better options for you. But let’s say you are determined to do it, what are some things you need to keep in mind? Here are two that I have encountered this week.

Properties Files

Java’s native method for configuration information is the java.util.properties class. So if you are going to pass information into your framework, and let’s face it, its a framework so you will, use properties files. Not Excel, csv, yaml or some crazy propriety format. Use properties. Not only is it built into the language but it is a text format so can be checked into version control (and diff’ed, etc.).


This builds off of the first point. Do not put paths into your code! Java has this thing called the classpath. You know, that thing that tells the JVM where to look for things — like properties files! Conveniently enough, you can parse a properties file as a stream from a file that was located in the classpath.

package ca.element34.flyingmonkey.environment;

import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

public class ReadEnvironment {
  static Logger log4j = Logger.getLogger("ca.element34.flyingmonkey.environment.ReadEnvironment");

  public Properties ReadEnvironment() throws Exception {
    Properties environmentProps = new Properties();
    InputStream is = this.getClass().getResourceAsStream("/environment.properties");
    return environmentProps;

Some things to keep in mind with this:

  • The directory that contains the environment.properties file needs to be in the classpath. Don’t put the file itself in the classpath. You have no idea how many times I made that mistake when I was at HP…
  • You need to have the leading / in the filename. Without it, the package hierarchy is searched and not the classpath
  • You will likely have a number of environments that your framework needs to interact with, so have an environment.properties.development, an environment.properties.staging, an evironment.properties.production, etc. But don’t have checked in an actual environment.properties file. Instead, create a symlink to the file on the local machine.

Part of my role as a Selenium Consultant is to dive into a team’s code and spot things that will likely hurt them in the long run. My typical technique is to run the framework on a platform they are not using — it should work. And when it doesn’t, these two tricks will go a long way in moving it a lot closer to working.

And this isn’t to pick on just Java. Python and Ruby have similar native tricks for accomplishing this as well.

Post a Comment

Your email is never published nor shared. Required fields are marked *