Skip to content
Scott Clark edited this page Oct 12, 2015 · 13 revisions

The LitePage source is extremely small. As such, the best way to get familiar with the library is most likely to simply read the source or the yard docs. Regardless, documentation has been written up on the different parts of LitePage in this wiki:

Documentation by Example

Perhaps the best way to gain an intuition for how LitePage works is simply by taking a look at a page class that uses LitePage, and then what that same class would look like if we were to explicitly define each of the methods that would be dynamically defined using LitePage instead.

Thus, this....

class LoginPage
  include LitePage
  page_url('http://www.example.com/login')

  def_elements(:@browser, {
    :username_field => [:text_field, :id => 'username'],
    :password_field => [:text_field, :id => 'password'],
    :log_in_button => [:button, :value => 'Log In']
  })

  def log_in(username, password)
    self.username_field.set(username)
    self.password_field.set(password)
    self.log_in_button.click
  end
end
# If using Cucumber
World(LitePage::PageInitializers)

@browser = Watir::Browser.new
visit(LoginPage).login('afinch', 'mockingbird60')

Would expand out to this...

class LoginPage
  def initialize(browser)
    @browser = browser
  end

  def page_url(query_params = {})
    uri = URI('http://www.example.com/login')
    existing_params = URI.decode_www_form(uri.query || '')
    new_params = query_params.to_a

    unless existing_params.empty? && new_params.empty?
      combined_params = existing_params.push(*new_params)
      uri.query = URI.encode_www_form(combined_params)
    end

    uri.to_s
  end

  def username_field(selectors = {})
    @browser.text_field({:label => 'username'}.merge(selectors))
  end

  def password_field(selectors = {})
    @browser.text_field({:label => 'password'}.merge(selectors))
  end

  def log_in_button(selectors = {})
    @browser.button({:text => 'Log In'}.merge(selectors))
  end

  def log_in(username, password)
    self.username_field.set(username)
    self.password_field.set(password)
    self.log_in_button.click
  end
end
@browser = Watir::Browser.new
login_page = LoginPage.new(@browser)

@browser.goto(login_page.page_url)
login_page.login('afinch', 'mockingbird60')
Clone this wiki locally