capybarasite-prism

Is it possible to use a class variable with a SitePrism module


I'm writing a test step and I need to give Capybara's page.check() method the name value for my checkbox with the name of checkbox_name. I wanted to save that as a class variable in my page object model like so:

#class_name_page.rb

class ClassNamePage < SitePrism::Page

  set_url "/cool/url"

  element :save_button, "input#save_button"

  @@checkbox = "checkbox_name"

  def self.checkbox
    @@checkbox
  end

end

Then, I'd call it here in my test step.

# test_steps.rb

When(/^creates a field with the name "(.*?)" that is enabled$/) do |field_label|
  @site_pages.class_name_page.load
  @site_pages.class_name_page.set field_label

  page.check(@site_pages.class_name_page.checkbox)

  @site_pages.class_name_page.save_button.click
end

Thing is when I try this, or using an instance variable, I get an undefined method error. Is there any way I can call this constant from the page object model or identify it in SitePrism?


Solution

  • You've defined checkbox as a class method but you're calling it on an instance. To access it the way you've defined it you'd need to do

    page.check(@site_pages.class_name_page.class.checkbox)
    

    Note that a better solution may be to just declare the checkbox in your ClassNamePage

    element :my_checkbox, :checkbox, 'checkbox_name'
    

    and then you could do

    @site_pages.class_name_page.my_checkbox.set(true)