-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWAAT.rb
227 lines (201 loc) · 8.66 KB
/
WAAT.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#**
# * Created by: Anand Bagmar
# * Email: [email protected]
# * Date: July 20, 2011
# * Time: 9:34:00 AM
# *
# * Copyright 2010 Anand Bagmar ([email protected]). Distributed under the Apache 2.0 License
# *
# * http://essenceoftesting.blogspot.com/search/label/waat
# * http://github.com/anandbagmar/WAAT-Ruby
# * http://github.com/anandbagmar/WAAT
# *
#**
module WAAT
#
# Result class converts the Result object returned by WAAT-Java to a Ruby class
#
# This class has 2 methods
# * status => PASS | FAIL | SKIP
# * list_of_errors => An array of strings containing all the Web Analytic validation errors as returned by the verify_web_analytics_data method
#
class Result
attr_reader :status, :list_of_errors
def initialize(java_result)
@status = java_result.getVerificationStatus.name
@list_of_errors = []
java_result.getListOfErrors.toArray.each do |each_error|
@list_of_errors << each_error.toString
end
end
end
#
# === Synopsis
# This method initializes 'WAAT-Ruby'
#
# === Args
# This takes a hash as an input with the following possible name/value pairs
# {
# *:keep_loaded_file_in_memory* => Default: true,
# *:waat_plugin* => Default: "http_sniffer",
# *:input_file_type* => Default: "xml", (Currently the only supported file type)
# *:log4j_properties_absolute_file_path => Default: File.join(File.dirname(__FILE__), "WAAT", "resources", "log4j.properties")}
# keep_loaded_file_in_memory: This means the loaded test data file will be kept in memory till the tests are running.
# waat_plugin: http_sniffer or js_sniffer
#
# === Examples:
#
# * initialize_waat => This will use the default value for keep_loaded_file_in_memory, and will use the http_sniffer
# * initialize_waat(:keep_loaded_file_in_memory => true) => Uses http_sniffer by default
# * initialize_waat(:keep_loaded_file_in_memory => false, :waat_plugin => "js_sniffer") => This will unload the test data file after the Web Analytic tags verification is done AND uses js_sniffer as the web analytic plugin
#
# === Corresponding WAAT-Java API
# getInstance(WebAnalyticTool, InputFileType, keepLoadedFileInMemory, log4jPropertiesFilePath)::
# This method initializes WAAT-Java.
# Unlike WAAT-Java, WAAT-Ruby supports the http_sniffer & js_sniffer mechanism for doing Web Analytics testing.
# Also, WAAT-Ruby supports specification of the input test data in XML format only.
#
#
def initialize_waat(init_params={})
logger = Logger.new(STDOUT)
logger.level = Logger::INFO
logger.info("Initializing WAAT")
load_java_classes
engine_instance(init_params)
end
#
# === Synopsis
# This method enables Web Analytic testing for all subsequent tests till it is explicitly disabled
# This is applicable ONLY WHEN USING HTTP_SNIFFER
#
# === Corresponding WAAT-Java API
# enableWebAnalyticsTesting::
# This method enables Web Analytic testing in WAAT-Java
# When this method is called, the packet capturing is started on all the network interfaces on the machine where the tests are running.
#
def enable_web_analytics_testing
logger.info("Enable Web Analytics Testing")
engine_instance.enableWebAnalyticsTesting
end
#
# === Synopsis
# This method disables Web Analytic testing for all subsequent tests till it is explicitly enabled again
# This is applicable ONLY WHEN USING HTTP_SNIFFER
#
# === Corresponding WAAT-Java API
# disableWebAnalyticsTesting::
# This method disables Web Analytic testing in WAAT-Java
# When this method is called, the packet capturing is stopped for all the network interfaces on the machine where the tests are running.
#
def disable_web_analytics_testing
logger.info("Disable Web Analytics Testing")
engine_instance.disableWebAnalyticsTesting
end
#
# === Synopsis
# This method verifies the Web Analytic tags using the http_sniffer mechanism
#
# === Args
# params Hash with the following entries
#
# *:test_data_file_name*:
# Absolute path to the input Test Data (xml) file name.
#
# *:action_name*:
# A (String) name representing the action done in the UI, for which we want to do Web Analytics Testing.
# This action_name matches the corresponding name in the test_data_file_name
#
# *:url_patterns*:
# An array of Strings containing URL snippets that will be used to filter the packets captured by HttpSniffer
#
# *:minimum_number_of_packets*:
# The minimum number of "filtered" packets to capture based on the url_patterns provided
# *This is applicable ONLY WHEN USING HTTP_SNIFFER*
#
# === Examples:
#
# * verify_web_analytics_data({:test_data_file_name, :action_name, :url_patterns, :minimum_number_of_packets}) => This will enable Web Analytic Testing
# where:
# :test_data_file_name => File.join(File.dirname(__FILE__), "..", "..", "sampleData", "TestData.xml")
# :action_name => "OpenWAATArticleOnBlog_HttpSniffer"
# :url_patterns => ["GET /ps/ifr?container=friendconnect&mid=0"]
# :minimum_number_of_packets => 1
#
# === Corresponding WAAT-Java API
# verifyWebAnalyticsData(test_data_file_name, action_name, url_patterns, minimum_number_of_packets)::
# This method enables Web Analytic testing in WAAT-Java
# When this method is called, the packet capturing is started on all the network interfaces on the machine where the tests are running.
#
def verify_web_analytics_data(params)
logger.info("Verify Web Analytics Data with params: #{params.inspect}")
logger.info("\tTest Data File Name: #{params[:test_data_file_name]}")
logger.info("\tAction Name: #{params[:action_name]}")
params = symbolize_keys(params)
if(@waat_plugin_type.nil?)
logger.error("WAAT plugin not initialized. Have you invoked the initialize_waat method?")
elsif(@waat_plugin_type.upcase=="HTTP_SNIFFER")
logger.info("Verifying web analytics data for: #{params.inspect}")
java_result = @engine_instance.verifyWebAnalyticsData(params[:test_data_file_name], params[:action_name], params[:url_patterns], params[:minimum_number_of_packets])
else
logger.info("Verifying web analytics data for: #{params.inspect}")
java_result = @engine_instance.verifyWebAnalyticsData(params[:test_data_file_name], params[:action_name], params[:url_patterns])
end
Result.new(java_result)
end
private
def logger
@logger ||= Logger.new(STDOUT)
@logger.level = Logger::INFO
@logger
end
def engine_instance(init_params={})
waat_defaults = {:keep_loaded_file_in_memory => true,
:waat_plugin => "http_sniffer",
:input_file_type => "xml",
:log4j_properties_absolute_file_path => File.join(File.dirname(__FILE__), "WAAT", "resources", "log4j.properties")}
init_params = waat_defaults.merge(symbolize_keys(init_params))
logger.info("Initializing WAAT with following params: #{init_params.inspect}")
@engine_instance ||= controller.getInstance(
web_analytic_tool(init_params[:waat_plugin]),
input_file_type(init_params[:input_file_type]),
init_params[:keep_loaded_file_in_memory],
init_params[:log4j_properties_absolute_file_path])
end
def controller
@controller ||= Rjb::import('com.thoughtworks.webanalyticsautomation.Controller')
end
def input_file_type_reference
@input_file_type_reference ||= Rjb::import('com.thoughtworks.webanalyticsautomation.inputdata.InputFileType')
end
def web_analytic_tool_reference
@web_analytic_tool_reference ||= Rjb::import('com.thoughtworks.webanalyticsautomation.plugins.WebAnalyticTool')
end
def load_java_classes
separator = (Config::CONFIG['target_os'] =~ /[win|mingw]/) == 0 ? ';' : ':'
waat_lib_directory = File.join(File.dirname(__FILE__), "WAAT", "lib")
logger.info("WAAT LIB directory: #{waat_lib_directory}")
waat_file_list = Dir.glob("#{waat_lib_directory}/*.jar").join(separator)
logger.info("Loading following JARs: #{waat_file_list}")
require 'rjb'
Rjb::load(classpath = waat_file_list,[])
end
def proxy_from_java_enum(java_enum, java_enum_value)
java_enum.values.each do |each_value|
return each_value if each_value.name==java_enum_value.upcase
end
end
def web_analytic_tool(waat_plugin)
@waat_plugin_type = waat_plugin
proxy_from_java_enum(web_analytic_tool_reference, waat_plugin)
end
def input_file_type(input_file_type)
proxy_from_java_enum(input_file_type_reference, input_file_type)
end
def symbolize_keys(hash)
hash.keys.each do |key|
hash[(key.to_sym rescue key) || key ] = hash.delete(key)
end
hash
end
end
World(WAAT)