Skip to content

Commit

Permalink
erb/erubis support in the yaml values
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Powell committed Feb 8, 2012
1 parent 5740030 commit ed86147
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 25 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Requirements
------------ ------------


- rspec - rspec
- erubis
- selenium-webdriver - selenium-webdriver
- Selenium Server standalone from http://seleniumhq.org/download/ - Selenium Server standalone from http://seleniumhq.org/download/


Expand Down Expand Up @@ -82,8 +83,8 @@ for a list.
Most of the rest of the options are of no use with tests as simple Most of the rest of the options are of no use with tests as simple
as the example. as the example.


YAML Data YAML Data Generation
--------- --------------------


Part of the point here is to have your tests be driven by data Part of the point here is to have your tests be driven by data
files as much as possible, so that you can chance information about files as much as possible, so that you can chance information about
Expand Down Expand Up @@ -130,8 +131,13 @@ each possible case can get very annoying, thus it's possible to
change the main data set (called $yaml_data in the code) from the change the main data set (called $yaml_data in the code) from the
command line: command line:


bin/runtest.rb -c 'server_url=http://www.google.co.jp/' bin/runtest.rb -c 'base_url=google.co.jp'


This will run the tests with a different server_url in $yaml_data, This will run the tests with a different server_url in $yaml_data,
which will make things break pretty badly. :) You can use as many which will make things break pretty badly. :) You can use as many
-c options as you like, and the format is just x=y -c options as you like, and the format is just x=y

To support this sort of variable replacement, all of the library
functions that take only a $yaml_data key will also perform erb
replacement just before actual use; look for "base_url" in the yaml
files to see examples.
63 changes: 48 additions & 15 deletions lib/GeneralSeleniumUtility.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -92,16 +92,49 @@ def selenium_setup
def load_yaml_data( file ) def load_yaml_data( file )
yaml_data = {} yaml_data = {}
yaml_data.merge!( File.open( file ) { |yf| YAML::load( yf ) } ) yaml_data.merge!( File.open( file ) { |yf| YAML::load( yf ) } )

return yaml_data return yaml_data
end end


def erubisize( data )
require 'erubis'
eruby = Erubis::Eruby.new( data )
value = eruby.result(binding())

return value
end
def get_yaml_data( yaml_data_key, sub_key = nil )
data = $yaml_data[yaml_data_key]

# print "gyd: data first: "+data.inspect+"\n"

if sub_key
data = data[sub_key]
end

# print "gyd: data second: "+data.inspect+"\n"

# Do erb processing in most normal cases
if data.class == Array
data.map! { |x| erubisize(x) }
elsif data.class == Hash
data.keys { |x| data[x] = erubisize(data[x]) }
elsif data.class == String
data = erubisize(data)
end

# print "gyd: data after: "+data.inspect+"\n"

return data
end

#**************** #****************
# Go to the page url given by the yaml_data key # Go to the page url given by the yaml_data key
#**************** #****************
def go_to(yaml_data_key) def go_to(yaml_data_key)
@driver.navigate.to $yaml_data[yaml_data_key][0] @driver.navigate.to get_yaml_data( yaml_data_key, 0 )
quiesce quiesce
@driver.current_url.should =~ Regexp.new($yaml_data[yaml_data_key][1], Regexp::MULTILINE) @driver.current_url.should =~ Regexp.new(get_yaml_data(yaml_data_key, 1), Regexp::MULTILINE)
end end


