我正在努力让以下工作。不确定我失败的地方。我收到以下错误:
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:在`test_google'
:/
答案 0 :(得分:5)
Method是Ruby中的内置类(http://ruby-doc.org/core-2.0/Method.html)您必须将类重命名为其他类。
答案 1 :(得分:0)
根据您的编辑,请执行以下操作:
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
答案 2 :(得分:0)
这真的不是一个解决方案,但是如果你有时间,那么Metaprogramming Ruby这本书就会详细介绍类。