"undefined method " error when calling adoption_helper

33 views
Skip to first unread message

Matthew Middleton

unread,
Jul 2, 2016, 2:45:34 PM7/2/16
to Cucumber and Cheese
When I run the script that is supposed to call adoption_helper.rb, I get the following error message:

C:/Users/mmiddleton/git/CucumberAndCheese/CucumberAndCheese/adoption_helper.rb:4:in `goto_puppy_adoption_site': undefined method `goto' for nil:NilClass (NoMethodError)
from C:/Users/mmiddleton/git/CucumberAndCheese/CucumberAndCheese/adopt_puppies_with_helper.rb:7:in `<main>'

I copied the methods from the previous script, and pasted them in adoption_helper.rb, and copied the lines that call those methods in to a script I called adopt_puppies_with_helper.rb, so I know the spelling is correct. I'm pretty sure that I've capitalized the Module declaration and call correctly, so I can't figure out what's on the go. Below are the two scripts:

adoption_helper.rb

module AdoptionHelper
  
  def goto_puppy_adoption_site
    @browser.goto 'http://puppies.herokuapp.com'
  end
  
  def adopt_puppy_number(num)
    @browser.button(:value => 'View Details', :index => num - 1).click
    @browser.button(:value => 'Adopt Me!').click
  end
  
  def continue_adopting_puppies
    @browser.button(:value => 'Adopt Another Puppy').click
  end
  
  def checkout_with(name, address, email, pay_type)
    @browser.button(:value => 'Complete the Adoption').click
    @browser.text_field(:id => 'order_name').set(name)
    @browser.text_field(:id => 'order_address').set(address)
    @browser.text_field(:id => 'order_email').set(email)
    @browser.select_list(:id => 'order_pay_type').select(pay_type)
    @browser.button(:value => 'Place Order').click
  end
  
  def verify_page_contains(text)
    fail 'Browser text did not match expected value' unless @browser.text.include? text
  end
  
  def close_browser
    @browser.close
  end
end

adopt_puppies_with_helper.rb

require 'rubygems'
require 'watir-webdriver'
require_relative 'adoption_helper'

include AdoptionHelper

goto_puppy_adoption_site
adopt_puppy_number 1
continue_adopting_puppies
adopt_puppy_number 2
checkout_with('Cheezy', '123 Main St.', 'che...@foo.com', 'Check')
verify_page_contains 'Thank you for adopting a puppy!'
close_browser

Matthew Middleton

unread,
Jul 10, 2016, 5:18:45 PM7/10/16
to Cucumber and Cheese
Thanks to some help from Jeff Morgan, I found the issue. I wasn't initializing the @browser variable. For those of us who aren't programmers by trade, another little tidbit - initialize the @browser variable in your main script, not your helper script.
Reply all
Reply to author
Forward
0 new messages