3

我正在尝试使以下工作。不知道我在哪里失败。我收到以下错误:

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'

:/

4

3 回答 3

5

Method 是 Ruby 中的一个内置类 ( http://ruby-doc.org/core-2.0/Method.html ) 您必须将您的类重命名为其他名称。

于 2013-05-13T19:14:41.707 回答
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
于 2013-05-13T19:13:40.190 回答
0

这确实不是一个解决方案,但如果你有时间,Metaprogramming Ruby 一书会详细介绍类。

于 2013-05-14T00:31:43.473 回答