Check out the Latest Blog posts:
Compiling a Java code for Bromine in Eclipse

Bromine v3 requires the Java code to be compiled and packed in an executable JAR. In the previous tutorial we covered the recording of the test case with Selenium IDE and its conversion to a Bromine ready format. Now we will focus on the compiling of the Java code, and see two ways to create the executable JAR file in Eclipse. We assume you already have downloaded Eclipse and set it up, so we can jump into creating magic.

  • Open the Java project creation wizard (File -> New -> Other -> Java Project) 01_0
  • Enter a Project name. ALAStest in our case.
  • Make sure to select “Use project folder as root for sources and class files” in the Project Layout section. This will be crucial for the second way of creating the JAR file, and I will describe this later on in this tutorial.  02_0
  • Click Finish, and a new project is created.
  • Now we want to add the Java code for our test case. As I mentioned earlier, we used the Selenium IDE to record the test case, and we exported it to Bromine friendly Java code. Create a new Java Class file, and leave the Package field empty. Once its done, copy the exported code to this file, and make sure all of the existing code in it is rewritten. There is another, easier way to do this file creation in Eclipse. In Package Explorer click the project and just paste the copied code. Eclipse will create a new Java file for you and place it in the appropriate location.
  • Here is the code that we will use:
import java.io.PrintWriter;
import java.io.StringWriter;
import bromine.brunit.BRUnit;

public class TestNew extends BRUnit {

 public void testNew() throws Exception {
   selenium.open("/");
   verifyTrue(selenium.isTextPresent("We Do QA"));
   selenium.click("//li[@id='projects']/a");
   selenium.waitForPageToLoad("30000");
   verifyTrue(selenium.isTextPresent("We Do QA"));
   selenium.click("link=We Do QA");
   selenium.waitForPageToLoad("30000");
   verifyTrue(selenium.isTextPresent("ALAS"));
 }

 public static void main(String[] args) {
   try{
     String host = args[0];
     int port = Integer.parseInt(args[1]);
     String brows = args[2];
     String sitetotest = args[3];
     String uid = args[4];
     String tid = args[5];
     String brows2 = brows+','+uid;
     TestNew t = new TestNew();
     t.setUp(host, port, brows2, sitetotest, uid, tid);
     try{
       t.testNew();
     }catch(Exception e){
       StringWriter sw = new StringWriter();
       PrintWriter pw = new PrintWriter(sw);
       e.printStackTrace(pw);
       t.customCommand("An Exception occured in the test", "failed", sw.toString() , "");
     }
     t.tearDown();
   }
   catch(Exception e){e.printStackTrace(System.out);
   }
 }
}
  • As you can see, Eclipse reports lots of errors in our code, so lets fix this. All we need to do is to add the dependency jars to the build path. We can do that by right click on the project in the Package Explorer and selecting Build Path -> Configure Build Path  03_0
  • Select the Libraries tab, and click on the Add External Jars. Find “your www folder\app\webroot\testscripts\Google Sample\jar\lib” and add all the jars from that folder, and click OK to close the Properties window.  04_0
  • Now we need to create a Run Configuration, so right click on the project and choose Run As -> Run Configurations…  05_0
  • Double click on Java Application to create a new configuration
  • Name the new configuration and select TestNew as main class  06_0
  • Apply the changes and close the window. We do not want to run it yet.
  • At this moment we have set up a project, and we can move on to packaging it to a JAR

Creating the JAR, the easy way

We will create the executable JAR with Eclips’ help, and pack all of the dependency jars inside our new package.

  • Right click on the project and choose Export  07_0
  • Select Runnable JAR file from the list and click Next. It can be found under Java.
  • Select the run configuration we have created previously
  • Choose an export destination
  • Select “Package required libraries into generated JAR”  08_0
  • Click Finish to create the JAR file.

This is the easier way, but it takes up more space due to the included dependency jars. Now if we have more then one test case, we will have the same dependency jars in all of them. The solution is to move these files outside our JAR file, and to include them in the classpath.

Creating the JAR, the right way

We will use an ANT script to build our executable JAR file, so we will ask Eclips again for help to create the base of the script, so we don’t have to write it from scratch.
Before we start, I have to make a little digression here. In order to use the dependency jars, we have to point to there location, and to avoid using absolute paths, we need to make sure the “Copy Java libs to the project?” checkbox is clicked when a new project is created in Bromine. Bromine will create a new folder for each project, and by ensuring this checkbox is clicked we will have a lib folder inside the created project folder with all of the dependency jars in it.
Now that this is clear, we can move on to creating the ANT script.

  • Right click on the project and choose Export
  • Select Runnable JAR file from the list and click Next. It can be found under Java.
  • Select the run configuration we have created previously
  • Choose an export destination
  • Select “Copy required libraries into a sub-folder next to the generated JAR”
  • Check the Save as ANT script checkbox, and add a location where to save it.  09_0
  • Click Finish to create the JAR file and to create the ANT script.
  • At this moment we can ignore the created JAR, and focus on the ANT script.
  • Eclipse has set up all absolute paths, so we need to change them to relative ones for a more robust script. We can also remove the part where a lib folder is created and all the jars are copied to it, because Bromine has already put them to the right space. After all modifications the script should look something like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="create_run_jar" name="Create Runnable Jar for Project ALAStest with libraries in sub-folder">
 <!--this file was created by Eclipse Runnable JAR Export Wizard-->
 <!--ANT 1.7 is required                                        -->
 <target name="create_run_jar">
   <jar destfile="./ALAStest.jar">
     <manifest>
       <attribute name="Main-Class" value="TestNew"/>
       <attribute name="Class-Path" value=". lib/BRUnit.jar lib/commons-lang-2.4.jar lib/mysql-connector-java-5.1.7-bin.jar lib/selenium-java-client-driver.jar"/>
     </manifest>
     <fileset dir="./"/>
   </jar>
 </target>
</project>
We are almost all done, and there is only one thing left to do: Run the ANT script.
  • Right click on the ANT script file and select Run As -> Ant Build  10_0
  • A success message should be displayed in the console along with some basic information regarding the running of the Ant Build.

Now we have a valid JAR file that we can use with Bromine.

Free PDF    Send article as PDF   


  1. [...] A duplicate of this post can be seen on the author’s personal blog: http://vilmoss.com/ [...]

  2. Mark on Tuesday 20, 2010

    Hi, I was wondering if it was possible to compile the test cases directly into Bromine, without having to upload a new script every time. It looks like Bromine makes a copy of the jar, and renames it to a number in a specific directory.
    It would be very useful to be able to use ant to, every time you make changes to a bunch of test cases, put it directly into Bromine and be able to test right away instead of manually re-uploading.
    Is this possible? And if so, could you explain how to do this?

    Great tutorials though, thanks.

  3. Vilmos Somogyi on Tuesday 20, 2010

    Yes, you are correct about the renaming and copying files. Bromine is keeping all java related files in app/webroot/testscripts/[name of the project]/jar and the php files in app/webroot/testscripts/[name of the project]/php. The actual number of the test case can be figured out from the url: http://localhost/requirements#/testcases/view/335 (Planning->Click on the testcase). In our case its 335, so now we know what the file name should be, and where to put it. Only modify the destfile tag in your ant script so it points to that location, and that the filename is the number of the testcase.

    This is only a short description of the solution, and I’ll do my best to create a fancy tutorial for this aswel.

  4. [...] How to use Eclipse to create a JAR for Bromine [...]

  5. Advoftadvassy on Tuesday 20, 2010

    Hello all! I like this forum, i organize many gripping people on this forum.!!!

    Great Community, respect all!