Watir



Comments



Description

For QTP Information & Scripts visit: www.gcreddy.com WATIR WATIR, pronounced "Water", is an acronym standing for "Web Application Testing in Ruby". Watir is a toolkit used to automate browser-based tests during web application development. This automated test tool uses the Ruby scripting language to drive the Internet Explorer web browser, and is available as a Ruby Gem. Watir was primarily developed by Bret Pettichord and Paul Rogers. Functionality Watir makes use of the fact that Ruby has built in OLE capabilities. As such it is possible to drive the Microsoft Internet Explorer browser programmatically. Watir operates differently than HTTP based test tools, which operate by simulating a browser. Instead Watir directly drives the browser through the Object Linking and Embedding protocol, which is implemented over the Component Object Model (COM) architecture. The COM permits interprocess communication (such as between Ruby and IE) and dynamic object creation and manipulation (which is what the Ruby program does to the Internet Explorer browser.) Microsoft calls this OLE automation, and calls the manipulating program an automation controller. Technically, the IE process is the server and serves the automation objects, exposing their methods; while the Ruby program then becomes the client which manipulates the automation objects. Installation WATIR is dependent upon Ruby, therefore both must be accessed. Ruby can be accessed at the RubyForge 1 Click Installer page. The current stable version is 1.9.1 stable, but unless otherwise stated you should use 1.8.6. WATIR can also be accessed at RubyForge. The current standard version is 1.6.2. This version is to be preferred if you do not need to handle security popups or modal dialogs. Development work on WATIR moved from the Rubyforge area after the 1.4.1 release in August 2005. It is now located at OpenQA.org. Once the WATIR 1.5 development branch is completed, it should be released back to Rubyforge here: WTR at RubyForge To install the current 1.5 Ruby Gem, go to OpenQA Development Builds and download the gem. Ruby should be installed first. Simply execute the ruby184-20.exe self extracting installer and let it run. This file will install Ruby, RubyGems, a Ruby IDE called FreeRIDE, the SciTE text editor, and various documentation. It will install in C:\ruby. RubyGems are the Ruby libraries. Type ‘gem list’ to see all installed in a command window. For Software Testing Information visit: www.gcreddy.net 1 For QTP Information & Scripts visit: www.gcreddy.com Playing around with Ruby can be done within a command window by running the irb. Or do ‘Start/All Programs/Ruby-184-20/FreeRide. It takes a few seconds for this to launch. But once it is started you may execute Ruby code. Output goes to the output view pane, or by going to ‘Edit/Preferences’ and checking ‘Run process in terminal’ in ‘Debugger/Run’ it will appear in a command window. (This is preferable; otherwise not all output might be displayed.) So do this. WATIR should be installed next. Assuming you are installing the current development gem, do the following. 1. Open a command window 2. Change to directory where the gem was downloaded (assume C:\Watir) 3. Type in: “gem install watir” You should see something like this: C:\Watir>gem install watir Successfully installed watir, version 1.5.1.1100 Installing ri documentation for watir-1.5.1.1100... Installing RDoc documentation for watir-1.5.1.1100... To uninstall this in the future, you will have to use the gem unistaller, because this installation does not add anything to your programs menu. You can validate that WATIR is installed by executing ‘gem list’ to see your installed gems. In the list you should now see something like this: watir (1.5.1.1100) Automated testing tool for web applications. If you accessed the development version from the link, you might also download the zip file titled "Bonus.zip". I suggest dropping it in the same directory where you placed the gem. Examples The google example # Here we see a very simple WATIR script to drive to google and validate a page require 'watir' # use watir gem test_site = 'http://www.google.com' # set a variable b = Watir::Browser.new # open the IE browser b.goto(test_site) # load url, go to site b.text_field(:name, "q").set("pickaxe") # load text "pickaxe" into search field named "q" b.button(:name, "btnG").click # "btnG" is the name of the Search button, click it if b.text.include?("Programming Ruby") puts "Test Passed. Found the test string: 'Programming Ruby'." else For Software Testing Information visit: www.gcreddy.net 2 For QTP Information & Scripts visit: www.gcreddy.com puts "Test Failed! Could not find: 'Programming Ruby'" end The previous commands can be executed in the Interactive Ruby Shell (irb), or in a Ruby IDE such as FreeRIDE that is installed when Ruby is installed with the once click installer. Handling pop-ups require 'watir' require 'watir/dialog' link = 'http://www.w3schools.com/js/tryit_view.asp?filename=tryjs_alert' ie = Watir::IE.start(link) # ensure popup won't block Watir ie.button(:value, 'Display alert box').click_no_wait dialog = Watir::Dialog.new # Need to be able to poll window to exist sleep 0.4 # Remember that the only button to click is OK! dialog.button('OK').click # Wait for dialog to do its stuff # and wait for IE to return to live ie.waitForIE This example extends upon the w3cshools try-it page for alert box, by ensuring the 'Display alert box' button click will not wait for the popup. This approach will handle most annoying popups, however it may be necessary to download and install the current development Watir gem rather than using the standard Watir release. View Browser Objects Suppose you want to be able to see the various objects within the browser? The following are useful: # To see objects within the browser ie.frame("top_frame").text_fields.each { |t| puts t.to_s } ie.frame("top_frame").spans.each { |s| puts s.to_s } ie.frame("top_frame").tables.each { |t| puts t.to_s } ie.frame("top_frame").links.each { |l| puts l.to_s } ie.frame("top_frame").select_lists.each { |s| puts s.to_s } ie.frame("top_frame").labels.each { |l| puts l.to_s } In the above example, this also shows you how you would access information from within a frame. If there were no frame involved, then drop the "frame("top_frame")." part of the commands. For Software Testing Information visit: www.gcreddy.net 3 For QTP Information & Scripts visit: www.gcreddy.com Capture Timings Suppose you want to capture timing information, because you are running a performance test. ie.button(:value, 'OK').click p ie.down_load_time This approach will time a single response. If you want to collect the time for a series of events, use this approach: # Performance test timing begin_time = Time.now =begin ***************************** * Body of script goes here ***************************** =end end_time = Time.now p (end_time - begin_time).to_s Be aware that anything enclosed by an "=begin" and "=end" (on their own lines) is treated as a multi line comment. Typically you might wrap the final button click to submit a page in these commands, and the timing information will then just show you the time from submission to page response. Write Output to Excel (as CSV) It may useful to be able to repeatedly record output to an Excel spreadsheet. Data written to a workbook can be displayed graphically in charts. (How to do this will not be provided here, but see the external link called "Ruby and Excel") Suppose you want to record a sequence of 4 screen to screen transition timings. You might then add the following to your script: #Read test data input file arr = IO.readlines("env.txt") executionEnvironment = arr[0] acceptableScreen1 = arr[1].to_f acceptableScreen2 = arr[2].to_f acceptableScreen3 = arr[3].to_f acceptableScreen4 = arr[4].to_f #Result OK or not resultValue = "OK" #Strip line return from tested environment string executionEnvironment = executionEnvironment.chomp #open spreadsheet - new one each day this script is run timeSpreadsheet = File.new( "../PC/" + Time.now.strftime("%d-%b-%y") + ".csv", "a") =begin ************************** For Software Testing Information visit: www.gcreddy.net 4 For QTP Information & Scripts visit: www.gcreddy.com * Add commands to load data to screen * Initialize timing variables as in "Capture Timings" example * Submit the "click" to submit from browser to web server * Update timing variable as in "Capture Timings" example ************************** =end if acceptableScreen1 < actualTime resultValue = "NotOK" end #Log results timeSpreadsheet.puts executionEnvironment + ",Expected screen 1 time," + acceptableScreen1.to_s + ",Actual screen 1 time," + (endTime - beginTime).to_s + "," + resultValue =begin ************************** * Repeat this 3 times, once for each of the other 3 screens * Remember to reinitialize resultValue, endTime, and beginTime each time ************************** =end #Close the browser ie.close if ie #Close the workbook timeSpreadsheet.close Some points about this example. First, the commented parts depend upon the previous examples above. Second, an input file is read to extract test case information. The information should be written on 5 separate lines. The first line contains a reference to the environment in which this test is run, e.g., "dev", "test", "int_test", etc. This first line is loaded into the variable executionEnvironment. The line feed must be chopped off the end of the string, otherwise when written to Excel this will be on its own line, and the other data would go to the next line—we want everything in the same row in the worksheet. Third, the acceptableScreen# variables hold a number—the expected maximum number of seconds for screen to screen response time. These numbers might all be the same, or different, depending on your test. The values are from lines 2 through 5 in the input file. Fourth and finally, the line above where the spreadsheet is written has a line break in it for formatting for this site, so if you copy the above you see that it is on two lines—you would want to put it all on one line. Write Output to Excel (as XLS) So the above example is very simple, and may be all that is needed, but if you want to format the speadsheet it really needs to be in Excel format. In which case, the opening of the file and the writing to the file needs to be a bit different. Try this modification: #open spreadsheet For Software Testing Information visit: www.gcreddy.net 5 For QTP Information & Scripts visit: www.gcreddy.com excel = WIN32OLE::new('excel.Application') workbook = excel.Workbooks.Add worksheet = workbook.Worksheets(1) worksheet.SaveAs("spreadsheet.xls") #Log results worksheet.range("a1").value = executionEnvironment worksheet.range("b1").value = "Acceptable Screen1 time" worksheet.range("c1").value = acceptableScreen1.to_s worksheet.range("d1").value = "Actual Screen1 time" worksheet.range("e1").value = actualScreen1.to_s worksheet.range("f1").value = resultValue # # Etcetera...assume the above happens 4 times, for 4 screens... # #Format workbook columns worksheet.range("b1:b4").Interior['ColorIndex'] = 36 #pale yellow worksheet.columns("b:b").AutoFit #close the workbook workbook.save workbook.close excel.Quit So this will permit you to not only write to Excel workbooks, but format the data as well. However here the writing is on a cell by cell basis rather than to an entire row as can be done in a CSV file. Recording Scripts There exists a very simple Ruby script called WatirMaker. See the External Link labeled "WatirMaker" to download it. Usage is very simple. Download it, remove the ".txt" suffix, then execute like: #Execute WatirMaker ruby WatirMaker.rb > c:\temp\rscript.rb That's it. Be aware that it will generate plenty of errors on any complex site. But some quick and judicious editing and you may find this little recorder to be very useful for you. Just don't expect it to compare to a commercial tool. It is merely a time saver to help you produce the framework for your script, which you can then polish up programmatically. WatirMarker recently was renamed WatirRecorder. It's easier to use, remaining useful for creating the framework of a testing script. WatirRecorder can't record all of what you do on your site, so many recorded scripts will require editing or further coding. WET Web Tester WET is a powertool for Watir. WET stands for Watir Extension Toolkit. Because the WET classes inherit from the Watir classes, WET provides all of the Watir functionality plus additional features, including powerful pop-up handling mechanisms. For Software Testing Information visit: www.gcreddy.net 6 For QTP Information & Scripts visit: www.gcreddy.com WatiN WatiN (pronounced What-in) is a similar tool as Watir for .NET. Tests can be written in C# (or any other .NET language like VB.NET, F# or IronPython) and are executed using Internet Explorer and Mozilla Firefox. Watij Watij (pronounced "wattage") stands for Web Application Testing in Java. Watij is a pure Java API created to allow for the automation of web applications. Based on the simplicity of Watir and enhanced by the power of Java, Watij automates functional testing of web applications through a real browser. Currently Watij supports automating Internet Explorer on Windows only. For QTP Information: www.gcreddy.com For Manual Testing: www.gcreddy.net For Software Testing Information visit: www.gcreddy.net 7
Copyright © 2024 DOKUMEN.SITE Inc.