Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby beginner trying to call a method from another class in Ruby?
    text
    copied!<p>I am trying to get the below working. Not sure where I am failing. I receive the following error:</p> <p>1) Error:</p> <pre><code>test_google(Google): NoMethodError: undefined method `new' for Method:Class google.rb:15:in `setup' </code></pre> <p>I am new to ruby, so this is pretty 101. Can someone explain to me my errors and why so I can understand. Thanks!</p> <pre><code>require "test/unit" require "selenium-webdriver" require "json" require "time" require_relative "methods" class Google &lt; Test::Unit::TestCase def setup @driver = Selenium::WebDriver.for :firefox @base_url = "https://www.google.com/" @accept_next_alert = true @driver.manage.timeouts.implicit_wait = 30 @verification_errors = [] @search = Method.new() end def teardown @driver.quit assert_equal [], @verification_errors end def test_google @driver.get(@base_url + "/") @search.search end end class Method def search @driver.find_element(:id, "gbqfq").clear @driver.find_element(:id, "gbqfq").send_keys "this is a test" @driver.find_element(:id, "gbqfb").click @driver.find_element(:id, "gbqfb").click end end </code></pre> <p>I changed the class name:</p> <pre><code>require "test/unit" require "selenium-webdriver" require "json" require "time" class Google &lt; Test::Unit::TestCase def setup @driver = Selenium::WebDriver.for :firefox @base_url = "https://www.google.com/" @accept_next_alert = true @driver.manage.timeouts.implicit_wait = 30 @verification_errors = [] @search = Suber.new() end def teardown @driver.quit assert_equal [], @verification_errors end def test_google @driver.get(@base_url + "/") @search.search end end class Suber def search @driver.find_element(:id, "gbqfq").clear @driver.find_element(:id, "gbqfq").send_keys "this is a test" @driver.find_element(:id, "gbqfb").click @driver.find_element(:id, "gbqfb").click end end </code></pre> <p>Now I am not exactly sure how to tackle setting @driver within my 'Suber' class. I assumed it would just work, but it throws: </p> <p>NoMethodError: undefined method <code>find_element' for nil:NilClass google.rb:37:in</code>search' google.rb:25:in `test_google'</p> <p>: /</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload