2

I would like to use the page-objects gem in my selenium test projects, but before I can do that I need to get a small proof of concept project working so I can play around with it.

I have been following the Get Started Now! guide on github, but for the life of me I can not figure out what I am doing wrong. Admittedly, I am a ruby newbie and there is probably a very big "duh" moment coming up.

Here is my practice page.rb:

require 'page-object'

class SearchPage   
  include PageObject

  text_field(:searchBar, :id => 'q')  
  button(:submitSearch, :name => 'btnK')

  def search(query)
    self.searchBar = query
    submitSearch
  end

  def open()
    @browser.get 'https://www.google.com'
  end

  def close()
    @browser.close()
  end

end

And here is my practice test.rb:

require_relative 'page'
require 'selenium-webdriver'
require 'test/unit'

class SearchPageTest < Test::Unit::TestCase
    def test_search_page
        @browser = Selenium::WebDriver.for :chrome
        search_page = SearchPage.new(@browser)
        search_page.open()
        search_page.search('page-objects')
        sleep 5
        search_page.close()
    end
end

This fails at line 10 in the test (search_page.search...) with the error

NoMethodError: undefined method `text_field' for #Selenium::WebDriver::Chrome::Driver:0x007fd37c1c1838

Here is the full stack trace:

Nates-MacBook-Air:Practice natesmith$ ruby test.rb 
Loaded suite test
Started
E
================================================================================
Error: test_search_page(SearchPageTest): NoMethodError: undefined method `text_field' for #<Selenium::WebDriver::Chrome::Driver:0x007fe64d2c60d8>
/Users/natesmith/.rvm/gems/ruby-2.4.1/gems/page-object-2.2/lib/page-object/platforms/watir/page_object.rb:1067:in `instance_eval'
/Users/natesmith/.rvm/gems/ruby-2.4.1/gems/page-object-2.2/lib/page-object/platforms/watir/page_object.rb:1067:in `instance_eval'
/Users/natesmith/.rvm/gems/ruby-2.4.1/gems/page-object-2.2/lib/page-object/platforms/watir/page_object.rb:1067:in `process_watir_call'
/Users/natesmith/.rvm/gems/ruby-2.4.1/gems/page-object-2.2/lib/page-object/platforms/watir/page_object.rb:216:in `text_field_value_set'
/Users/natesmith/.rvm/gems/ruby-2.4.1/gems/page-object-2.2/lib/page-object/accessors.rb:203:in `block in text_field'
/Users/natesmith/Desktop/Ruby/Practice/page.rb:10:in `search'
test.rb:10:in `test_search_page'
      7:         @browser = Selenium::WebDriver.for :chrome
      8:         search_page = SearchPage.new(@browser)
      9:         search_page.open()
  => 10:         search_page.search('page-objects')
     11:         sleep 5
     12:         search_page.close()
     13:     end
================================================================================


Finished in 2.875252 seconds.
--------------------------------------------------------------------------------
1 tests, 0 assertions, 0 failures, 1 errors, 0 pendings, 0 omissions, 0 notifications
0% passed
--------------------------------------------------------------------------------
0.35 tests/s, 0.00 assertions/s

Help, what am I doing wrong?

  • Does the browser correctly open and navigate to google.com prior to the failure? – Sam Woods Aug 16 '17 at 19:09
  • Yes the browser opens, and it appears to be navigating to the google homepage but quits before anything is visible – Natels Aug 17 '17 at 16:00
  • Could you include the stack trace? There are a number of possibilities, but I think if you give the stack trace I could probably figure out why it is erroring. – Sam Woods Aug 17 '17 at 20:01
  • @SamWoods I have included them now in the question. – Natels Aug 17 '17 at 21:40
1

It looks like page-object is using the Watir implementation instead of the Webdriver implementation, and since Webdriver does not have a text_field property (while Watir does) it is failing. I am not sure why it is trying to use Watir since your implementation matches the documentation for the page-object gem. It looks like someone is having a similar issue with the latest version: https://github.com/cheezy/page-object/issues/439. You might try using an older version of the page-object gem until that gets resolved.

  • Thanks for your help. I followed the link to the issue and they have now resolved this issue. After updating the gem it is working as I would expect now – Natels Aug 24 '17 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.