lib/WAAT.rb in WAAT-1.4.1 vs lib/WAAT.rb in WAAT-1.5.0

- old
+ new

@@ -37,37 +37,46 @@ # # === Synopsis # This method initializes 'WAAT-Ruby' # # === Args - # *keep_loaded_file_in_memory*: Default: true - # This means the loaded test data file will be kept in memory till the tests are running. + # 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 - # * initialize_waat(true) => Same as initialize_waat - # * initialize_waat(false) => This will unload the test data file after the Web Analytic tags verification is done + # * 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 only the http_sniffer mechanism for doing Web Analytics testing. + # 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(keep_loaded_file_in_memory = true) + def initialize_waat(init_params={}) logger = Logger.new(STDOUT) logger.level = Logger::INFO logger.info("Initializing WAAT") load_java_classes - engine_instance(keep_loaded_file_in_memory) + 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. @@ -78,10 +87,11 @@ 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. @@ -108,10 +118,11 @@ # *: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: @@ -124,27 +135,48 @@ # 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") + 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]}") - java_result = @engine_instance.verifyWebAnalyticsData(params[:test_data_file_name], params[:action_name], params[:url_patterns], params[:minimum_number_of_packets]) + + 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(keep_loaded_file_in_memory = true) - log4j_properties_absolute_file_path = File.join(File.dirname(__FILE__), "WAAT", "resources", "log4j.properties") - @engine_instance ||= controller.getInstance(web_analytic_tool("http_sniffer"), input_file_type("xml"), keep_loaded_file_in_memory, log4j_properties_absolute_file_path) + 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 @@ -168,21 +200,28 @@ require 'rjb' Rjb::load(classpath = waat_file_list,[]) end - def proxy_from_java_enum(java_enum, web_analytic_tool) + def proxy_from_java_enum(java_enum, java_enum_value) java_enum.values.each do |each_value| - return each_value if each_value.name==web_analytic_tool.upcase + return each_value if each_value.name==java_enum_value.upcase end end - def web_analytic_tool(web_analytic_tool) - proxy_from_java_enum(web_analytic_tool_reference, web_analytic_tool) + 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) \ No newline at end of file