2012-12-14 10:14:48 +00:00
|
|
|
ENV["RAILS_ENV"] = "test"
|
|
|
|
require File.expand_path('../../config/environment', __FILE__)
|
|
|
|
require 'rails/test_help'
|
|
|
|
require 'watir-webdriver'
|
|
|
|
|
|
|
|
class ActiveSupport::TestCase
|
|
|
|
|
|
|
|
# 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',
|
2012-12-14 12:16:04 +00:00
|
|
|
:instance => data[:instance] || Watir::Browser.new,
|
|
|
|
:url => data[:url] || 'http://localhost:3000',
|
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',
|
|
|
|
:value => 3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
: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
|
|
|
|
tests.each { |test|
|
|
|
|
if test[:instance]
|
|
|
|
instance = test[:instance]
|
|
|
|
end
|
|
|
|
if test[:url]
|
|
|
|
instance.goto( test[:url] )
|
|
|
|
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
|
2012-12-14 12:16:04 +00:00
|
|
|
|
|
|
|
def browser_element_action(test, action, instance)
|
2013-02-22 10:36:00 +00:00
|
|
|
if action[:css]
|
|
|
|
element = instance.element( { :css => action[:css] } )
|
2012-12-14 12:16:04 +00:00
|
|
|
if action[:result] == false
|
2013-02-22 10:36:00 +00:00
|
|
|
assert( !element.exists?, "(#{test[:name]}) Element with css '#{action[:css]}' exists" )
|
2012-12-14 12:16:04 +00:00
|
|
|
else
|
2013-02-22 10:36:00 +00:00
|
|
|
assert( element.exists?, "(#{test[:name]}) Element with css '#{action[:css]}' doesn't exist" )
|
2012-12-14 12:16:04 +00:00
|
|
|
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'
|
2013-02-22 10:36:00 +00:00
|
|
|
element.to_subtype.set( action[:value] )
|
2013-02-21 21:09:36 +00:00
|
|
|
elsif action[:execute] == 'select'
|
2013-02-22 10:36:00 +00:00
|
|
|
element.to_subtype.select( action[:value] )
|
2012-12-14 12:16:04 +00:00
|
|
|
elsif action[:execute] == 'click'
|
|
|
|
element.click
|
|
|
|
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/
|
|
|
|
success = element.to_subtype.selected?(action[:value])
|
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-02-22 10:36:00 +00:00
|
|
|
if action[:css] =~ /input|textarea/i
|
|
|
|
text = element.to_subtype.value
|
2012-12-14 12:16:04 +00:00
|
|
|
else
|
2013-02-22 10:36:00 +00:00
|
|
|
text = element.text
|
|
|
|
end
|
|
|
|
if text =~ /#{Regexp.quote(action[:value])}/
|
|
|
|
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'
|
|
|
|
else
|
|
|
|
assert( false, "(#{test[:name]}) unknow action '#{action[:execute]}'" )
|
|
|
|
end
|
|
|
|
end
|
2012-12-14 10:14:48 +00:00
|
|
|
end
|