Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby beginner trying to call a method from another class in Ruby?

Tags:

ruby

I am trying to get the below working. Not sure where I am failing. I receive the following error:

1) Error:

test_google(Google):
NoMethodError: undefined method `new' for Method:Class
    google.rb:15:in `setup'

I am new to ruby, so this is pretty 101. Can someone explain to me my errors and why so I can understand. Thanks!

require "test/unit"
require "selenium-webdriver"
require "json"
require "time"
require_relative "methods"

class Google < 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

I changed the class name:

require "test/unit"
require "selenium-webdriver"
require "json"
require "time"


class Google < 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

Now I am not exactly sure how to tackle setting @driver within my 'Suber' class. I assumed it would just work, but it throws:

NoMethodError: undefined method find_element' for nil:NilClass google.rb:37:insearch' google.rb:25:in `test_google'

: /

like image 249
Josh Avatar asked Feb 12 '26 00:02

Josh


1 Answers

Method is a built in class in Ruby (http://ruby-doc.org/core-2.0/Method.html) You'll have to rename your class to something else.

like image 133
Dogbert Avatar answered Feb 14 '26 17:02

Dogbert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!