From e35ac9b468cdf302fd86f56a901703f5ff89f855 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 23 Feb 2013 23:54:48 +0100 Subject: [PATCH] Moved back to selenium-webdriver for browser testing. --- Gemfile | 3 +- ...started.rb => aaa_getting_started_test.rb} | 10 +-- ...auth_customer.rb => auth_customer_test.rb} | 8 +- .../{auth_master.rb => auth_master_test.rb} | 8 +- test/browser/{chat.rb => chat_test.rb} | 39 +++++++--- ...eate.rb => customer_ticket_create_test.rb} | 4 +- .../{prefereces.rb => prefereces_test.rb} | 4 +- test/browser/{setting.rb => setting_test.rb} | 4 +- test/browser/{signup.rb => signup_test.rb} | 14 ++-- test/browser_test_helper.rb | 78 +++++++++++++++---- test/unit/tag_test.rb | 2 +- 11 files changed, 116 insertions(+), 58 deletions(-) rename test/browser/{aaa_getting_started.rb => aaa_getting_started_test.rb} (90%) rename test/browser/{auth_customer.rb => auth_customer_test.rb} (92%) rename test/browser/{auth_master.rb => auth_master_test.rb} (92%) rename test/browser/{chat.rb => chat_test.rb} (73%) rename test/browser/{customer_ticket_create.rb => customer_ticket_create_test.rb} (96%) rename test/browser/{prefereces.rb => prefereces_test.rb} (96%) rename test/browser/{setting.rb => setting_test.rb} (98%) rename test/browser/{signup.rb => signup_test.rb} (88%) diff --git a/Gemfile b/Gemfile index 21dbdbbee..e380ae213 100644 --- a/Gemfile +++ b/Gemfile @@ -72,7 +72,6 @@ group :development, :test do gem 'simplecov-rcov' # UI tests w/ Selenium - #gem 'selenium-webdriver' - gem 'watir-webdriver' + gem 'selenium-webdriver' end diff --git a/test/browser/aaa_getting_started.rb b/test/browser/aaa_getting_started_test.rb similarity index 90% rename from test/browser/aaa_getting_started.rb rename to test/browser/aaa_getting_started_test.rb index 2f3d5cf3e..7cb9e3691 100644 --- a/test/browser/aaa_getting_started.rb +++ b/test/browser/aaa_getting_started_test.rb @@ -1,17 +1,17 @@ # encoding: utf-8 require 'browser_test_helper' -class AaaGettingStarted < ActiveSupport::TestCase - test 'getting started' do +class AaaGettingStartedTest < TestCase + def test_getting_started tests = [ { :name => 'start', - :instance => Watir::Browser.new, - :url => 'http://localhost:3000/#getting_started', + :instance => browser_instance, + :url => browser_url + '/#getting_started', :action => [ { :execute => 'wait', - :value => 6, + :value => 1, }, { :execute => 'check', diff --git a/test/browser/auth_customer.rb b/test/browser/auth_customer_test.rb similarity index 92% rename from test/browser/auth_customer.rb rename to test/browser/auth_customer_test.rb index aae172a50..535a316fb 100644 --- a/test/browser/auth_customer.rb +++ b/test/browser/auth_customer_test.rb @@ -1,13 +1,13 @@ # encoding: utf-8 require 'browser_test_helper' -class Auth < ActiveSupport::TestCase - test 'authentication' do +class AuthTest < TestCase + def test_authentication tests = [ { :name => 'start', - :instance => Watir::Browser.new, - :url => 'http://localhost:3000', + :instance => browser_instance, + :url => browser_url, :action => [ { :execute => 'check', diff --git a/test/browser/auth_master.rb b/test/browser/auth_master_test.rb similarity index 92% rename from test/browser/auth_master.rb rename to test/browser/auth_master_test.rb index ade58d33e..8dd13c266 100644 --- a/test/browser/auth_master.rb +++ b/test/browser/auth_master_test.rb @@ -1,13 +1,13 @@ # encoding: utf-8 require 'browser_test_helper' -class AuthMaster < ActiveSupport::TestCase - test 'authentication' do +class AuthMasterTest < TestCase + def test_authentication tests = [ { :name => 'start', - :instance => Watir::Browser.new, - :url => 'http://localhost:3000', + :instance => browser_instance, + :url => browser_url, :action => [ { :execute => 'check', diff --git a/test/browser/chat.rb b/test/browser/chat_test.rb similarity index 73% rename from test/browser/chat.rb rename to test/browser/chat_test.rb index 09e7dab85..6bdd03046 100644 --- a/test/browser/chat.rb +++ b/test/browser/chat_test.rb @@ -1,19 +1,19 @@ # encoding: utf-8 require 'browser_test_helper' -class Chat < ActiveSupport::TestCase - test 'websocket' do +class ChatTest < TestCase + def test_websocket message = 'message 1äöüß ' + rand(99999999999999999).to_s tests = [ { :name => 'start', - :instance1 => Watir::Browser.new, - :instance2 => Watir::Browser.new, + :instance1 => browser_instance, + :instance2 => browser_instance, :instance1_username => 'master@example.com', :instance1_password => 'test', :instance2_username => 'master@example.com', :instance2_password => 'test', - :url => 'http://localhost:3000', + :url => browser_url, :action => [ { :where => :instance1, @@ -27,7 +27,24 @@ class Chat < ActiveSupport::TestCase :css => '#login', :result => false, }, - + { + :where => :instance1, + :execute => 'click', + :css => '#chat_toogle', + }, + { + :execute => 'wait', + :value => 4, + }, + { + :where => :instance1, + :execute => 'click', + :css => '#chat_toogle', + }, + { + :execute => 'wait', + :value => 1, + }, { :where => :instance1, :execute => 'click', @@ -47,7 +64,7 @@ class Chat < ActiveSupport::TestCase }, { :execute => 'wait', - :value => 3, + :value => 6, }, { :where => :instance1, @@ -63,10 +80,10 @@ class Chat < ActiveSupport::TestCase :value => message, :match_result => true, }, - { - :execute => 'wait', - :value => 10, - }, +# { +# :execute => 'wait', +# :value => 1, +# }, ], }, ] diff --git a/test/browser/customer_ticket_create.rb b/test/browser/customer_ticket_create_test.rb similarity index 96% rename from test/browser/customer_ticket_create.rb rename to test/browser/customer_ticket_create_test.rb index 2329fa60b..0dddc58a5 100644 --- a/test/browser/customer_ticket_create.rb +++ b/test/browser/customer_ticket_create_test.rb @@ -1,8 +1,8 @@ # encoding: utf-8 require 'browser_test_helper' -class CustomerTicketCreate < ActiveSupport::TestCase - test 'customer ticket create' do +class CustomerTicketCreateTest < TestCase + def test_customer_ticket_create tests = [ { :name => 'customer ticket create', diff --git a/test/browser/prefereces.rb b/test/browser/prefereces_test.rb similarity index 96% rename from test/browser/prefereces.rb rename to test/browser/prefereces_test.rb index ce474ffcf..d14109b55 100644 --- a/test/browser/prefereces.rb +++ b/test/browser/prefereces_test.rb @@ -1,8 +1,8 @@ # encoding: utf-8 require 'browser_test_helper' -class Preferences < ActiveSupport::TestCase - test 'preferences' do +class PreferencesTest < TestCase + def test_preferences tests = [ { :name => 'preferences', diff --git a/test/browser/setting.rb b/test/browser/setting_test.rb similarity index 98% rename from test/browser/setting.rb rename to test/browser/setting_test.rb index 0dc8ee064..d883c5844 100644 --- a/test/browser/setting.rb +++ b/test/browser/setting_test.rb @@ -1,8 +1,8 @@ # encoding: utf-8 require 'browser_test_helper' -class TestSetting < ActiveSupport::TestCase - test 'setting' do +class SettingTest < TestCase + def test_setting tests = [ { :name => 'setting', diff --git a/test/browser/signup.rb b/test/browser/signup_test.rb similarity index 88% rename from test/browser/signup.rb rename to test/browser/signup_test.rb index 31c5b2647..301449867 100644 --- a/test/browser/signup.rb +++ b/test/browser/signup_test.rb @@ -1,23 +1,19 @@ # encoding: utf-8 require 'browser_test_helper' -class Signup < ActiveSupport::TestCase - test 'signup' do +class SignupTest < TestCase + def test_signup signup_user_email = 'signup-test-' + rand(999999).to_s + '@example.com' tests = [ { :name => 'start', - :instance => Watir::Browser.new, - :url => 'http://localhost:3000', + :instance => browser_instance, + :url => browser_url, :action => [ { :execute => 'click', :css => 'a[href="#signup"]', }, - { - :execute => 'wait', - :value => 1, - }, { :execute => 'check', :css => '#form-signup', @@ -59,7 +55,7 @@ class Signup < ActiveSupport::TestCase }, { :execute => 'wait', - :value => 3, + :value => 2, }, # check action diff --git a/test/browser_test_helper.rb b/test/browser_test_helper.rb index 3f65148df..14f381c55 100644 --- a/test/browser_test_helper.rb +++ b/test/browser_test_helper.rb @@ -1,17 +1,50 @@ ENV["RAILS_ENV"] = "test" require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' -require 'watir-webdriver' +#require 'watir-webdriver' +require 'selenium-webdriver' -class ActiveSupport::TestCase +class TestCase < Test::Unit::TestCase + def browser_url + ENV['BROWSER_URL'] || 'http://localhost:3000' + end + + def browser_instance + if !@browsers + @browsers = [] + end + if !ENV['REMOTE_URL'] + browser = Selenium::WebDriver.for :firefox + @browsers.push browser + return browser + end +# return Watir::Browser.new if !ENV['REMOTE_URL'] + + caps = Selenium::WebDriver::Remote::Capabilities.send( ENV['BROWSER'] ) + caps.platform = ENV['BROWSER_OS'] || 'Windows 2008' + caps.version = ENV['BROWSER_VERSION'] || '8' + Selenium::WebDriver.for( + :remote, + :url => ENV['REMOTE_URL'], + :desired_capabilities => caps, + ) + + end + + def teardown + return if !@browsers + @browsers.each{ |browser| + browser.close + } + end # Add more helper methods to be used by all tests here... def browser_login(data) all_tests = [ { :name => 'login', - :instance => data[:instance] || Watir::Browser.new, - :url => data[:url] || 'http://localhost:3000', + :instance => data[:instance] || browser_instance, + :url => data[:url] || browser_url, :action => [ { :execute => 'wait', @@ -99,7 +132,7 @@ class ActiveSupport::TestCase instance = test[:instance] end if test[:url] - instance.goto( test[:url] ) + instance.get( test[:url] ) end if test[:action] test[:action].each { |action| @@ -119,32 +152,45 @@ class ActiveSupport::TestCase def browser_element_action(test, action, instance) if action[:css] - element = instance.element( { :css => action[:css] } ) + begin + element = instance.find_element( { :css => action[:css] } ) + rescue + element = nil + end if action[:result] == false - assert( !element.exists?, "(#{test[:name]}) Element with css '#{action[:css]}' exists" ) + assert( !element, "(#{test[:name]}) Element with css '#{action[:css]}' exists" ) else - assert( element.exists?, "(#{test[:name]}) Element with css '#{action[:css]}' doesn't exist" ) + assert( element, "(#{test[:name]}) Element with css '#{action[:css]}' 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]}" ) + if instance.current_url =~ /#{Regexp.quote(action[:result])}/ + assert( true, "(#{test[:name]}) url #{instance.current_url} is matching #{action[:result]}" ) else - assert( false, "(#{test[:name]}) url #{instance.url} is not matching #{action[:result]}" ) + assert( false, "(#{test[:name]}) url #{instance.current_url} is not matching #{action[:result]}" ) end else assert( false, "(#{test[:name]}) unknow selector for '#{action[:element]}'" ) end if action[:execute] == 'set' - element.to_subtype.set( action[:value] ) + element.send_keys( action[:value] ) elsif action[:execute] == 'select' - element.to_subtype.select( action[:value] ) + dropdown = Selenium::WebDriver::Support::Select.new(element) + dropdown.select_by(:text, action[:value]) elsif action[:execute] == 'click' element.click elsif action[:execute] == 'send_key' element.send_keys action[:value] elsif action[:execute] == 'match' if action[:css] =~ /select/ - success = element.to_subtype.selected?(action[:value]) + dropdown = Selenium::WebDriver::Support::Select.new(element) + success = false + if dropdown.selected_options + dropdown.selected_options.each {|option| + if option.text == action[:value] + success = true + end + } + end if action[:match_result] if success assert( true, "(#{test[:name]}) matching '#{action[:value]}' in select list" ) @@ -159,8 +205,8 @@ class ActiveSupport::TestCase end end else - if action[:css] =~ /input|textarea/i - text = element.to_subtype.value + if action[:css] =~ /input/i + text = element.attribute('value') else text = element.text end diff --git a/test/unit/tag_test.rb b/test/unit/tag_test.rb index e9dfa346e..5a492c9fb 100644 --- a/test/unit/tag_test.rb +++ b/test/unit/tag_test.rb @@ -96,4 +96,4 @@ class TagTest < ActiveSupport::TestCase assert( !list.include?( test[:tag_add][:item] ), "Tag entry destroyed") } end -end \ No newline at end of file +end