#**************** #****************
Expand All @@ -110,10 +143,10 @@ def go_to(yaml_data_key)
#**************** #****************
def check_element_send_keys(yaml_data_key) def check_element_send_keys(yaml_data_key)
check_element_send_keys_raw( check_element_send_keys_raw(
$yaml_data[yaml_data_key][0].to_sym, get_yaml_data( yaml_data_key, 0 ).to_sym,
$yaml_data[yaml_data_key][1], get_yaml_data( yaml_data_key, 1 ),
$yaml_data[yaml_data_key][2], get_yaml_data( yaml_data_key, 2 ),
$yaml_data[yaml_data_key][3] get_yaml_data( yaml_data_key, 3 )
) )
end end
def check_element_send_keys_raw(how, what, tag_name, to_type) def check_element_send_keys_raw(how, what, tag_name, to_type)
Expand All @@ -139,11 +172,11 @@ def check_element_send_keys_raw(how, what, tag_name, to_type)
#**************** #****************
def check_element_click(yaml_data_key) def check_element_click(yaml_data_key)
check_element_click_raw( check_element_click_raw(
$yaml_data[yaml_data_key][0].to_sym, get_yaml_data( yaml_data_key, 0 ).to_sym,
$yaml_data[yaml_data_key][1], get_yaml_data( yaml_data_key, 1 ),
$yaml_data[yaml_data_key][2], get_yaml_data( yaml_data_key, 2 ),
$yaml_data[yaml_data_key][3], get_yaml_data( yaml_data_key, 3 ),
$yaml_data[yaml_data_key][4] get_yaml_data( yaml_data_key, 4 )
) )
end end
def check_element_click_raw(how, what, tag_name, resulting_url, alert_text = nil) def check_element_click_raw(how, what, tag_name, resulting_url, alert_text = nil)
Expand Down Expand Up @@ -191,9 +224,9 @@ def check_element_click_raw(how, what, tag_name, resulting_url, alert_text = nil
#**************** #****************
def no_move_click(yaml_data_key) def no_move_click(yaml_data_key)
no_move_click_raw( no_move_click_raw(
$yaml_data[yaml_data_key][0].to_sym, get_yaml_data( yaml_data_key, 0 ).to_sym,
$yaml_data[yaml_data_key][1], get_yaml_data( yaml_data_key, 1 ),
$yaml_data[yaml_data_key][2] get_yaml_data( yaml_data_key, 2 )
) )
end end
def no_move_click_raw(how, what, tag_name) def no_move_click_raw(how, what, tag_name)
Expand Down Expand Up @@ -409,7 +442,7 @@ def quiesce()
# page, errors if it doesn't. # page, errors if it doesn't.
#**************** #****************
def wait_for_element(yaml_data_key) def wait_for_element(yaml_data_key)
wait_for_element_raw($yaml_data[yaml_data_key][0], $yaml_data[yaml_data_key][1]) wait_for_element_raw(get_yaml_data( yaml_data_key, 0 ), get_yaml_data( yaml_data_key, 1 ))
end end
def wait_for_element_raw(how, what) def wait_for_element_raw(how, what)
e = nil e = nil
Expand Down
5 changes: 3 additions & 2 deletions yaml/100_basic_prod.yaml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ conditions:
- -
- test_set - test_set
- basic - basic
base_url: "google.com"
server_url: server_url:
- "http://www.google.com/" - "http://www.<%= $yaml_data['base_url'] %>/"
- "^http://www.google.com/$" - "^http://www.<%= $yaml_data['base_url'] %>/$"
2 changes: 1 addition & 1 deletion yaml/BasicSearch_000_basic.yaml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ basic_search_click_search:
- name - name
- "btnK" - "btnK"
- "input" - "input"
- "^http://www.google.com/.*q=monkeys.*$" - "^http://www.<%= $yaml_data['base_url'] %>/.*q=monkeys.*$"


basic_search_search_monkeys: basic_search_search_monkeys:
- id - id
Expand Down
6 changes: 3 additions & 3 deletions yaml/Setup_000_basic.yaml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ setup_instant_predictions_div2: - id
setup_click_save: - xpath setup_click_save: - xpath
- "//div[text()='Save']" - "//div[text()='Save']"
- "div" - "div"
- "^http://www.google.com/$" - "^http://www.<%= $yaml_data['base_url'] %>/$"


setup_url: setup_url:
- "http://www.google.com/preferences?hl=en" - "http://www.<%= $yaml_data['base_url'] %>/preferences?hl=en"
- "^http://www.google.com/preferences.hl=en$" - "^http://www.<%= $yaml_data['base_url'] %>/preferences.hl=en$"
2 changes: 1 addition & 1 deletion yaml/Translate_000_basic.yaml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ translate_click_translate:
- id - id
- "gb_51" - "gb_51"
- "a" - "a"
- "^http://translate.google.com/.hl=en.tab=wT$" - "^http://translate.<%= $yaml_data['base_url'] %>/.hl=en.tab=wT$"

0 comments on commit ed86147

Please sign in to comment.