Q. How do I integrate watir with TestLink?
A. This requires several hacks and use of the developer API …
(more…)
A. This requires several hacks and use of the developer API …
(more…)
A previous post highlights some ways in which you can install watir from behind a proxy server via the command line.
This approach can be problematic when using authenticated proxy servers. The following file is a simple MSI which will install Watir 1.5.6 with its required dependencies. The pre-requisites for this MSI are:
1. Ruby 1.8.6 installed to c:\ruby
2. Windows XP SP2 (I have not tried this on Windows Vista)
Note: this is not the preferred way of installing Watir and as such I cannot offer any guarantees on its success.
A. Use a ci_reporter gem …
To install the ci_reporter gem on windows:
gem install ci_reporter
If you’re using Test::Unit, ensure the ci/reporter/rake/test_unit_loader.rb file is loaded before the test is run. If you’re using RSpec, you‘ll need to pass the following arguments to the spec command:
--require GEM_PATH/lib/ci/reporter/rake/rspec_loader --format CI::Reporter::RSpec
You may also want to set the output directory as demonstrated by setting the CI_REPORTS environment variable.
require 'test/unit' require 'ci/reporter/rake/test_unit_loader.rb' require 'watir' ENV["CI_REPORTS"] = 'C:/temp/'
A. Use the — argument to stop processing Test::Unit specific arguments …
Module Test::Unit has its own command line arguments as specified by the following:
~/just_add_watir $>ruby test_suite.rb --help
Test::Unit automatic runner.
Usage: test/unit/graph_test.rb [options] [-- untouched arguments]
-r, --runner=RUNNER Use the given RUNNER.
(c[onsole], f[ox], g[tk], g[tk]2, t[k])
-n, --name=NAME Runs tests matching NAME.
(patterns may be used).
-t, --testcase=TESTCASE Runs tests in TestCases matching TESTCASE.
(patterns may be used).
-v, --verbose=[LEVEL] Set the output level (default is verbose).
(s[ilent], p[rogress], n[ormal], v[erbose])
-- Stop processing options so that the
remaining options will be passed to the
test.
-h, --help Display this help.
Deprecated options:
--console Console runner (use --runner).
--gtk GTK runner (use --runner).
--fox Fox runner (use --runner).Example watir:
def test_0021 # need to call test unit with -- argument puts "argument 1 is: ",ARGV[0] end
A. Use the .goto method to call the javascript …
Example html:
<HTML lang="en-US" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US"> <BODY scroll="no"> <script type="text/javascript" charset="utf-8"> function openWin(i){ alert(i); } </script> <DIV id="menuLayer1"> <DIV id="menuLite1"> <DIV id="menuFg1"> <DIV id="menuItem1" mmaction="location='javascript:openWin(2);'" zIndex="1"> <DIV id="menuItemText1"> <DIV id="menuItemShim1"> <DIV align="left"> just_add_watir </DIV> </DIV> </DIV> </DIV> </DIV> </DIV> </DIV> </BODY> </HTML>
Example watir:
@b.goto('http://justaddwatir.com/watir/test_html/tc_0001_0100/test_0020.html') @b.div(:id, "menuItem1").flash @b.goto("javascript:openWin(2)")
A. Use the Time.now method …
Example html:
<html> <body> <form method="post" action=""> <textarea name="comments" cols="40" rows="1"> Enter your comments here... </textarea><br> <input type="submit" value="Submit" /> </form> </body> </html>
Example watir:
t=Time.now @b.text_field(:name,"comments").set(t.strftime("%m/%d/%Y_%H%:M%"))
A. Use the WIN32OLE extension library with the autoit function library …
You will need to install autoit. You can download it here, once installed you can call the function library using the win32OLE extension library.
Example html:
<html> <head> <script language="javascript"> function msgbox (textstring) { alert (textstring) } </script> </head> <body id="test_00test_0107" onload=""> <form> <input name="text1" type=text> <input name="submit" type=button value="show me" onclick="msgbox(form.text1.value)"> </form> </body> </html>
Example watir:
require 'watir' require 'rubygems' require 'win32ole' autoit = WIN32OLE.new('AutoItX3.Control') @b.goto('http://justaddwatir.com/watir/test_html/tc_0101_0200/test_0107.html') @b.text_field(:name,"text1").set("This is the text in the popup") @b.button(:name,"submit").click_no_wait autoit.WinWaitActive("[Class:#32770]") text = autoit.ControlGetText("[Class:#32770]", "", "Static2") autoit.ControlClick("[Class:#32770]","","Button1") puts text
In order for the autoit function to execute, you need to use the .click_no_wait method in watir before the autoit function. This is because once the pop up is presented, the focus comes off the IE window and the script will pause. The .click_no_wait tells the script to continue running regardless of what happens next.
You may use the autoit window identifier to get the properties of the pop up the static text.
A. Use the .attach method…
Example watir:
@b=Watir::IE.attach(:title,//)
I have used regex here for the window title, you can be more specific if you like.
A. Invoke the autoit function library with the .click_no_wait method…
You will need to install autoit. You can download it here, once installed you can call the function library using the win32OLE extension library.
Example html:
<html> <head> <script language="javascript"> function msgbox (textstring) { alert (textstring) } </script> </head> <body id="test_00test_0107" onload=""> <form> <input name="text1" type=text> <input name="submit" type=button value="show me" onclick="msgbox(form.text1.value)"> </form> </body> </html>
Example watir:
require 'watir' require 'rubygems' require 'win32ole' autoit = WIN32OLE.new('AutoItX3.Control') @b.goto('http://justaddwatir.com/watir/test_html/tc_0101_0200/test_0107.html') @b.text_field(:name,"text1").set("Justaddwatir") @b.button(:name,"submit").click_no_wait autoit.WinWaitActive("[Class:#32770]") result =autoit.ControlClick("[Class:#32770]","","Button1") puts "successful click =1 unsuccessful =0, the result was "+ result
In order for the autoit function to execute, you need to use the .click_no_wait method in watir before the autoit function. This is because once the pop up is presented, the focus comes off the IE window and the script will pause. The .click_no_wait tells the script to continue running regardless of what happens next.
You may use the autoit window identifier to get the properties of the pop up and the button attributes.
A. Use the autoit function library with the win32OLE extension library…
You will need to install autoit. You can download it here, once installed you can call the function library using the win32OLE extension library.
require 'watir' require 'rubygems' require 'win32ole' autoit = WIN32OLE.new('AutoItX3.Control') url = autoit.ControlGetText("[CLASS:IEFrame]", "", "Edit1") puts "url is " + url
The defintion for the autoit function ‘ControlGetText’ is ControlGetText(title, text, controlId)
You can use the “AutoIt Window Info” tool to find the title and controlId, you do not need to use the ‘text’ parameter. I find that using the Class instead of the window title a neater and more accurate solution.
A. Use a regex match to populate a dynamic variable …
Example html:
<form action="test_0019.html" method="get" accept-charset="utf-8"> <input id="cblRoles_3" type="checkbox" tabindex="47" name="cblRoles:3"/> <label for="cblRoles_3">Customer</label> <input id="cblRoles_4" type="checkbox" tabindex="48" name="cblRoles:4"/> <label for="cblRoles_4">Expedite</label>
Example watir:
dynamic_id = @b.html[/cblRoles_\d+>Expedite/].gsub(">Expedite","") @b.checkbox(:id,dynamic_id).set
A. Make sure you prefix your element with the correct frame reference …
Example watir:
@b.goto('http://justaddwatir.com/watir/test_html/tc_0001_0100/test_0017.html') @b.frame(:index, 2).button(:name, "submit").click
Note: sometimes frames aren’t easily identified with id or name attributes, the example above uses the index attribute to get to the second frame in the frameset.
A. Use the getElementsByTagName method and iterate through results …
Example html:
<title>test_0016</title> <meta name="generator" content="TextMate http://macromates.com/"> <meta name="author" content="koops"> <!-- Date: 2008-08-12 --> <script type="text/javascript">var PAGE_TID ="cache;app05.qa.sfo1:8101;2008-08-07T18:38:30Z;0001"; </script>
Example watir:
@b.goto('http://justaddwatir.com/watir/test_html/tc_0001_0100/test_0016.html') s=[] scripts = @b.document.body.parentElement.getElementsByTagName("script") scripts.each do |script_tag| script_tag.invoke("innerHTML").each do |script| if script.match(/var PAGE_TID/) puts script end end end
A. Use the .send_keys method …
A complete list of send_keys combinations is available here.
Example watir:
@b.goto("http://www.google.com") @b.text_field(:name, 'q').set('foobar') @b.send_keys("{TAB}") @b.send_keys("{TAB}") @b.send_keys("+{TAB}") @b.send_keys("{SPACE}")
A. Use the send_keys method …
Example watir:
@b.goto("http://www.google.com") @b.text_field(:name, 'q').set('foobar') @b.send_keys("{ENTER}")
| button | <input> tags with type=button, submit, image or reset |
| radio | <input> tags with the type=radio; known as radio buttons |
| check_box | <input> tags with type=checkbox |
| text_field | <input> tags with the type=text (single-line), type=textarea (multi-line), and type=password |
| hidden | <input> tags with type=hidden |
| select_list | <select> tags, known as drop-downs or drop-down lists |
| label | <label> tags (including “for” attribute) |
| span | <span> tags |
| div | <div> tags |
| p | <p> (paragraph) tags |
| link | <a> (anchor) tags |
| table | <table> tags, including row and cell methods for accessing nested elements |
| image | <img> tags |
| form | <form> tags |
| frame | frames, including both the <frame> elements and the corresponding pages |
| map | <map> tags |
| area | <area> tags |
| li | <li> tags |
A. Use the .radio in conjunction with .set
Example html:
<html> <body> <input type="radio" name="group1" value="Watir"> Watir<br> </body> </html>
Example watir:
@b.radio(:name,"group1").set
A. Use a the .checkbox in conjunction with .set(true/false)
Example html:
<form> <input name="checkbox" type="checkbox" />I love just_add_watir ! </form>
Example watir to activate:
@b.checkbox(:name, "checkbox").set(true)
Example watir to deactivate:
@b.checkbox(:name, "checkbox").set(false)
A. Use the text_field in conjunction with the .set command
Example html:
<html> <body> <form method="post" action=""> <textarea name="comments" cols="20" rows="1"> Enter your comments here... </textarea><br> <input type="submit" value="Submit" /> </form> </body> </html>
Example watir:
@b.text_field(:name, "comments").set("just_add_watir rocks!")
A. Set an environment variable for your proxy from the command line …
set HTTP_PROXY=http://my.work.proxy:8080 gem install watir
A. Iterate over the directory using the File class …
Example watir:
dir = File.dirname(__FILE__) Dir[File.expand_path("#{dir}/*.rb")].uniq.each do |file| require file end
A. Use .close_others method …
Example watir:
@b.close_others
A. Use %x{} to run a sub process that calls tasklist.exe …
Example watir:
if %x[tasklist /FI "IMAGENAME eq IEXPLORE.EXE" /NH] =~ /IEXPLORE/ then puts "browser IS running" else puts "no browsers running" end
A. Use the select_list comand in conjunction with the getSelectedItems command
Example html:
<select name="watirCombo"> <option value="0" selected>(please select:)</option> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> <option value="other">Other</option> </select>
Example watir:
@b.goto('http://justaddwatir.com/watir/test_html/tc_0001_0100/test_0021.html') combo_items = @b.select_list(:name, "watirCombo").getSelectedItems puts combo_items
A. Use the document.scrollintoview command.
Example html:
<p> Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod incidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis ostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis utem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit raesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Epsum factorial non deposit quid pro quo hic escorol. Olypian quarrels et gorilla congolium sic ad nauseum. Souvlaki ignitus carborundum e pluribus unum. Defacto lingo est igpay atinlay. Marquee selectus non provisio incongruous feline nolo contendre. Gratuitous octopus niacin, sodium glutimate. Quote meon an estimate et non interruptus stadium. Sic tempus fugit esperanto hiccup estrogen. Glorious baklava ex librus hup hey ad infinitum. Non sequitur condominium facile et geranium incognito. Epsum factorial non deposit quid pro quo hic escorol. Marquee selectus non provisio incongruous feline nolo contendre Olypian quarrels et gorilla congolium sic ad nauseum. Souvlaki ignitus carborundum e pluribus unum. Li Europan lingues es membres del sam familie. Lor separat existentie es un myth. Por scientie, musica, sport etc, li tot Europa usa li sam vocabularium. Li lingues differe solmen in li grammatica, li pronunciation e li plu commun vocabules. Omnicos directe al desirabilita; de un nov lingua franca: on refusa continuar payar custosi traductores. It solmen va esser necessi far uniform grammatica, pronunciation e plu sommun paroles. Ma quande lingues coalesce, li grammatica del resultant lingue es plu simplic e regulari quam ti del coalescent lingues. Li nov lingua franca va esser plu simplic e regulari quam li existent Europan lingues. It va esser tam simplic quam Occidental: in fact, it va esser Occidental. A un Angleso it va semblar un simplificat Angles, quam un skeptic Cambridge amico dit me que Occidental es. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Epsum factorial non deposit quid pro quo hic escorol. Olypian quarrels et gorilla congolium sic ad nauseum. Souvlaki ignitus carborundum e pluribus unum. Defacto lingo est igpay atinlay. Marquee selectus non provisio incongruous feline nolo contendre. Gratuitous octopus niacin, sodium glutimate. Quote meon an estimate et non interruptus stadium. Sic tempus fugit esperanto hiccup estrogen. Glorious baklava ex librus hup hey ad infinitum. Non sequitur condominium facile et geranium incognito. Epsum factorial non deposit quid pro quo hic escorol. Marquee selectus non provisio incongruous feline nolo contendre Olypian quarrels et gorilla congolium sic ad nauseum. Souvlaki ignitus carborundum e pluribus unum. Li Europan lingues es membres del sam familie. Lor separat existentie es un myth. Por scientie, musica, sport etc, li tot Europa usa li sam vocabularium. Li lingues differe solmen in li grammatica, li pronunciation e li plu commun vocabules. Omnicos directe al desirabilita; de un nov lingua franca: on refusa continuar payar custosi traductores. It solmen va esser necessi far uniform grammatica, pronunciation e plu sommun paroles. Ma quande lingues coalesce, li grammatica del resultant lingue es plu simplic e regulari quam ti del coalescent lingues. Li nov lingua franc va esser plu simplic e regulari quam li existent Europan lingues. It va esser tam simplic quam Occidental: in fact, it va esser Occidental. A un Angleso it va semblar un simplificat Angles, quam un skeptic Cambridge amico dit me que Occidental es. </p> <a href="http://justaddwatir.com/">justaddwatir</a><br />
Example watir:
@b.goto('http://justaddwatir.com/watir/test_html/tc_0001_0100/test_0020.html') @b.link(:text, 'justaddwatir').document.scrollintoview
In this example we are scrolling down to the link which is displayed as “justaddwatir”.
A. Use the Timeout class …
Example watir:
Timeout::timeout(10)do begin @b.goto(url) rescue Timeout::Error puts 'Page took longer than 10 seconds to load' end end
A. the /i indicates an ignore case modifier …
In Ruby, a regular expression is written in the form of /pattern/modifiers where “pattern” is the regular expression itself, and “modifiers” are a series of characters indicating various options.
Ruby supports the following modifiers:
* /i makes the regex match case insensitive.
* /m makes the dot match newlines. Ruby indeed uses /m, whereas Perl and
many other programming languages use /s for “dot matches newlines”.
* /x tells Ruby to ignore whitespace between regex tokens.
* /o causes any #{…} substitutions in a particular regex literal to be performed just once, the first time it is evaluated. Otherwise, the substitutions will be performed every time the literal generates a Regexp object.
You can combine multiple modifiers by stringing them together as in /regex/is.
See more information here: http://www.regular-expressions.info/ruby.html
A. Use the .innerText method …
Example html:
<div class="UserServiceValues"> Apples </div> <div class="UserServiceValues"> Oranges </div> <div class="UserServiceValues"> Bananas </div>
Example watir:
@b.elements_by_xpath("//div[@class='UserServiceValues']").each do |elem| puts elem.innerText end
A. Get a window handle and close the pid using the Win32API …
Example watir:
hWnd ||= Watir::IE::Process.process_id_from_hwnd @b.hwnd pid=" " * 32 thread=Win32API.new("user32", "GetWindowThreadProcessId", 'IP', 'I').Call(hWnd,pid) puts pid.unpack("L")[0] right_to_terminate_process = 1 handle = Win32API.new('kernel32.dll', 'OpenProcess', 'lil', 'l'). call(right_to_terminate_process, 0, hWnd) Win32API.new('kernel32.dll', 'TerminateProcess', 'll', 'l').call(handle, 0)
A. The Watir::IE class has a close all method …
Example watir:
Watir::IE.close_all()
Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes