Create your Selenium test reports with Ant

In the previous blog post I described how to JUnit test your Selenium tests. Once you have this set up, you can use Ant to run this tests and create a report about the results.

Since there are standard targets for JUnit and JUnitReport in Ant, the Ant file is pretty clean. We start with setting up our environment, including a clean target. The tool I use to display the XML on my blog makes it a bit messy, but you get the idea:












    
    
    
    
    
    



    

We set up the output directories, the reports will come in the reports directory. In the Lib directory, we put the selenium standalone jar file that is used to run the Selenium test in JUnit. As well as the JUnit jar to run the JUnit test itself.

After that there are two targets, prepare and compile:

    
     
    



    
    

In the prepare we set up the structure and create the directories that we need. In the compile we look for all the java sources in the src directory and compile them to the classes directory. Now all that is left is a target to run the JUnit and create the reports:

  
    
      
        
      
    
    
    
  
  
    
      
    
    
  

I choose not to split those two target, since I never want to run a test without creating the reports or vice versa. The JUnit runs all the tests that are in the classes dir, where the compile target puts his output, he runs the JUnit test using the classpath that is set up in the environment and puts this in the output directory.
After this the JUnitReport tasks picks up the reports from the output directory and creates a HTML structure off all the tests that are ran. This is how the index.html looks:

  
You see an overview of the tests, the failures and erros, all clickable to drill down for more information.
If you like you can browse through the packages & classes.

JUnit test your application with Selenium

We want to test our ADF application with Selenium. To get this working, we use the Selenium IDE (Firefox plugin) to record a session. You can export this recorded session to different formats, since we are mainly Java/ADF developers at our project, we choose to export to Java.

Because we build ADF applications with the JDeveloper IDE, this is the preferred IDE to build the JUnit test as well. In the Fusion WebApplication I created an extra Project called ‘SeleniumTest’.


To get it working, you need to have the JUnit extension, you can easily install this through the Help -> Check for update menu.
Next to that, you also need to add the selenium-server-standalone jar, to your project. You can download the latest version from the Selenium download page.

First we’re going to record a session with Selenium. Start your application, open it in firefox and open the Selenium plugin. After that click the record button.


Now just navigate to your application, in this example I got a form and a table, I click the next button two times navigating to the ‘Purchasing’ department. For this example we’ll keep the test clean and simple.


Stop the record session by clicking the record button again, you’ll see it has recorded the clicks.
If you want, you can replay it with the play button to check if it works.


Now export your file through the menu File -> Export -> Java / JUnit 4 / WebDriver.


Save this Java file in your SeleniumTest project, after this you need to rename your package in the java file. In this java class you see a few methods, we’ll focus on two off them for now, the setUp and testHr.

 @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://127.0.0.1:7101/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

Here the Firefox driver is initiated and the baseUrl is set.

  @Test
  public void testHr() throws Exception {
    driver.get(baseUrl + "/SeleniumAntJenkins/faces/untitled1.jspx");
    driver.findElement(By.cssSelector("#b3 > a.xg7 > span.xgf")).click();
    driver.findElement(By.cssSelector("#b3 > a.xg7 > span.xgf")).click();
  }

The testHr method is the actual JUnit test that get launched after the setUp.
There is one bug in this generated class. As you can see the baseUrl ends on a slash and the first line in the testHr method adds a slash. I prefer to change the baseUrl and remove the last slash behind the portnumber.

Now your test is ready to run.
Just right click the java class and run (or debug, as you prefer).


You’ll see that a FireFox browser is launched (with the text WebDriver in the right bottom). It follows the steps in your test case and closes the browser again. After that you see the green positive result in the JUnit test runner.


That’s all it takes to set up a simple JUnit test that does a functional (screen) test with the help of Selenium in the JDeveloper IDE.