Search code examples
rspecfactorywatirpageobjects

NameError: uninitialized constant when calling my PageObject using Test-Factory gem


I have been struggling for a while now with this issue. I looked at too many posts and even tried another similar gem (Page-Object) with the same results.

I seem to be missing a point on how to instantiate an object I guess.

I would appreciate if someone could enlighten me.

Best,

JFF

Here is my script built by following the instructions of the https://github.com/KualiCo/TestFactory:

Base_Page.rb

class MainPage < PageFactory    
  class << self
    def basic_element    
      action(:my_request_is) { |b| b.text_field(name: 'q').set }
      action(:search) { |b| b.button(name: 'btnK').click }
    end
  end
end

search_spec.rb

require 'watir'
require 'test-factory'
require './lib/Base_Page'

include Foundry

RSpec.configure do |config|
  browser = Watir::Browser.new :firefox

  config.before(:all) { @b = browser }
  config.before(:each) { @b.goto("http://google.com") }   
  config.after(:suite) { browser.close unless browser.nil? }
end

describe 'Search_Page' do
  it 'allows me to search for a DataObject' do
    on_page MyPage do |page|
      page.my_request_is "Hello World"
    end
  end
end

ERROR

> Search_Page   allows me to search for a DataObject (FAILED - 1)
> 
> Failures:
> 
>   1) Search_Page allows me to search for a DataObject
>      > Failure/Error:
>      > > on_page MyPage do |page|
>      > > >    page.my_request_is "Hello World"
>      > > end
> 
>      NameError:
>        uninitialized constant MyPage
>      # ./spec/search_spec.rb:20:in `block (2 levels) in <top (required)>'
> 
> Finished in 3.37 seconds (files took 7.25 seconds to load) 1 example,
> 1 failure
> 
> Failed examples:
> 
> rspec ./spec/search_spec.rb:18 # Search_Page allows me to search for a
> DataObject

Solution

  • There are a couple of issues:

    • The page class names do not match - MainPage` vs MyPage.
    • on_page assumes that the browser is stored in @browser (not @b).
    • In the MainPage class, class << self is used in base-like page classes to define sections that can be included into specific page classes. Since you are not using a base class at this point, it can be removed.
    • The block for defining my_request_is needs to accept the supplied parameter.

    Try the following:

    class MainPage < PageFactory
      action(:my_request_is) { |v, b| b.text_field(name: 'q').set(v) }
      action(:search) { |b| b.button(name: 'btnK').click }
    end
    
    RSpec.configure do |config|
      browser = Watir::Browser.new :chrome
    
      config.before(:each) { @browser = browser }
      config.before(:each) { @browser.goto("http://google.com") }
      config.after(:suite) { browser.close unless browser.nil? }
    end
    
    describe 'Search_Page' do
      it 'allows me to search for a DataObject' do
        on_page MainPage do |page|
          page.my_request_is "Hello World"
        end
      end
    end