saving . . . saved Matching key,values of Hash of Hash in script with key,values of Array of Hash got from the page is not working has been deleted. Matching key,values of Hash of Hash in script with key,values of Array of Hash got from the page is not working has been hidden .
Matching key,values of Hash of Hash in script with key,values of Array of Hash got from the page is not working
Title
Question
I want to write a Ruby script to check if the layer found in DOM in Firebug for a page (For example: www.jira.com) is matching with the hash values declared in my script. Below is the Ruby script I have written:

I want to write a Ruby script to check if the layer found in DOM in Firebug for a page (For example: www.jira.com) is matching with the hash values declared in my script. Below is the Ruby script I have written:

require 'watir'

# Initialize the browser
browser = Watir::Browser.new(:chrome)

# Go to the page
browser.goto('www.jira.com')

# Initialize hash for what needs to be matched. I initiliazed this way because in future, if some more addition comes to the hash, it will be easy to fix
JIRA_DATA_LAYER = {
'one' => {
'event' => 'gtm.js',
'gtm.start' => 1468393184212,
},
}

# Function to read the elements of datalayer of JIRA
def read_data_layer(browser)
data_layer = browser.execute_script("return dataLayer")

return data_layer
end

# Function to compare the values of JIRA got directly from the page
def compare_jira_data_layer(browser)
result = []

compare_data_layer = read_data_layer(browser)

compare_data_layer[0].each do |key,value|
result.push(key)
end

return result.join("\\n")
end

# Function to check if the hash created and values got from page matches
def jira_data_layer(browser, layer)
message = []

result_compare = compare_jira_data_layer(browser)

message.push('Checking Jira Data Layer')

JIRA_DATA_LAYER.each do |key, value|
value.each do |data_layer_key, data_layer_value|
if data_layer_value == result_compare
result = 'matches - PASS'
else
result = 'does not match - FAIL'
end

message.push("#{data_layer_key} #{result}")
end
end

return message.join("\\n")
end

# Read the elements of the dataLayer from the page
data_layer = read_data_layer(browser)

puts jira_data_layer(browser, data_layer)

Following is the error I got when I ran the script in Netbeans.
event does not match - FAIL
gtm.start does not match - FAIL

I want the following OUTPUT to be achieved:
'event => gtm.js' matches - PASS
'gtm.start => 1468393184212' matches - PASS

My intention is that most part of possible code should be covered in function so that it can be used for more main scripts

Following code is not working:
# Function to check if the hash created and values got from page matches
def jira_data_layer(browser, layer)
message = []

result_compare = compare_jira_data_layer(browser)

message.push('Checking Jira Data Layer')

JIRA_DATA_LAYER.each do |key, value|
value.each do |data_layer_key, data_layer_value|
if data_layer_value == result_compare
result = 'matches - PASS'
else
result = 'does not match - FAIL'
end

message.push("#{data_layer_key} #{result}")
end
end

return message.join("\\n")
end

# Read the elements of the dataLayer from the page
data_layer = read_data_layer(browser)

puts jira_data_layer(browser, data_layer)

Where am I going wrong? Please help. Thanks in advance





Ruby General None min None sec 14-07-16, 10:01 a.m. ashwint87

Answers:

Log-in to answer to this question.