Unable to get Ruby Page-Object gives error "undefined method"

439 views Asked by At

I am new to using PageObject::PageFactory.

I can’t get this simple scenario to work. Can someone Help me with this?

My Feature file

Feature: Find Pens

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

My 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

Page Object file

QUESTION: Is it right to put this file in Support folder or should this be living elsewhere?

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

The Given part when run with Cucumber-SeleniumWebdriver does open the Amazon browser, but after that gives the following error for the When part

The Error

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”‘

My env.rb in the support folder contains:

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

There are 1 answers

3
roonie On

Typically page object files are maintained in a separate folder that is at the same level as the features directory. You will need to use additional require statements so that Ruby knows that they are located in that folder.

Re the error: Looks like the compiler is having difficulty recognizing that look_pens is a method of the HomePage class. Try moving the |page| from the end of the When step def to the second line, so that it looks like code below.

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