Advertisement
saasbook

Examples of checking view elements using XPath and Nokogiri

Mar 15th, 2012
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.75 KB | None | 0 0
  1. # Check if menu option selected
  2. Then /^"(.*)" should be selected in the "(.*)" menu$/ do |opt,menu|
  3.   html = Nokogiri::HTML(page.body)
  4.   menu_id = if !html.xpath("//select[@id='#{menu}']").empty? then menu else html.xpath("//label[contains(text(),'#{menu}')]").first['for'] end
  5.   html.xpath("//select[@id='#{menu_id}']/option[contains(text(),'#{opt}')]").first['selected'].should_not be_blank
  6. end
  7.  
  8. # Variant for dates
  9. Then /^"(.*)" should be selected as the "(.*)" date$/ do |date,menu|
  10.   date = Time.parse(date)
  11.   html = Nokogiri::HTML(page.body)
  12.   menu_id = html.xpath("//label[contains(text(),'#{menu}')]").first['for']
  13.   year, month, day =
  14.     html.xpath("//select[@id='#{menu_id}_1i']").empty? ? %w[year month day] : %w[1i 2i 3i]
  15.   Then %Q{"#{date.year}" should be selected in the "#{menu_id}_#{year}" menu}
  16.   Then %Q{"#{Date::MONTHNAMES[date.month]}" should be selected in the "#{menu_id}_#{month}" menu}
  17.   Then %Q{"#{date.day}" should be selected in the "#{menu_id}_#{day}" menu}
  18. end
  19.  
  20.  
  21. # 'should come before/should come after' for verifying orderings of things
  22. Then /^(.*):"(.*)" should come (before|after) (.*):"(.*)" within "(.*)"$/ do |tag1,val1,order,tag2,val2,sel|
  23.   html = Nokogiri::HTML(page.body)
  24.   elt1 = html.xpath("//#{sel}//#{tag1}[contains(.,'#{val1}')]").first
  25.   elt2 = html.xpath("//#{sel}//#{tag2}[contains(.,'#{val2}')]").first
  26.   sequence = (elt1 <=> elt2)
  27.   if order =~ /before/
  28.     assert sequence == -1
  29.   else
  30.     assert sequence == 1
  31.   end
  32. end
  33.  
  34.  
  35. # tabular data
  36. Then /^I should see a row "(.*)" within "(.*)"$/ do |row, table|
  37.   page.should have_css(table)
  38.   @html = Nokogiri::HTML(page.body)
  39.   @rows = @html.xpath("//#{table}//tr").collect { |r| r.xpath('.//th|td') }
  40.   col_regexps = row.split('|').map { |s| Regexp.new(s) }
  41.   @rows.any? do |table_row|
  42.     match = true
  43.     col_regexps.each_with_index do |regexp,index|
  44.       match &&= table_row[index].content.match(regexp)
  45.     end
  46.     match
  47.   end.should be_true, "Expected #{table} to contain a row matching <#{row}>"
  48. end
  49.    
  50.  
  51. Then /^I should see a table "(.*)" with rows? (.*)$/ do |table,all_rows|
  52.   page.should have_css(table)
  53.   all_rows.split(/, ?/).each do |row|
  54.     Then "I should see a row #{row} within \"#{table}\""
  55.   end
  56. end      
  57.  
  58. Then /^I should see a quantity menu for "([^\"]*)"$/ do |name|
  59.   vtype = Vouchertype.find_by_name(name)
  60.   page.should have_css("select.itemQty[name='vouchertype[#{vtype.id}]']")
  61. end
  62.  
  63. Then /^the "([^\"]*)" menu should contain "([^\"]*)"$/ do |menu,choice|
  64.   page.should(have_css("select[id=#{menu}]") || have_css("select[name=#{menu}]")) do |m|
  65.     m.should have_css("option", :content => choice)
  66.   end
  67. end
  68.  
  69. Then /^I should see the "(.*)" message$/ do |m|
  70.   page.should(have_css("div##{m}") || have_css("div.#{m}"))
  71. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement