我正在尝试使以下工作。不知道我在哪里失败。我收到以下错误:
1) 错误:
test_google(Google):
NoMethodError: undefined method `new' for Method:Class
google.rb:15:in `setup'
我是 ruby 新手,所以这很漂亮 101。有人可以向我解释我的错误以及为什么我可以理解。谢谢!
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
我更改了班级名称:
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
现在我不确定如何在我的“Suber”类中设置@driver。我以为它会起作用,但它会抛出:
NoMethodError: 未定义的方法find_element' for nil:NilClass
google.rb:37:in
搜索'google.rb:25:in `test_google'
:/