Search code examples
rubytestingautomationcalabashpageobjects

Following Test Automation best practise of "Methods return other PageObjects" in Ruby


I am a big advocate of the Page Object Pattern (POP) as defined by the experts at Selenium: https://code.google.com/p/selenium/wiki/PageObjects

A key view of theirs that I have always followed when using Appium with Java is: "Methods return other PageObjects"

e.g. LoginPage loginPage = homePage.gotoLoginPage();

I am now trying to following POP using Calabash with Ruby and so have been writing code like this:

e.g. @login_page = @home_page.goto_login_page

However, since Ruby doesn't know what type of object @login_page is or @home_page is, you dont get any of the benefits of intellisense showing what methods are available for a given page.

Anyone know a good way around this?


Solution

  • Found a way of getting this to work after much research and applying well known Java/C#/Obj-c principles to Ruby:

    Given(/^I am on the launch page$/) do
      @launch_page ||= LaunchPage.new
    end
    
    When(/^I open the set alarm time page$/) do
      @set_alarm_page = @launch_page.goto_set_alarm_page
    end
    
    When(/^I open our apps from the home page$/) do
      @launch_page.navigation_toolbar.open_our_apps
    end
    
    Then(/^I should see the homepage alarm time is (\d+)$/) do |alarm_time|
      alarm_time_actual = @launch_page.get_alarm_time
      assert_equal(alarm_time, alarm_time_actual)
    end
    

    As long as somewhere on the step definition class you explicitly create a new page object (in the above example: LaunchPage.new), then all subsequent pages will provide intellisense method/property values, since the resulting page types returned will be known by RubyMine.