page-object gem seems not working

泪湿孤枕 提交于 2019-12-25 04:53:15

问题


I am trying to use page-object gem in my watir-webdriver scripts and I think I might be missing something.

Here is my code for log_in.rb: require "./all_deals"

class LogIn
  include PageObject

  text_field(:email, :id => 'UserName')
  text_field(:password, :id => 'Password')
  checkbox(:remember_me, :id => 'RememberMe')
  button(:log_me_in, :value => 'Login')

  def initialize(browser)
    @browser = browser
  end

  def log_in (email, password)
    self.email = email
    self.password = password
    remember_me
    log_me_in
    AllDeals.new(@browser)
  end
end

My home_page.rb

require "./log_in"

  class HomePage

    def initialize(browser)
      @browser = browser
    end

    def visit
      @browser.goto "http://treatme.co.nz/"
    end

    def go_to_log_in
      @browser.goto "https://treatme.co.nz/Account/LogOn"
      LogIn.new(@browser)
    end
  end

Here is my log_in_test.rb

require "rubygems"
require "test/unit"
require "watir-webdriver"
require "page-object"
require "./home_page"


class LogInTest < Test::Unit::TestCase
  def setup
    @browser ||= Watir::Browser.new :firefox
  end

  def teardown
   @browser.close
  end

  def test_fail
    @home_page = HomePage.new(@browser)
    @home_page.visit
    @log_in_page = @home_page.go_to_log_in
    @all_deals = @log_in_page.log_in("test@gmail.com","test")
    assert (@all_deals.get_title == "GrabOne Daily Deals - Buy Together, Save Together")
  end
end

The result of the test run is:

Finished tests in 22.469286s, 0.0445 tests/s, 0.0000 assertions/s.

  1) Error:
test_fail(LogInTest):
NoMethodError: undefined method `text_field_value_set' for nil:NilClass
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/page-object-0.9.2/lib/page-object/accessors.rb:142:in `block in text_field'

I am using Ruby 1.9 with page-object gem 0.9.2.

Can you please help me?

Also in each of those rb file, I need to require the class files it references, is there a way I don't have to declare it every time?

Thanks so much.


回答1:


Addressing the Exception

That exception is occurring do to the LogIn page re-defining the initialize method.

class LogIn
  include PageObject

  def initialize(browser)
    @browser = browser
  end
end

When you include PageObject, it already adds a specific initialization method. Your class is overriding that initialization and presumably causing something to not get setup correctly.

Removing the initialize method will get you past that exception.

However, you will then hit a remember_me is not a variable/method exception. Assuming you want to check the checkbox, it should be check_remember_me.

Requiring Class Files

I usually have my test file require a file that requires all my page_objects. It is a similar concept to how your would require any other gem or library.

For example, I would create a treatme.rb file with:

require 'log_in'
require 'home_page'

Assuming you require the files in the required order (ie files that are needed by others are required first), none of your page object files (ie log_in.rb and home_page.rb) should need to do any requiring.

Your test files would then just require your treatme.rb file. Example:

require './treatme'


来源:https://stackoverflow.com/questions/19473510/page-object-gem-seems-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!