2012-12-14 10:14:48 +00:00
|
|
|
ENV["RAILS_ENV"] = "test"
|
|
|
|
require File.expand_path('../../config/environment', __FILE__)
|
|
|
|
require 'rails/test_help'
|
2013-02-23 22:54:48 +00:00
|
|
|
require 'selenium-webdriver'
|
2012-12-14 10:14:48 +00:00
|
|
|
|
2013-02-23 22:54:48 +00:00
|
|
|
class TestCase < Test::Unit::TestCase
|
2013-06-25 14:00:28 +00:00
|
|
|
def browser
|
|
|
|
ENV['BROWSER'] || 'firefox'
|
|
|
|
end
|
|
|
|
|
|
|
|
def browser_support_cookies
|
|
|
|
if browser =~ /(internet_explorer|ie)/i
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2013-02-23 22:54:48 +00:00
|
|
|
def browser_url
|
|
|
|
ENV['BROWSER_URL'] || 'http://localhost:3000'
|
|
|
|
end
|
|
|
|
|
|
|
|
def browser_instance
|
|
|
|
if !@browsers
|
|
|
|
@browsers = []
|
|
|
|
end
|
|
|
|
if !ENV['REMOTE_URL']
|
2013-06-25 14:00:28 +00:00
|
|
|
local_browser = Selenium::WebDriver.for( browser.to_sym )
|
2013-06-28 22:23:57 +00:00
|
|
|
local_browser.manage.window.resize_to(1024, 1024)
|
2013-06-25 14:00:28 +00:00
|
|
|
@browsers.push local_browser
|
|
|
|
return local_browser
|
2013-02-23 22:54:48 +00:00
|
|
|
end
|
|
|
|
|
2013-06-25 14:00:28 +00:00
|
|
|
caps = Selenium::WebDriver::Remote::Capabilities.send( browser )
|
2013-02-23 22:54:48 +00:00
|
|
|
caps.platform = ENV['BROWSER_OS'] || 'Windows 2008'
|
|
|
|
caps.version = ENV['BROWSER_VERSION'] || '8'
|
2013-06-25 14:00:28 +00:00
|
|
|
local_browser = Selenium::WebDriver.for(
|
2013-02-23 22:54:48 +00:00
|
|
|
:remote,
|
|
|
|
:url => ENV['REMOTE_URL'],
|
|
|
|
:desired_capabilities => caps,
|
|
|
|
)
|
2013-06-25 14:00:28 +00:00
|
|
|
@browsers.push local_browser
|
|
|
|
return local_browser
|
2013-02-23 22:54:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
return if !@browsers
|
2013-02-23 23:33:30 +00:00
|
|
|
|
|
|
|
# only shut down browser type once
|
|
|
|
# otherwise this error will happen "Errno::ECONNREFUSED: Connection refused - connect(2)"
|
|
|
|
shutdown = {}
|
2013-06-25 14:00:28 +00:00
|
|
|
@browsers.each{ |local_browser|
|
|
|
|
next if shutdown[ local_browser.browser ]
|
|
|
|
shutdown[ local_browser.browser ] = true
|
|
|
|
local_browser.quit
|
2013-02-23 22:54:48 +00:00
|
|
|
}
|
|
|
|
end
|
2012-12-14 10:14:48 +00:00
|
|
|
|
|
|
|
# Add more helper methods to be used by all tests here...
|
2012-12-14 12:16:04 +00:00
|
|
|
def browser_login(data)
|
2013-02-12 00:56:23 +00:00
|
|
|
all_tests = [
|
2012-12-14 10:14:48 +00:00
|
|
|
{
|
|
|
|
:name => 'login',
|
2013-02-23 22:54:48 +00:00
|
|
|
:instance => data[:instance] || browser_instance,
|
|
|
|
:url => data[:url] || browser_url,
|
2012-12-14 10:14:48 +00:00
|
|
|
:action => [
|
|
|
|
{
|
|
|
|
:execute => 'wait',
|
|
|
|
:value => 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
:execute => 'check',
|
2013-02-22 10:36:00 +00:00
|
|
|
:css => '#login',
|
2012-12-14 10:14:48 +00:00
|
|
|
:result => true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
:execute => 'set',
|
2013-02-22 10:36:00 +00:00
|
|
|
:css => 'input[name="username"]',
|
2012-12-14 12:16:04 +00:00
|
|
|
:value => data[:username] || 'nicole.braun@zammad.org',
|
2012-12-14 10:14:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
:execute => 'set',
|
2013-02-22 10:36:00 +00:00
|
|
|
:css => 'input[name="password"]',
|
2012-12-14 12:16:04 +00:00
|
|
|
:value => data[:password] || 'test'
|
2012-12-14 10:14:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
:execute => 'click',
|
2013-02-22 10:36:00 +00:00
|
|
|
:css => '#login button',
|
2012-12-14 10:14:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
:execute => 'wait',
|
2013-06-10 11:00:22 +00:00
|
|
|
:value => 2,
|
2012-12-14 10:14:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
:execute => 'check',
|
2013-02-22 10:36:00 +00:00
|
|
|
:css => '#login',
|
2012-12-14 10:14:48 +00:00
|
|
|
:result => false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
2012-12-14 12:16:04 +00:00
|
|
|
return all_tests
|
|
|
|
end
|
|
|
|
|
2013-02-22 10:36:00 +00:00
|
|
|
def browser_signle_test_with_login(tests, login = {})
|
|
|
|
all_tests = browser_login( login )
|
2012-12-14 10:14:48 +00:00
|
|
|
all_tests = all_tests.concat( tests )
|
|
|
|
browser_single_test(all_tests)
|
|
|
|
end
|
|
|
|
|
2012-12-14 12:16:04 +00:00
|
|
|
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)
|
2012-12-14 10:14:48 +00:00
|
|
|
instance = nil
|
2013-04-23 18:36:09 +00:00
|
|
|
@stack = nil
|
2012-12-14 10:14:48 +00:00
|
|
|
tests.each { |test|
|
|
|
|
if test[:instance]
|
|
|
|
instance = test[:instance]
|
|
|
|
end
|
|
|
|
if test[:url]
|
2013-02-23 22:54:48 +00:00
|
|
|
instance.get( test[:url] )
|
2012-12-14 10:14:48 +00:00
|
|
|
end
|
|
|
|
if test[:action]
|
|
|
|
test[:action].each { |action|
|
|
|
|
if action[:execute] == 'wait'
|
|
|
|
sleep action[:value]
|
|
|
|
next
|
|
|
|
end
|
2012-12-14 12:16:04 +00:00
|
|
|
browser_element_action(test, action, instance)
|
2012-12-14 10:14:48 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
}
|
2012-12-14 12:16:04 +00:00
|
|
|
if keep_connection
|
|
|
|
return instance
|
|
|
|
end
|
2012-12-14 10:14:48 +00:00
|
|
|
instance.close
|
|
|
|
end
|
2013-06-08 22:35:18 +00:00
|
|
|
|
2012-12-14 12:16:04 +00:00
|
|
|
def browser_element_action(test, action, instance)
|
2013-06-10 11:59:16 +00:00
|
|
|
#puts "NOTICE: " + action.inspect
|
2013-02-22 10:36:00 +00:00
|
|
|
if action[:css]
|
2013-06-18 20:58:29 +00:00
|
|
|
if action[:css].match '###stack###'
|
|
|
|
action[:css].gsub! '###stack###', @stack
|
|
|
|
end
|
2013-02-23 22:54:48 +00:00
|
|
|
begin
|
2013-06-08 22:35:18 +00:00
|
|
|
if action[:range] == 'all'
|
|
|
|
element = instance.find_elements( { :css => action[:css] } )
|
|
|
|
else
|
|
|
|
element = instance.find_element( { :css => action[:css] } )
|
|
|
|
end
|
2013-02-23 22:54:48 +00:00
|
|
|
rescue
|
|
|
|
element = nil
|
|
|
|
end
|
2012-12-14 12:16:04 +00:00
|
|
|
if action[:result] == false
|
2013-02-23 22:54:48 +00:00
|
|
|
assert( !element, "(#{test[:name]}) Element with css '#{action[:css]}' exists" )
|
2012-12-14 12:16:04 +00:00
|
|
|
else
|
2013-02-23 22:54:48 +00:00
|
|
|
assert( element, "(#{test[:name]}) Element with css '#{action[:css]}' doesn't exist" )
|
2012-12-14 12:16:04 +00:00
|
|
|
end
|
|
|
|
elsif action[:element] == :url
|
2013-02-23 22:54:48 +00:00
|
|
|
if instance.current_url =~ /#{Regexp.quote(action[:result])}/
|
|
|
|
assert( true, "(#{test[:name]}) url #{instance.current_url} is matching #{action[:result]}" )
|
2012-12-14 12:16:04 +00:00
|
|
|
else
|
2013-02-23 22:54:48 +00:00
|
|
|
assert( false, "(#{test[:name]}) url #{instance.current_url} is not matching #{action[:result]}" )
|
2012-12-14 12:16:04 +00:00
|
|
|
end
|
2013-06-18 21:36:07 +00:00
|
|
|
elsif action[:element] == :title
|
|
|
|
title = instance.title
|
|
|
|
if title =~ /#{action[:value]}/i
|
2013-06-24 08:09:59 +00:00
|
|
|
assert( true, "(#{test[:name]}) matching '#{action[:value]}' in title '#{title}'" )
|
2013-06-18 21:36:07 +00:00
|
|
|
else
|
2013-06-24 08:09:59 +00:00
|
|
|
assert( false, "(#{test[:name]}) not matching '#{action[:value]}' in title '#{title}'" )
|
2013-06-18 21:36:07 +00:00
|
|
|
end
|
|
|
|
return
|
2013-06-23 21:32:45 +00:00
|
|
|
elsif action[:element] == :cookie
|
2013-06-25 14:00:28 +00:00
|
|
|
if !browser_support_cookies
|
|
|
|
assert( true, "(#{test[:name]}) '#{action[:value]}' ups browser is not supporting reading cookies")
|
2013-07-14 11:43:37 +00:00
|
|
|
return
|
2013-06-25 14:00:28 +00:00
|
|
|
end
|
2013-06-23 21:32:45 +00:00
|
|
|
cookies = instance.manage.all_cookies
|
|
|
|
cookies.each {|cookie|
|
|
|
|
if cookie.to_s =~ /#{action[:value]}/i
|
2013-06-24 08:09:59 +00:00
|
|
|
assert( true, "(#{test[:name]}) matching '#{action[:value]}' in cookie '#{cookie.to_s}'" )
|
2013-06-23 21:32:45 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
}
|
2013-06-25 12:24:39 +00:00
|
|
|
assert( false, "(#{test[:name]}) not matching '#{action[:value]}' in cookie '#{cookies.to_s}'" )
|
2013-06-23 21:32:45 +00:00
|
|
|
return
|
2013-03-22 08:29:44 +00:00
|
|
|
elsif action[:element] == :alert
|
|
|
|
element = instance.switch_to.alert
|
2013-06-08 22:35:18 +00:00
|
|
|
elsif action[:execute] == 'close_all_tasks'
|
|
|
|
while true
|
|
|
|
begin
|
|
|
|
element = instance.find_element( { :css => '.taskbar [data-type="close"]' } )
|
|
|
|
if element
|
|
|
|
element.click
|
|
|
|
sleep 0.8
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2013-06-17 05:38:17 +00:00
|
|
|
elsif action[:execute] == 'navigate'
|
|
|
|
instance.navigate.to( action[:to] )
|
2013-06-18 20:58:29 +00:00
|
|
|
elsif action[:execute] == 'reload'
|
|
|
|
instance.navigate.refresh
|
2013-06-23 21:32:45 +00:00
|
|
|
elsif action[:execute] == 'js'
|
|
|
|
result = instance.execute_script( action[:value] )
|
2013-06-18 20:58:29 +00:00
|
|
|
elsif action[:link]
|
|
|
|
if action[:link].match '###stack###'
|
|
|
|
action[:link].gsub! '###stack###', @stack
|
|
|
|
end
|
|
|
|
element = instance.find_element( { :partial_link_text => action[:link] } )
|
2012-12-14 12:16:04 +00:00
|
|
|
else
|
|
|
|
assert( false, "(#{test[:name]}) unknow selector for '#{action[:element]}'" )
|
|
|
|
end
|
|
|
|
if action[:execute] == 'set'
|
2013-03-22 08:29:44 +00:00
|
|
|
element.clear
|
2013-04-23 18:36:09 +00:00
|
|
|
if action[:value] == '###stack###'
|
|
|
|
element.send_keys( @stack )
|
|
|
|
else
|
|
|
|
element.send_keys( action[:value] )
|
|
|
|
end
|
2013-04-21 23:03:19 +00:00
|
|
|
elsif action[:execute] == 'sendkey'
|
|
|
|
element.send_keys( action[:value] )
|
2013-02-21 21:09:36 +00:00
|
|
|
elsif action[:execute] == 'select'
|
2013-02-23 22:54:48 +00:00
|
|
|
dropdown = Selenium::WebDriver::Support::Select.new(element)
|
|
|
|
dropdown.select_by(:text, action[:value])
|
2012-12-14 12:16:04 +00:00
|
|
|
elsif action[:execute] == 'click'
|
2013-06-10 11:59:16 +00:00
|
|
|
if element.class == Array
|
|
|
|
element.each {|item|
|
|
|
|
item.click
|
|
|
|
}
|
|
|
|
else
|
|
|
|
element.click
|
2013-06-08 22:35:18 +00:00
|
|
|
end
|
2013-03-22 08:29:44 +00:00
|
|
|
elsif action[:execute] == 'accept'
|
|
|
|
element.accept
|
|
|
|
elsif action[:execute] == 'dismiss'
|
|
|
|
element.dismiss
|
2012-12-14 12:16:04 +00:00
|
|
|
elsif action[:execute] == 'send_key'
|
|
|
|
element.send_keys action[:value]
|
|
|
|
elsif action[:execute] == 'match'
|
2013-02-22 10:36:00 +00:00
|
|
|
if action[:css] =~ /select/
|
2013-02-23 22:54:48 +00:00
|
|
|
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
|
2012-12-14 12:16:04 +00:00
|
|
|
if action[:match_result]
|
2013-02-22 10:36:00 +00:00
|
|
|
if success
|
|
|
|
assert( true, "(#{test[:name]}) matching '#{action[:value]}' in select list" )
|
|
|
|
else
|
|
|
|
assert( false, "(#{test[:name]}) not matching '#{action[:value]}' in select list" )
|
|
|
|
end
|
2012-12-14 12:16:04 +00:00
|
|
|
else
|
2013-02-22 10:36:00 +00:00
|
|
|
if success
|
|
|
|
assert( false, "(#{test[:name]}) not matching '#{action[:value]}' in select list" )
|
|
|
|
else
|
|
|
|
assert( true, "(#{test[:name]}) matching '#{action[:value]}' in select list" )
|
|
|
|
end
|
2012-12-14 12:16:04 +00:00
|
|
|
end
|
|
|
|
else
|
2013-05-28 12:24:48 +00:00
|
|
|
if action[:css] =~ /(input|textarea)/i
|
2013-02-23 22:54:48 +00:00
|
|
|
text = element.attribute('value')
|
2012-12-14 12:16:04 +00:00
|
|
|
else
|
2013-02-22 10:36:00 +00:00
|
|
|
text = element.text
|
|
|
|
end
|
2013-04-23 18:36:09 +00:00
|
|
|
if action[:value] == '###stack###'
|
|
|
|
action[:value] = @stack
|
|
|
|
end
|
|
|
|
match = false
|
|
|
|
if action[:no_quote]
|
|
|
|
if text =~ /#{action[:value]}/
|
|
|
|
if $1
|
|
|
|
@stack = $1
|
|
|
|
end
|
|
|
|
match = $1 || true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if text =~ /#{Regexp.quote(action[:value])}/
|
|
|
|
match = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if match
|
2013-02-22 10:36:00 +00:00
|
|
|
if action[:match_result]
|
|
|
|
assert( true, "(#{test[:name]}) matching '#{action[:value]}' in content '#{text}'" )
|
|
|
|
else
|
|
|
|
assert( false, "(#{test[:name]}) matching '#{action[:value]}' in content '#{text}' but should not!" )
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if !action[:match_result]
|
|
|
|
assert( true, "(#{test[:name]}) not matching '#{action[:value]}' in content '#{text}'" )
|
|
|
|
else
|
|
|
|
assert( false, "(#{test[:name]}) not matching '#{action[:value]}' in content '#{text}' but should not!" )
|
|
|
|
end
|
2012-12-14 12:16:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
elsif action[:execute] == 'check'
|
2013-06-08 22:35:18 +00:00
|
|
|
elsif action[:execute] == 'close_all_tasks'
|
2013-06-17 05:38:17 +00:00
|
|
|
elsif action[:execute] == 'navigate'
|
2013-06-18 20:58:29 +00:00
|
|
|
elsif action[:execute] == 'reload'
|
2013-06-23 21:32:45 +00:00
|
|
|
elsif action[:execute] == 'js'
|
2012-12-14 12:16:04 +00:00
|
|
|
else
|
|
|
|
assert( false, "(#{test[:name]}) unknow action '#{action[:execute]}'" )
|
|
|
|
end
|
|
|
|
end
|
2012-12-14 10:14:48 +00:00
|
|
|
end
|