cheezy / page-object

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

Handling module namespace using @current_page #430

Closed pgundlupetvenkatesh closed 7 years ago

pgundlupetvenkatesh commented 7 years ago

I have been using page-object for a while now, recently our automation team had to restructure the framework to qualify each page object model class within module which made @current_page stop working for calling objects. Does PageObject support @current_page on classes qualified with module?

# Ruby class file
module MyModule
    class MyClass
        include PageObject
        include PageFactory
        text_field(:first_name, id: ‘firstName’)
        button(:click_save_button, id: ‘save_button’)
    end
end
# Cucumber step file
When /^I save record$/ do
    on(MyModule::MyCLass).first_name = ‘Pratik’
    @current_page.click_save_button          # This doesn’t work
end

One alternative I found was to have all actions of the page in a block. But, using @current_page was way better to use.

When /^I save record$/ do
   on MyModule::MyClass do |page|
      page.first_name = 'Pratik'
      page.click_save_button    # This worked
   end
end
vveliev commented 7 years ago

:/ This does not work ?!

module Pages
  module UCP
    class Base
.
.
.
#in the step 
visit Pages::UCP::Base
pgundlupetvenkatesh commented 7 years ago

@vveliev visit() and on() works just fine, the problem is when I use @current_page after on() where class is qualified with module namespace.

This works.

class MyCLass1
    include PageObject
    include PageFactory
    text_field(:first_name, id: 'firstName')
    button(:save, id: 'saveButton')
end

module MyModule
  class MyCLass2
    include PageObject
    include PageFactory
    text_field(:last_name, id: 'LastName')
    text_field(:age, id: 'Age')
  end
end

# Cucumber step file
When /^I enter details and save the record$/ do
  on(MyCLass1).first_name = ‘Pratik’
  @current_page.save          # This works just fine
  on(MyModule::MyCLass2).last_name = 'G V'
  @current_page.age = '20'    # This doesn't work. Here @current_page has MyClass1 instance
end

Here is the behavior on how @current_page works

https://github.com/cheezy/page-object/wiki/Creating-and-using-page-objects#pagefactory

thamilton2014 commented 7 years ago

It seems to update the @current_page instance fine for me though I'm not using it within Cucumber. There could be some strange collision causing an issue for you.

screen shot 2017-07-23 at 10 49 41 pm
vveliev commented 7 years ago

I'm using cucumber and it's working just fine for me, never had any issues.

Will need more info.

pgundlupetvenkatesh commented 7 years ago

Thanks @vveliev and @thamilton2014

I did a sample test on a setup and it's working now. I have to test it on the production code which would take a week or two to kick off. Please do not close this issue, I will get back.

cheezy commented 7 years ago

@pgundlupetvenkatesh I'm going to close this issue since you have it working. Please reopen if you find that this is happening in your production code.