Reputation: 490
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:in
search'
google.rb:25:in `test_google'
: /
Upvotes: 3
Views: 1648
Reputation: 247
This really isn't a solution but if you have the time, the book Metaprogramming Ruby goes over classes in good detail.
Upvotes: 0
Reputation: 118299
As per your edit do the below:
require "test/unit"
require "selenium-webdriver"
require "json"
require "time"
module 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
class Google < Test::Unit::TestCase
include Suber
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
end
end
Upvotes: 0
Reputation: 222448
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.
Upvotes: 5