Web Application Testing in Ruby: How to Create watir Frame work : Test Driver tc_main.rb
require 'test/unit'
002 include Test::Unit::Assertions
003
004 module Customer
005
006 TITLE = 'Pragprog Books Online Store'
007 URL = 'http://localhost:3000/store/'
008
009 # Description:: Adds a book named 'book_title' to cart
010 def Customer.add_book(browser, book_title)
011 browser.goto(URL)
012 # Check if title is already in cart - so we can check it was added correctly
013 browser.link(:text,'Show my cart').click
014 prev_cart_count = 0
015 prev_cart_total = 0.00
016 if not browser.div(:text,'Your cart is currently empty').exist? then
017 # We have a non-empty cart
018 for row in browser.table(:index,1)
019 if row[2].text == book_title then
020 prev_cart_count = row[1].text.to_i
021 break
022 end
023 end
024 prev_cart_total = browser.cell(:id, 'totalcell').text[1..-1].to_f #remove $ sign
025 browser.link(:text, 'Continue shopping').click
026 end
027
028 found = false
029 book_price = 0.00
030 1.upto(browser.divs.length) do |index|
031 if (browser.div(:index,index).attribute_value('className') == 'catalogentry') and (browser.div(:index,index).h3(:text,book_title).exists?) then
032 book_price = browser.div(:index,index).span(:class, 'catalogprice').text[1..-1].to_f #remove $ sign
033 browser.div(:index,index).link(:class,'addtocart').click
034 found = true
035 break
036 end
037 end
038 if not found then
039 return false,'Could not locate title in store'
040 end
041
042 new_cart_count = 0
043 for row in browser.table(:index,1)
044 if row[2].text == book_title then
045 new_cart_count = row[1].text.to_i
046 break
047 end
048 end
049 new_cart_total = browser.cell(:id, 'totalcell').text[1..-1].to_f # remove $ sign
050 assert_equal(new_cart_count,(prev_cart_count+1), "Ensure that new quantity is now one greater than previously")
051 assert_equal(new_cart_total,(prev_cart_total + book_price), "Ensure that new cart total is old cart total plus book price")
052 browser.link(:text, 'Continue shopping').click
053 return true,new_cart_total
054 end
055
056 def Customer.check_out(browser, customerName, customerEmail, customerAddress, customerPaymentMethod)
057 browser.goto(URL)
058 browser.link(:text,'Show my cart').click
059 if browser.div(:text,'Your cart is currently empty').exist? then
060 return false,'Your cart is currently empty'
061 end
062 browser.link(:text,"Checkout").click
063 browser.text_field(:id, 'order_name').set(customerName)
064 browser.text_field(:id, 'order_email').set(customerEmail)
065 browser.text_field(:id, 'order_address').set(customerAddress)
066 begin
067 browser.select_list(:id, 'order_pay_type').select(customerPaymentMethod)
068 rescue Watir::Exception::NoValueFoundException
069 flunk('Could not locate customer payment method in drop down list: '+customerPaymentMethod)
070 end
071 browser.button(:name, 'commit').click
072 if browser.div(:id,'errorExplanation').exist? then
073 error = ''
074 1.upto(browser.div(:id,'errorExplanation').lis.length) do |index|
075 error << (browser.div(:id,'errorExplanation').li(:index,index).text + ",")
076 end
077 browser.link(:text,'Continue shopping').click
078 return false, error
079 end
080 assert_equal(browser.div(:id,'notice').text, 'Thank you for your order.',"Thank you for your order should appear.")
081 return true,''
082 end
083
084 def Customer.empty_cart(browser)
085 browser.goto(URL)
086 browser.link(:text,"Show my cart").click
087 if browser.div(:text,"Your cart is currently empty").exist? then
088 assert('Cart was never empty')
089 else
090 browser.link(:text,'Empty cart').click
091 assert_equal(browser.div(:id, 'notice').text,'Your cart is now empty')
092 end
093 return true,''
094 end
095
096 def Customer.check_cart_total(browser, exp_total)
097 browser.goto(URL)
098 browser.link(:text,'Show my cart').click
099 if browser.div(:text,'Your cart is currently empty').exist? then
100 return false,'Your cart is currently empty'
101 end
102 act_total = browser.cell(:id, 'totalcell').text[1..-1].to_f
103 assert_equal(act_total,exp_total.to_f,"Check that cart total is as expected.")
104 return true,act_total
105 end
106 end
No comments:
Post a Comment