From b6adaa28d627b436fab9eb14bf7df2404ee24892 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 14 Dec 2012 13:16:04 +0100 Subject: [PATCH] Added new browser tests. --- .../javascripts/app/views/chat_widget.jst.eco | 2 +- script/websocket-server.rb | 2 +- test/browser/chat.rb | 82 +++++++++ test/browser_test_helper.rb | 172 ++++++++++++------ 4 files changed, 201 insertions(+), 57 deletions(-) create mode 100644 test/browser/chat.rb diff --git a/app/assets/javascripts/app/views/chat_widget.jst.eco b/app/assets/javascripts/app/views/chat_widget.jst.eco index 8ce6903fd..b4a1e5a51 100644 --- a/app/assets/javascripts/app/views/chat_widget.jst.eco +++ b/app/assets/javascripts/app/views/chat_widget.jst.eco @@ -1,5 +1,5 @@
-

<%- @T('Chat') %>×

+

<%- @T('Chat') %>×

class="hide"<% end %>>
<% for message in @messages: %> diff --git a/script/websocket-server.rb b/script/websocket-server.rb index 0c0ed6662..56528863e 100644 --- a/script/websocket-server.rb +++ b/script/websocket-server.rb @@ -142,7 +142,7 @@ EventMachine.run { # list all current clients client_list = Session.list client_list.each {|local_client_id, local_client| - if local_client_id != client_id + if local_client_id.to_s != client_id.to_s # broadcast to recipient list if data['recipient'] && data['recipient']['user_id'] diff --git a/test/browser/chat.rb b/test/browser/chat.rb new file mode 100644 index 000000000..540dd91d8 --- /dev/null +++ b/test/browser/chat.rb @@ -0,0 +1,82 @@ +# encoding: utf-8 +require 'browser_test_helper' + +class Chat < ActiveSupport::TestCase + test 'websocket' do + message = 'message 1äöüß ' + rand(99999999999999999).to_s + tests = [ + { + :name => 'start', + :instance1 => Watir::Browser.new, + :instance2 => Watir::Browser.new, + :instance1_username => 'm@edenhofer.de', + :instance1_password => 'test', + :instance2_username => 'm@edenhofer.de', + :instance2_password => 'test', + :url => 'http://localhost:3000', + :action => [ + { + :where => :instance1, + :execute => 'check', + :element => :form, + :id => 'login', + :result => false, + }, + { + :where => :instance2, + :execute => 'check', + :element => :form, + :id => 'login', + :result => false, + }, + + { + :where => :instance1, + :execute => 'click', + :element => :a, + :id => 'chat_toogle', + }, + { + :where => :instance1, + :execute => 'set', + :element => :text_field, + :name => 'chat_message', + :value => message, + }, + { + :where => :instance1, + :execute => 'send_key', + :element => :text_field, + :name => 'chat_message', + :value => :enter, + }, + { + :execute => 'wait', + :value => 3, + }, + { + :where => :instance1, + :execute => 'match', + :element => :div, + :id => 'chat_log_container', + :value => message, + :match_result => true, + }, + { + :where => :instance2, + :execute => 'match', + :element => :div, + :id => 'chat_log_container', + :value => message, + :match_result => true, + }, + { + :execute => 'wait', + :value => 10, + }, + ], + }, + ] + browser_double_test(tests) + end +end \ No newline at end of file diff --git a/test/browser_test_helper.rb b/test/browser_test_helper.rb index 34cacfbc6..adb5eeb69 100644 --- a/test/browser_test_helper.rb +++ b/test/browser_test_helper.rb @@ -6,12 +6,12 @@ require 'watir-webdriver' class ActiveSupport::TestCase # Add more helper methods to be used by all tests here... - def browser_signle_test_with_login(tests) + def browser_login(data) all_tests = [ { :name => 'login', - :instance => Watir::Browser.new, - :url => 'http://localhost:3000', + :instance => data[:instance] || Watir::Browser.new, + :url => data[:url] || 'http://localhost:3000', :action => [ { :execute => 'wait', @@ -27,13 +27,13 @@ class ActiveSupport::TestCase :execute => 'set', :element => :text_field, :name => 'username', - :value => 'nicole.braun@zammad.org', + :value => data[:username] || 'nicole.braun@zammad.org', }, { :execute => 'set', :element => :text_field, :name => 'password', - :value => 'test' + :value => data[:password] || 'test' }, { :execute => 'click', @@ -53,11 +53,51 @@ class ActiveSupport::TestCase ], }, ]; + return all_tests + end + + def browser_signle_test_with_login(tests) + all_tests = browser_login( {} ) all_tests = all_tests.concat( tests ) browser_single_test(all_tests) end - def browser_single_test(tests) + def browser_double_test(tests) + instance1 = browser_single_test( browser_login({ + :instance => tests[0][:instance1], + :username => tests[0][:instance1_username], + :password => tests[0][:instance1_password], + :url => tests[0][:url], + }), true ) + instance2 = browser_single_test( browser_login({ + :instance => tests[0][:instance2], + :username => tests[0][:instance2_username], + :password => tests[0][:instance2_password], + :url => tests[0][:url], + }), true ) + tests.each { |test| + if test[:action] + test[:action].each { |action| + if action[:execute] == 'wait' + sleep action[:value] + next + end + next if !action[:where] + if action[:where] == :instance1 + instance = instance1 + else + instance = instance2 + end + + browser_element_action(test, action, instance) + } + end + } + instance1.close + instance2.close + end + + def browser_single_test(tests, keep_connection = false) instance = nil tests.each { |test| if test[:instance] @@ -72,58 +112,80 @@ class ActiveSupport::TestCase sleep action[:value] next end - - if action[:id] - element = instance.send( action[:element], { :id => action[:id] } ) - if action[:result] == false - assert( !element.exists?, "(#{test[:name]}) Element #{action[:element]} with id #{action[:id]} exists" ) - else - assert( element.exists?, "(#{test[:name]}) Element #{action[:element]} with id #{action[:id]} doesn't exist" ) - end - elsif action[:type] - if action[:result] == false - element = instance.send( action[:element], { :type => action[:type] } ) - assert( !element.exists?, "(#{test[:name]}) Element #{action[:element]} with type #{action[:type]} exists" ) - else - element = instance.send( action[:element], { :type => action[:type] } ) - assert( element.exists?, "(#{test[:name]}) Element #{action[:element]} with type #{action[:type]} doesn't exist" ) - end - elsif action[:name] - if action[:result] == false - element = instance.send( action[:element], { :name => action[:name] } ) - assert( !element.exists?, "(#{test[:name]}) Element #{action[:element]} with name #{action[:name]} exists" ) - else - element = instance.send( action[:element], { :name => action[:name] } ) - assert( element.exists?, "(#{test[:name]}) Element #{action[:element]} with name #{action[:name]} doesn't exist" ) - end - elsif action[:href] - if action[:result] == false - element = instance.send( action[:element], { :href => action[:href] } ) - assert( !element.exists?, "(#{test[:name]}) Element #{action[:element]} with href #{action[:href]} exists" ) - else - element = instance.send( action[:element], { :href => action[:href] } ) - assert( element.exists?, "(#{test[:name]}) Element #{action[:element]} with href #{action[:href]} doesn't exist" ) - end - elsif action[:element] == :url - if instance.url =~ /#{Regexp.quote(action[:result])}/ - assert( true, "(#{test[:name]}) url #{instance.url} is matching #{action[:result]}" ) - else - assert( false, "(#{test[:name]}) url #{instance.url} is not matching #{action[:result]}" ) - end - else - assert( false, "(#{test[:name]}) unknow selector for '#{action[:element]}'" ) - end - if action[:execute] == 'set' - element.set( action[:value] ) - elsif action[:execute] == 'click' - element.click - elsif action[:execute] == 'check' - else - assert( false, "(#{test[:name]}) unknow action '#{action[:execute]}'" ) - end + browser_element_action(test, action, instance) } end } + if keep_connection + return instance + end instance.close end + + def browser_element_action(test, action, instance) + if action[:id] + element = instance.send( action[:element], { :id => action[:id] } ) + if action[:result] == false + assert( !element.exists?, "(#{test[:name]}) Element #{action[:element]} with id #{action[:id]} exists" ) + else + assert( element.exists?, "(#{test[:name]}) Element #{action[:element]} with id #{action[:id]} doesn't exist" ) + end + elsif action[:type] + if action[:result] == false + element = instance.send( action[:element], { :type => action[:type] } ) + assert( !element.exists?, "(#{test[:name]}) Element #{action[:element]} with type #{action[:type]} exists" ) + else + element = instance.send( action[:element], { :type => action[:type] } ) + assert( element.exists?, "(#{test[:name]}) Element #{action[:element]} with type #{action[:type]} doesn't exist" ) + end + elsif action[:name] + if action[:result] == false + element = instance.send( action[:element], { :name => action[:name] } ) + assert( !element.exists?, "(#{test[:name]}) Element #{action[:element]} with name #{action[:name]} exists" ) + else + element = instance.send( action[:element], { :name => action[:name] } ) + assert( element.exists?, "(#{test[:name]}) Element #{action[:element]} with name #{action[:name]} doesn't exist" ) + end + elsif action[:href] + if action[:result] == false + element = instance.send( action[:element], { :href => action[:href] } ) + assert( !element.exists?, "(#{test[:name]}) Element #{action[:element]} with href #{action[:href]} exists" ) + else + element = instance.send( action[:element], { :href => action[:href] } ) + assert( element.exists?, "(#{test[:name]}) Element #{action[:element]} with href #{action[:href]} doesn't exist" ) + end + elsif action[:element] == :url + if instance.url =~ /#{Regexp.quote(action[:result])}/ + assert( true, "(#{test[:name]}) url #{instance.url} is matching #{action[:result]}" ) + else + assert( false, "(#{test[:name]}) url #{instance.url} is not matching #{action[:result]}" ) + end + else + assert( false, "(#{test[:name]}) unknow selector for '#{action[:element]}'" ) + end + if action[:execute] == 'set' + element.set( action[:value] ) + elsif action[:execute] == 'click' + element.click + elsif action[:execute] == 'send_key' + element.send_keys action[:value] + elsif action[:execute] == 'match' + if element.text =~ /#{Regexp.quote(action[:value])}/ + if action[:match_result] + assert( true, "(#{test[:name]}) matching '#{action[:value]}' in content '#{element.text}'" ) + else + assert( false, "(#{test[:name]}) matching '#{action[:value]}' in content '#{element.text}' but should not!" ) + end + else + if !action[:match_result] + assert( true, "(#{test[:name]}) not matching '#{action[:value]}' in content '#{element.text}'" ) + else + assert( false, "(#{test[:name]}) not matching '#{action[:value]}' in content '#{element.text}' but should not!" ) + end + end + elsif action[:execute] == 'check' + else + assert( false, "(#{test[:name]}) unknow action '#{action[:execute]}'" ) + end + end end