Showing posts with label Watir Framework. Show all posts
Showing posts with label Watir Framework. Show all posts

Friday, February 4, 2011

Watir Framework

A Watir framework that object models your AUT and uses Rspec Story Runner aidy dot lewis at googlemail dot com

gem install rspec

Run example from:
watirFramework\runner\gmail.rb

(you will need to sign out of gmail)

watirFramework.zip you want this mail me...or comment on this post


The rspecReports zip contains js and css to colour your HTML reports

Red is a fail
Green a pass
Yellow means pending

eg.
cd watirFramework\runner
ruby gmail.rb -fh: > "C:\rspec_reports\gmail.htm"

rspecReports.zip
you want this mail me...or comment on this post


Labels parameters

Labels

Is Ruby is a Scripting Language or Compiled Language?

In general, programming languages fall into one of two
categories: they're either compiled languages or scripting
languages. Let's explore what each of those terms means, and
understand the differences between them.

Compiled Languages: The language in which you write an
application is not actually something that your computer
understands. Your code needs to be translated into bits and
bytes that can be executed by your computer. This process of
translation is called compilation, and any language that
requires compilation is referred to as a compiled language.
Examples of compiled languages include C, C#, and Java.

For a compiled language, the actual compilation is the final
step in the development process. You invoke a compiler --
the software program that translates your final
hand-written, human-readable code into machine-readable code
-- and the compiler creates an executable file. This final
product is then able to execute independently of the
original source code.

Thus, if you make changes to your code, and you want those
changes to be incorporated into the application, you must
stop the running application, recompile it, then start the
application again.

Scripting Languages: On the other hand, a scripting language
such as Ruby, PHP, or Python, relies upon an application's
source code all of the time. Scripting languages don't have
a compiler or a compilation phase per se; instead, they use
an interpreter -- a program that runs on the web server --
to translate hand-written code into machine-executable code
on the fly. The link between the running application and
your hand-crafted code is never severed, because that
scripting code is translated every time it is invoked -- in
other words, for every web page that your application renders.

As you might have gathered from the name, the use of an
interpreter rather than a compiler is the major difference
between a scripting language and a compiled language.

The Great Performance Debate: If you've come from a
compiled-language background, you might be concerned by all
this talk of translating code on the fly -- how does it
affect the application's performance?

These concerns are valid -- translating code on the web
server every time it's needed is certainly more expensive,
performance-wise, than executing pre-compiled code, as it
requires more effort on the part of your machine's
processor. The good news is that there are ways to speed up
scripted languages, including techniques such as code
caching and persistent interpreters. However, both topics
are beyond the scope of this book.

There's also an upside to scripted languages in terms of
performance -- namely, your performance while developing an
application.

Imagine that you've just compiled a shiny new Java
application, and launched it for the first time ... and then
you notice a typo on the welcome screen. To fix it, you have
to stop your application, go back to the source code, fix
the typo, wait for the code to recompile, and restart your
application to confirm that it is fixed. And if you find
another typo, you'll need to repeat that process again.
Lather, rinse, repeat.

In a scripting language, you can fix the typo and just
reload the page in your browser -- no restart, no recompile,
no nothing. It's as simple as that.

Tuesday, February 1, 2011

How to Create watir Frame work : Admin Module.rb

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

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

How to Create watir Frame work : Test Driver tc_main.rb

Web Application Testing in Ruby: How to Create watir Frame work ?


$:.unshift File.join(File.dirname(__FILE__), ".", "lib")

002 require 'watir'

003 require 'roo'

004 require 'test/unit'

005 require 'customer'

006 require 'admin'

007 $stdout = File.new('log.txt',File::WRONLY|File::APPEND|File::CREAT)

008 $stderr = File.new('log.txt',File::WRONLY|File::APPEND|File::CREAT)

009

010 class TC_WatirMelon < Test::Unit::TestCase

011 @@colmap = {:module_name=>0, :method_name=>1, :comments=>2, :exp_outcome=>3, :exp_error=>4, :first_param=>5}

012 @@ss_format = ARGV[0]

013 @@specified_browser = ARGV[1]

014

015 def setup

016 puts "[Starting at #{Time.now}]\n"

017 case @@ss_format

018 when "excel"

019 @ss = Excel.new("watirmelon.xls")

020 when "wiki"

021 @ss = Excel.new("http://localhost:8080/download/attachments/2097153/watirmelon.xls")

022 when "gdocs"

023 @ss = Google.new("0AtL3mPY2rEqmdEY3XzRqUlZKSmM5Z3EtM21UdFdqb1E")

024 else

025 @ss = Openoffice.new("watirmelon.ods")

026 end

027 @ss.default_sheet = @ss.sheets.first

