Web Application Testing in Ruby: How to Create watir Frame work : Customer Module.rb
require 'test/unit'
02 include Test::Unit::Assertions
03
04 module Admin
05 TITLE = 'ADMINISTER Pragprog Books Online Store'
06 URL = 'http://localhost:3000/admin/'
07
08 def Admin.log_on(browser, username, password)
09 browser.goto(URL)
10 if browser.link(:text,'Log out').exist? then #if already logged in
11 browser.link(:text,'Log out').click
12 end
13 browser.text_field(:id, 'user_name').set username
14 browser.text_field(:id, 'user_password').set password
15 browser.button(:value, ' LOGIN ').click
16 if browser.div(:id, 'notice').exist? then
17 return false,browser.div(:id, 'notice').text
18 else
19 return true,''
20 end
21 end
22
23 def Admin.ship_items(browser, name)
24 browser.goto(URL)
25 browser.link(:text, 'Shipping').click
26 num_orders = 0
27 index = 0
28 browser.form(:action,'/admin/ship').divs.each do |div|
29 if div.class_name == "olname"
30 index+=1
31 if div.text == name then
32 browser.form(:action,'/admin/ship').checkbox(:index, index).set
33 num_orders+=1
34 end
35 end
36 end
37
38 browser.button(:value, ' SHIP CHECKED ITEMS ').click
39
40 if num_orders == 1 then
41 assert_equal(browser.div(:id,"notice").text, "One order marked as shipped","Correct notice")
42 elsif num_orders > 1 then
43 assert_equal(browser.div(:id,"notice").text, "#{num_orders} orders marked as shipped","Correct notice")
44 end
45 return true, num_orders.to_s
46 end
47
48 end
This will help you for Automation Software Testing with Watir Tool(Ruby)
Tuesday, February 1, 2011
How to Create watir Frame work : Customer Module.rb
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
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
Labels:
General Testing,
Watir Framework,
Watir testing
Subscribe to:
Posts (Atom)