The trend and best practice in test automation is to move away from writing lots of functional test in Selenium. So, we want to try to replace high level Selenium test with Integration and Unit tests as much as possible.

Pros of Integration test:

  • Easier to write in comparison to Unit test (no need mocking)
  • Able to utilize test data in test database
  • Able to eliminate the need of some Selenium test

Cons of Integration test:

  • Data reliability issue due to dependency on test data

Grails testing framework provide a lot of convenience to achieve the above. The scenario below shows I eliminate the need of Selenium test:

  1. In DataSource.groovy change test to look point at test database
test {
    dataSource {
        username = "adam"
        password = "eve"
        url = 'jdbc:oracle:thin:@test.oracle.com.au:1521/adamAndEve'
    }
}

2.  Create an Integration test repo via command. The command will create a repo called “TestRepo” under “test” directory in the project file structure.

$ grails create-integration-test TestRepo

3.  Example of integration that:

  • Finds a book using its unique Id
  • Checks that the book title is correct
@Integration
class BookSpec extends IntegrationSpec {

    void "a book is returned"() {

        //Finding the item ID in the test database that you have defined in DataSource
        given:"we find a book with id"
          def bookId=Item.get(15102876765285400414)

        //Call Book object by passing the unique Id
        when: "we call the find function"
          def book=Book.findByItem(bookId)

        //Assert the Book object returned
        then: "book will be returned"
          Assert.assertTrue(book.title.equals("The Happy Pig"))
    }

4. Example of integration test that checks for UI. The test below:

  • Finds the book using Id
  • Check that the book displays a link named Buy Now
@Integration

class BookTagLibTests extends GroovyPagesTestCase {

    void testShowBuyBook(){

        //Find the book using Id
        def bookId=Item.get(15110576705972615055)

        //Checks that the book displays Buy Now link which will activate a JavaScript function upon click
        applyTemplate('<item:bookBanner item="${item}"/>', [item:bookId] ).contains("<a id='book-buy-${bookId.id}' class='btn carf na' href='javascript:buyBook(\"${bookId.id}\")' data-original-title='' title=''>Buy Now</a>")
    }

Both examples above shows that we could replace certain Selenium scenarios with Integration test, including those that involves UI.