Sunday, March 31, 2013

test report generation (50 mcg)

Report generation is a common function of the applications especially financial applications. For example, all the banks provide statement download function through their websites. I am not sure how they test that functionality, this is what I did for one of my client.

The report downloading process contains following steps, thus a template method or composite approach will be suitable to design a generic ReportDownload, let me design an interface first,

public interface Report{

public interface ReportDownload {
   void navigate();
   void enterCriteria(ReportCriteria criteria);
   void triggering();
   Clickable trigger();
   Report findReport();
   Report resolveExpection(Report actual)

Report is a type, based on its file type, it can be a CSV, Excel or PDF document.

This is an article about how to save the downloaded file a certain folder, selenium forefox profile, with design, we can add a variable to hold the directory of the download files as per test base, so the findReport() method can look into that directory to find newly arrived file, in that way, you don't need to specify the download file name in the test. To test the result, a simple way is to use pre generation file as an expectation to compare the downloaded report. The expectation can be stored in a classpath directory which can be resolved by the file name of the actual report.

Once expectation and actual report are founded, we can compare two files to find out whether they are same, we can override the equals method to compare the content of the two files.

Report actual = page.findReport();
Report expected = page.resolveExpectation();
assertEquals(expected, actual)

Saturday, March 23, 2013

tackle the incompatibility of selenium jar file with firefox binary version (120 mcg)

If we don't specify the firefox binary location, Selenium web driver will use the default firefox binary location, on Windows, it is C:\Program Files\Mozilla\Firefox\. When firefox upgrade to a newer version, the web driver may fail to trigger the firefox since some API may be changed. Selenium developers will release a newer version of selenium jar file for you to upgrade, but unless you upgrade you dependency file, the test will not work and it can cause frustration.

There is a way to overcome this, to include firefox binary in a jar file and add a maven step to explode it to file system before starting the tests, and you can also include a customized profile in the jar file so it can support file download of known file formats, for example, CSV, PDF and Excel formats. It also saves each individual developer's time in manipulating profile which is also error prone. You can include firebug plugin with the binary so you can use it to examine web elements when running the test on a debug mode.

if the above step is too difficult to achieve, there is another simpler approach, just copy target firefox binary and customized profiles to a location and pass the location as a JVM option variable to the test, the test class then load the firefox binary from the location,

   public static WebDriver getWebDriver() {
      FirefoxBinary firefox = new FirefoxBinary(new File(System.getProperty("firefox.binary"));
      FirefoxProfile profile = new FirefoxProfile(new File(System.getProperty("firefox.profile")
      return new FirefoxDriver(firefox, profile);
This is the constructor used by the method.

and set the following JVM options variables,


/projects/test/firefox is the unix style for file system folder, it works for Windows as well, it points to C:\projects\test\firefox, where firefox.exe is located for the tests.

This solved the problem caused by the incompatibility of web driver and firefox browser version and make the test more stable.

This solution has evolved into a more advance form, a browser factory using enum

use enum to increase the readability of the test (50 mcg)

Using enum can greatly increase the readability of the code and it may significantly reduce the complexity as well. Let us start with some code not using enum,
    DateUtils.getDate(2012, 5, 6)
It is not clear which month it is. Does it mean May or June? Worse thing is in one library it may mean May and in another library it means June. I added another method to make the following call possible, with static import in Java, it look like this,
    date(June, 6, 2012)
I believe even non technical people can read it and understand now, it make the code much easier to understand.
   public enum Months {
I don't need to rewrite new method, I just need to call the old method in the new method,
    static Date date(Enum month, int day, int year) {
       DateUtils.getDate(year, month.ordinal(), day)
Sometimes, on the web layer, the month may be spelled as a short form, then we can introduce another enum,
   public enum ShortMonths {
We can pass this ShortMonths and make the call like this,
    date(Jun, 6, 2012)
DatePicker uses this pattern.