Ruby 脚本抛出错误

Ruby script throwing error

我想写一个Ruby脚本来检查在Firebug中的DOM中找到的图层是否与页面匹配(例如:www.google.com)我的脚本中声明的哈希值。下面是我写的 Ruby 脚本:

require 'watir'

browser = Watir::Browser.new(:chrome)

browser.goto('www.google.co.in')

#Function
def page_data_layer(browser)
    page_layer  = {
        'host'     => 'www.google.co.in',
        'hostname' => 'www.google.co.in',
        'pathname' => '/',
        'protocol' => 'https:'
    }

    if page_layer.each do |data|
        data.has_key?('host')
            message.push("#{data['host']} matches")
        end
    end

    return message.join("\n")
end

#Main script
layer = page_data_layer(browser)

layer = Hash.new{|key,value| key[value] =
                  Hash.new(&key.default_proc)}

layer = browser.execute_script("return _gjwl")

log < layer

以下是我在 Netbeans 中 运行 脚本时得到的错误。

C:/Users/ruby_script.rb:17:in `block in page_data_layer': undefined method `has_key?' for ["host", "www.google.co.in"]:Array (NoMethodError)
        from C:/Users/ruby_script.rb:16:in `each'
        from C:/Users/ruby_script.rb:16:in `page_data_layer'
        from C:/Users/ruby_script.rb:26:in `<main>'

我想要实现的是 - layer = browser.execute_script("return _gjwl") 将 return google 站点内容中可用的任何内容 _gjwl。 Hash是为了匹配google站点内容中的键值是否相同。所以比较两者,看是否匹配,return通过或失败通过消息

请帮忙。提前致谢。

page_layer 是一个 Hash 实例。迭代哈希将 key, value 对传递给块。您正试图在其上调用 has_key?

if page_layer.each do |data|
    data.has_key?('host')
        message.push("#{data['host']} matches")
    end
end

一个人可能会这样做:

message.push("#{data['host']} matches") if page_layer.has_key?('host')

或:

page_layer.each do |key, value|
  message.push("#{value} matches") if key == 'host'
end

有2个问题:

  1. 您正在尝试比较方法中的图层,但只能访问预期的图层(不是浏览器返回的图层)。
  2. 您正在遍历预期的层值,但只检查自身的键。

这是您需要的。您可以根据需要将其重构为方法。

require 'watir-webdriver'
browser = Watir::Browser.new :chrome
at_exit { browser.close }
browser.goto('www.google.co.in')

page_layer  = {
   'host'     => 'www.google.co.in',
   'hostname' => 'www.google.co.in',
   'pathname' => '/',
   'protocol' => 'https:'
}

layer = browser.execute_script("return _gjwl")
# => {"ancestorOrigins"=>[], "assign"=>{}, "hash"=>"", "host"=>"www.google.co.in", "hostname"=>"www.google.co.in", "href"=>"https://www.google.co.in/?gws_rd=ssl", "origin"=>"https://www.google.co.in", "pathname"=>"/", "port"=>"", "protocol"=>"https:", "reload"=>{}, "replace"=>{}, "search"=>"?gws_rd=ssl"}

page_layer.each do |key, value|
    # Output key being checked
    p key

    # Check if the value returned from the browser matches the expected value
    p value == layer[key]
end

结果为:

"host"
true
"hostname"
true
"pathname"
true
"protocol"
true

在测试结果方面,您可能应该查看一些测试框架。他们提供了强大的比较工具和报告,而工作却少得多。例如,如果使用 RSpec,比较这些值将很简单:

expect(layer).to include(page_layer)

如果你想要一个 returns 组合结果消息的方法:

def page_data_layer(layer)
  page_layer  = {
    'host'     => 'www.google.co.in',
    'hostname' => 'www.google.co.in',
    'pathname' => '/',
    'protocol' => 'https:'
  }

  page_layer.map do |key, value|
    result = value == layer[key] ? 'matches' : 'does not match'
    "#{key} #{result}"
  end.join("\n")
end

layer = browser.execute_script("return _gjwl")
puts page_data_layer(layer)
#=> "host matches"
#=> "hostname matches"
#=> "pathname matches"
#=> "protocol matches"