无法获取Ruby Page-Object给出错误“未定义的方法”

时间:2015-06-09 21:59:36

标签: selenium rubygems tdd cucumber

我是使用PageObject::PageFactory的新手。

我不能让这个简单的场景起作用。有人可以帮我这个吗?

我的功能文件

Feature: Find Pens

Scenario:
 Given a user goes to Amazon website
 When they search for “pens”
Then they are able to find “pens”

我的Step_definition

Given(/^a user goes to Amazon website$/) do
 visit HomePage
 end

When(/^they search for “(.*?)”$/) do |page|
 on HomePage do
 page.look_pens
 end
 end

Then(/^they are able to find “(.*?)”$/) do |arg1|
 pending # express the regexp above with the code you wish you had
 end

页面对象文件

问题:将此文件放在支持文件夹中是否正确,还是应该在其他地方生效?

class HomePage
 include PageObject

page_url(‘http://www.amazon.co.uk’)

text_field(:name, :id=> ‘twotabsearchtextbox’) #:id is the web-element from the actual webpage. Should this be something else?
 button(:search, :value=>’Go’)

def look_pens(name,search)
 self.name = pens
 self.search
 end
 end

使用Given运行时Cucumber-SeleniumWebdriver部分会打开亚马逊浏览器,但之后会为When部分提供以下错误

错误

p.rb:1
 When they search for “pens” # features/step_definitions/buy_pens_ste
 p.rb:5
 undefined method `look_pens’ for “pens”:String (NoMethodError)
 ./features/step_definitions/buy_pens_step.rb:7:in `block (2 levels) in ‘
./features/step_definitions/buy_pens_step.rb:6:in `/^they search for “(.*?
 )”$/’
features\buy_pens.feature:5:in `When they search for “pens”‘

支持文件夹中的env.rb包含:

require ‘page-object’
require ‘selenium-webdriver’
require ‘page-object/page_factory’

Before do
 @browser = Selenium::WebDriver.for(:firefox)
 end

After do
 @browser.close
 end

World(PageObject::PageFactory)

1 个答案:

答案 0 :(得分:0)

通常,页面对象文件保存在与features目录处于同一级别的单独文件夹中。您将需要使用其他require语句,以便Ruby知道它们位于该文件夹中。

重新出错:看起来编译器很难识别出look_pens是HomePage类的一种方法。尝试移动|页面|从步骤def结束到第二行,以便它看起来像下面的代码。

When(/^they search for “(.*?)”$/) do
 on HomePage do |page|
  page.look_pens
 end
end