我编写了一个Ruby脚本来检查在Firebug中为www.jira.com页面找到的图层是否与我脚本中声明的哈希值匹配。下面是我写的Ruby脚本:
require 'watir'
browser = Watir::Browser.new(:chrome)
browser.goto('https://jira.com')
JIRA_DATA_LAYER = {
'jira' => {
'event' => ['gtm.js', 'gtm.load'],
'gtm.start' => '1468949036556',
}
}
def get_jira_data_layer(get_data_layer)
result = []
get_data_layer.each do |data_layer|
data_layer.each do |data_layer_key, data_layer_value|
result << {"#{data_layer_key}" => data_layer_value}
end
end
return result
end
def compare_jira_data_layer(layer, jira_name)
message = []
index = 0
JIRA_DATA_LAYER[jira_name].each do |jira_key, jira_value|
if layer.include?({jira_key => jira_value})
result = 'matches - PASS'
else
result = 'matches - FAIL'
end
index += 1
message.push("'#{jira_key} => #{jira_value}' #{result}")
end
return message.join("\n")
end
data_layer = browser.execute_script("return dataLayer")
get_data_layer = get_jira_data_layer(data_layer)
compare_data_layer = compare_jira_data_layer(get_data_layer, "jira")
puts compare_data_layer
我得到以下输出:
'event => ["gtm.js", "gtm.load"]' matches - FAIL
'gtm.start => 1468949036556' matches - FAIL
我想要实现以下目标:
'event => gtm.js' matches - FAIL
'gtm.start => 1468949036556' matches - FAIL
答案 0 :(得分:0)
您可以在JIRA_DATA_LAYER中更改event
键的值,但我想它必须是那样。
在检查此哈希的密钥时尝试展开if
句子,并使用is_a?
方法检查特定密钥的值是否为数组。如果是这样,请遍历此数组的each
成员。