028 case @@specified_browser

029 when "firefox"

030 Watir::Browser.default = 'firefox'

031 @browser = Watir::Browser.new

032 else

033 Watir::Browser.default = 'ie'

034 @browser = Watir::Browser.new

035 @browser.speed = :zippy

036 @browser.visible = true

037 end

038 end

039

040 def test_run_sheet()

041 @ss.first_row.upto(@ss.last_row) do |row|

042 #Read row into array

043 line = Array.new

044 @ss.first_column.upto(@ss.last_column) do |column|

045 line << @ss.cell(row, column).to_s.strip

046 end

047

048 module_name = line[@@colmap[:module_name]]

049 if module_name != "Function" then #if not a header

050 method_name = line[@@colmap[:method_name]].downcase.gsub(' ','_') #automatically determine ruby method name based upon data sheet

051 exp_outcome = line[@@colmap[:exp_outcome]]

052 exp_error = line[@@colmap[:exp_error]]

053 first_param = @@colmap[:first_param]

054 required_module = Kernel.const_get(module_name)

055 required_method = required_module.method(method_name)

056 arity = required_method.arity() # this is how many arguments the method requires, it is negative if a 'catch all' is supplied.

057 arity = ((arity * -1) - 1) if arity < 0 # arity is negative when there is a 'catch all'

058 arity = arity-1 # Ignore the first browser parameter

059 unless arity == 0

060 parameters = line[first_param..first_param+(arity-1)]

061 else

062 parameters = []

063 end

064 begin

065 act_outcome, act_output = required_method.call(@browser, *parameters)

066 rescue Test::Unit::AssertionFailedError => e

067 self.send(:add_failure, e.message, e.backtrace)

068 act_outcome = false

069 act_output = e.message

070 end

071 if (exp_outcome == 'Success') and act_outcome then

072 assert(true, "Expected outcome and actual outcome are the same")

073 result = 'PASS'

074 elsif (exp_outcome == 'Error') and (not act_outcome) and (exp_error.strip! == act_output.strip!)

075 assert(true, "Expected outcome and actual outcome are the same, and error messages match")

076 result = 'PASS'

077 else

078 result = 'FAIL'

079 begin

080 assert(false,"Row: #{row}: Expected outcome and actual outcome for #{method_name} for #{module_name} do not match, or error messages do not match.")

081 rescue Test::Unit::AssertionFailedError => e

082 self.send(:add_failure, e.message, e.backtrace)

083 end

084 end

085 puts "###########################################"

086 puts "[Running: #{module_name}.#{method_name}]"

087 puts "[Expected Outcome: #{exp_outcome}]"

088 puts "[Expected Error: #{exp_error}]"

089 puts "[Actual Outcome: Success]" if act_outcome

090 puts "[Actual Outcome: Error]" if not act_outcome

091 puts "[Actual Output: #{act_output}]"

092 puts "[RESULT: #{result}]"

093 puts "###########################################"

094 end

095 end

096 end

097

098 def teardown

099 @browser.close

100 puts "[Finishing at #{Time.now}]\n\n"

101 end

102

103 end

How to Create watir Frame work ?

One common challenge I see over and over again is people figuring out how to design a logical and maintainable automated testing framework. I have designed quite a few frameworks for various projects, but one thing that has consistently been a win for me is purposely separating test case and test execution design.

It’s therefore logical that the design of my Watir framework deliberately separates test case design and test execution design so that:

■test case design is done visually in spreadsheets; and
■test execution design is done in ruby methods, because code is the most efficient and maintainable way.
Since I last published details about my framework on this blog, I have started doing assertions using the Test::Unit ruby library. The reasons I chose Test::Unit are:

■it is easy to ‘mix-in’ Test::Unit assertions into modules of ruby code using include Test::Unit::Assertions;
■it is included with ruby;
■ruby scripts with Test::Unit::TestCase are instantly executable, in my case, from SciTE;
■its assertions are easy to understand and use.
I have also made some other improvements to my framework code, including:

■the ability to specify browser types, and spreadsheet sources, as command line arguments (with defaults);
■logging test output to a file;
■no longer attaching to an open browser, the same browser instance is used completely for all tests (and elegantly closed at the end).
The main design has been kept the same, in that a spreadsheet (either excel, openoffice or Google Docs) contains tests grouped by functional area, which call a method in a particular module.

The great thing about my framework is that adding a new test is a matter of designing the test case, and then writing the ruby method: as the methods are called dynamically from the spreadsheet, no extra glue is needed!

Enough talk, here’s the code. The Google spreadsheet is here. You can find a .zip file of all the required files to run it here. It runs on the depot app, which you get here. You will need two gems: Watir (oh duh), and Roo.