Permalink
Browse files

erb/erubis support in the yaml values

  • Loading branch information...
1 parent 5740030 commit ed8614765717174c0180652968f58e0d09350e1c @rlpowell committed Feb 8, 2012
View
@@ -16,6 +16,7 @@ Requirements
------------
- rspec
+- erubis
- selenium-webdriver
- Selenium Server standalone from http://seleniumhq.org/download/
@@ -82,8 +83,8 @@ for a list.
Most of the rest of the options are of no use with tests as simple
as the example.
-YAML Data
----------
+YAML Data Generation
+--------------------
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
@@ -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
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,
which will make things break pretty badly. :) You can use as many
-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.
@@ -92,16 +92,49 @@ def selenium_setup
def load_yaml_data( file )
yaml_data = {}
yaml_data.merge!( File.open( file ) { |yf| YAML::load( yf ) } )
+
return yaml_data
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
#****************
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
- @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
#****************
@@ -110,10 +143,10 @@ def go_to(yaml_data_key)
#****************
def check_element_send_keys(yaml_data_key)
check_element_send_keys_raw(
- $yaml_data[yaml_data_key][0].to_sym,
- $yaml_data[yaml_data_key][1],
- $yaml_data[yaml_data_key][2],
- $yaml_data[yaml_data_key][3]
+ get_yaml_data( yaml_data_key, 0 ).to_sym,
+ get_yaml_data( yaml_data_key, 1 ),
+ get_yaml_data( yaml_data_key, 2 ),
+ get_yaml_data( yaml_data_key, 3 )
)
end
def check_element_send_keys_raw(how, what, tag_name, to_type)
@@ -139,11 +172,11 @@ def check_element_send_keys_raw(how, what, tag_name, to_type)
#****************
def check_element_click(yaml_data_key)
check_element_click_raw(
- $yaml_data[yaml_data_key][0].to_sym,
- $yaml_data[yaml_data_key][1],
- $yaml_data[yaml_data_key][2],
- $yaml_data[yaml_data_key][3],
- $yaml_data[yaml_data_key][4]
+ get_yaml_data( yaml_data_key, 0 ).to_sym,
+ get_yaml_data( yaml_data_key, 1 ),
+ get_yaml_data( yaml_data_key, 2 ),
+ get_yaml_data( yaml_data_key, 3 ),
+ get_yaml_data( yaml_data_key, 4 )
)
end
def check_element_click_raw(how, what, tag_name, resulting_url, alert_text = nil)
@@ -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)
no_move_click_raw(
- $yaml_data[yaml_data_key][0].to_sym,
- $yaml_data[yaml_data_key][1],
- $yaml_data[yaml_data_key][2]
+ get_yaml_data( yaml_data_key, 0 ).to_sym,
+ get_yaml_data( yaml_data_key, 1 ),
+ get_yaml_data( yaml_data_key, 2 )
)
end
def no_move_click_raw(how, what, tag_name)
@@ -409,7 +442,7 @@ def quiesce()
# page, errors if it doesn't.
#****************
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
def wait_for_element_raw(how, what)
e = nil
@@ -3,6 +3,7 @@ conditions:
-
- test_set
- basic
+base_url: "google.com"
server_url:
- - "http://www.google.com/"
- - "^http://www.google.com/$"
+ - "http://www.<%= $yaml_data['base_url'] %>/"
+ - "^http://www.<%= $yaml_data['base_url'] %>/$"
@@ -8,7 +8,7 @@ basic_search_click_search:
- name
- "btnK"
- "input"
- - "^http://www.google.com/.*q=monkeys.*$"
+ - "^http://www.<%= $yaml_data['base_url'] %>/.*q=monkeys.*$"
basic_search_search_monkeys:
- id
@@ -13,8 +13,8 @@ setup_instant_predictions_div2: - id
setup_click_save: - xpath
- "//div[text()='Save']"
- "div"
- - "^http://www.google.com/$"
+ - "^http://www.<%= $yaml_data['base_url'] %>/$"
setup_url:
- - "http://www.google.com/preferences?hl=en"
- - "^http://www.google.com/preferences.hl=en$"
+ - "http://www.<%= $yaml_data['base_url'] %>/preferences?hl=en"
+ - "^http://www.<%= $yaml_data['base_url'] %>/preferences.hl=en$"
@@ -12,4 +12,4 @@ translate_click_translate:
- id
- "gb_51"
- "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.