Reputation: 631
I am using the page-object gem. Suppose i have a page-object on features/bussines/pages/booking_page.rb
for a page like:
class Booking
include PageObject
span(:txtFirstName, :id => 'details_first_name')
end
...and i use a "tools" class located at features/support/tools.rb
with something like:
class MyTools
def call_to_page_object
on Booking do |page|
puts page.txtFirstName
end
end
end
...but this approach fails because calling to the object from the class is not allowed:
undefined method `on' for #<Booking:0x108f5b0c8> (NoMethodError)
Pretty sure i'm missing some concept on the way to use the page-object from a class but don't realize whats the problem. Can you please give me an idea about what could be wrong here, please?
Thank you very much!
============================
Justin found the reason why the call to the class crash. The final class code results:
class MyTools
#Include this module so that the class has the 'on' method
include PageObject::PageFactory
def initialize(browser)
#Assign a browser object to @browser, which the 'on' method assumes to exist
@browser = browser
end
def getCurrentRewards
on Booking do |page|
rewards_text = page.rewards_amount
rewards_amount = rewards_text.match(/(\d+.*\d*)/)[1].to_f
puts "The current rewards amount are: #{rewards_amount}."
return rewards_amount
end
end
end
And the call to the function:
user_rewards = UserData.new(@browser).getCurrentRewards
Why it did not work me? Two main reasons:
Thanks all!
Upvotes: 1
Views: 2393
Reputation: 46846
To use the on
(or on_page
) method requires two things:
PageObject::PageFactory
module.@browser
variable (within the scope of the class) that is the browser.So you could make your MyTools class work by doing:
class MyTools
#Include this module so that the class has the 'on' method
include PageObject::PageFactory
def initialize(browser)
#Assign a browser object to @browser, which the 'on' method assumes to exist
@browser = browser
end
def call_to_page_object
on Booking do |page|
puts page.txtFirstName
end
end
end
You would then be calling your MyTools class like:
#Assuming your Cucumber steps have the the browser stored in @browser:
MyTools.new(@browser).call_to_page_object
Upvotes: 3
Reputation: 629
Your class should use the extend
keyword to access special class methods like span
:
class Booking
extend PageObject
span(:txtFirstName, :id => 'details_first_name')
end
I hope this works.
Upvotes: 0
Reputation: 57322
What are you trying to do?
Did you read Cucumber & Cheese book?
Pages should be in the features/support/pages
folder. You can put other files that pages need there too.
If you want to use on
method in a class, you have to add this to the class:
include PageObject
The code from MyTools class looks to me like it should be in Cucumber step file, not in a class.
Upvotes: 3