Skip to content

4 Defining Regions

Justin Watts edited this page Apr 5, 2017 · 10 revisions

Ferris provides 2 mechanisms for defining elements based on other elements (sections/regions).

Through Watir directly.

Replace browser with the name of the element you want to use as your parent element.

class Foo < Ferris::Core
  element(:parent) { browser.div(id: 'foo') }
  element(:child) { parent.text_element(id: 'bar') }
end

Thanks to blocks, you can also pass in information dynamically when desired.

class Foo < Ferris::Core
  element(:foo) { |index| browser.div(class: 'foo', index: index) }
end

This allows you to access different elements by passing in parameters along with the name of the element. So Foo.new.foo(0) will return the first div element that has a class 'foo', etc.

Via Ferris Regions

In this approach, you define a method called region which takes a name, class, and a block.

Singular Region

class MyPage < Ferris::Core
  region(:header, MyHeader) { browser.div(id: 'head') }
end

class MyHeader < Ferris::Core
  element(:username) { root.div(id: 'user') }
  element(:logout_btn) { root.button(id: 'button') }
end

# Usage
page = MyPage.new
page.header.present?          # Checks the presence of the element used to define the region
page.header.logout_btn.click  # Clicks element defined within the header region

# Cool RSpec usage
expect(page.header).to be_present

Page Regions - They Will Change Your Life. Seriously. #TBUS #LoblawDigital

class MyPage < Ferris::Core
  region(:faqs, MyFaqs) { browser.divs(class: 'faq') }
end

class MyFaqs < Ferris::Core
  element(:question) { root.div(id: 'q') }
  element(:answer) { root.button(id: 'a') }

  def has_a_question?
    question.present?
  end

  def has_an_answer?
    answer.present?
  end
end

# Usage
page = MyPage.new
page.faqs.size                            # returns number of faqs which exist in the DOM
page.faqs.each {|faq| puts faq.question } # outputs question for each faq
page.faqs.map(&:present?).all?            # returns true if all faqs are visible

# Cool RSpec usage
expect(page.faqs).to all(have_a_question)
expect(page.faqs).to all(have_an_answer)