cheezy / page-object

Gem to implement PageObject pattern in watir-webdriver and selenium-webdriver
MIT License
653 stars 220 forks source link

Pass helper class instance into PageObject #469

Closed chbndrhnns closed 5 years ago

chbndrhnns commented 5 years ago

I am new to page-object, cucumber, and watir so I might be missing something that is not related to page-object. Here is my qustion:

How do I make the BrowserHelper class instance available for my BasePage class during runtime?

I have these page objects:

class BasePage
  include PageObject

  page_url "bla"
end

class LoginPage < BasePage
  page_url "bla/login"

  text_field(:username, id: "user")
  button(:login, id: "submit")

  def do_login
    self.username = "abc"
    @browser_helper.take_screenshot
    login
  end
end

Part of my env.rb:

require "page-object"
require "page-object/page_factory"

@browser_helper = BrowserHelper.new
@browser = browser_helper.instance

In my env.rb, I have assign the driver instance to @browser and my helper class instance to @browser_helper. I would like this @browser_helper to be available in my page objects. Can I achieve this somehow? Or should I optimize my design?

jkotests commented 5 years ago

@chbndrhnns , can you elaborate on the purpose of @browser_helper?

chbndrhnns commented 5 years ago

My issue was not understanding Ruby enough. I just wanted to have access to modules I included in my env.rb via the world command (like World(My helper))) fromage objects. I found it is possible via MyWorld.method(), for example.