Saturday, May 24, 2014

separate test data from page

1. Input Data

Test data can be organized into Java Value Object or POJO as people call it, then used as the parameter for the methods of Page Object. For example, in a shopping page, it can have methods such setBillingAddress, setCreditCard and setOtherInformation which take a parameter object such as Address, CreditCard and OtherInfomation, etc.



Here is the Address Java Value Object, it is a POJO, since all its properties are final, all its fields can be public without getter to save some extra code.


The address can be a bean in a spring config file, or a row in jBehave table, depends on your preference,



Then the test can be as simple as this,



This example just showed you how to separate test data from page and use the data to populate the form with input, radio button, select and checkbox widgets. However, It is not the ultimate way to manage test data. There are other frameworks, i.e. jBehave which can be used to organize the tests in tabular format. Please check out their websites for more information.


2. Output Data

Expectation data can also be organized into Java objects, for example, ErrorMessages,





So in the test, you can just call,
        assertEquals(expectedErrorMessages, cartPage.getErrorMessages());

And in the case of test failure, it will display meaningful error messages,

On page,



On IDE,



Please implement toString method of this expectation class, without it, the reported error would be like following, which really doesn't tell us much.

2 comments:

  1. Hi, does your ErrorMessages class need to override the hashCode() method or is it ok in this case?

    ReplyDelete
    Replies
    1. According to Joshua Block's Effective Java, it should have a hashCode method, however, it won't be used in any hash based collections, so it is OK not to have a hashCode method.

      Delete