.

watir send keys

<code> browser.send_keys("{ENTER}") </code>

To do the same thing in Watir-WebDriver, you do this:

<code> browser.send_keys :enter </code>

You can also do things like this:

<code>browser.element.send_keys [:control, 'a'], :backspace</code>

The full list of keys are available from here:

  • :null
  • :cancel
  • :help
  • :backspace
  • :tab
  • :clear
  • :return
  • :enter
  • :shift
  • :left_shift
  • :control
  • :left_control
  • :alt
  • :left_alt
  • :pause
  • :escape
  • :space
  • :page_up
  • :page_down
  • :end
  • :home
  • :left
  • :arrow_left
  • :up
  • :arrow_up
  • :right
  • :arrow_right
  • :down
  • :arrow_down
  • :insert
  • :delete
  • :semicolon
  • :equals
  • :numpad0
  • :numpad1
  • :numpad2
  • :numpad3
  • :numpad4
  • :numpad5
  • :numpad6
  • :numpad7
  • :numpad8
  • :numpad9
  • :multiply
  • :add
  • :separator
  • :subtract
  • :decimal
  • :divide
  • :f1
  • :f2
  • :f3
  • :f4
  • :f5
  • :f6
  • :f7
  • :f8
  • :f9
  • :f10
  • :f11
  • :f12
  • :meta
  • :command

might wanna look into this – watir stuff

Want to use watir to achieve an operation, open a Web page, the confirmation page checkbox is not checked, that is checked state.

Search revealed that the watir of the checkbox control is only set and clear operations operations

1. Using the name attribute set to check
ie.checkbox (: name, “checkme”). set
2. Use the name attribute check box cleared
ie.checkbox (: name, “checkme”). clear
3. Use the check box name and value property
ie.checkbox (: name, “checkme”, “1″). set
4. Use the name and value attributes check box cleared
ie.checkbox (: name, “checkme”, “1″). clear

There is no direct way to achieve, multi-query, get ideas from here ( http://blog.chinaunix.net/u/15010/showart_2058597.html ),

Script can get innerHTML, and then if there are keyword “<INPUT type = checkbox CHECKED” can confirm that the state is not checked, generally the code is as follows:

#———————–

require ‘watir’
$ HIDE_IE = true
ie = Watir:: IE.new

ie.goto (” http://localhost:8080/ “)

scripts = ie.document.body.parentElement.getElementsByTagName (“td”)
scripts.each do | script_tag |
script_tag.invoke (“innerHTML”). each do | script |
puts script
if script.match (/ <INPUT type = checkbox CHECKED /)
puts ‘status is checked’
end
end
end

#————————

Another more complex example, when there are multiple checkbox, requires recognizing checkbox1 previous name, found that match well with, we use the index, as follows:

#————————-

$ Name = ‘color’
keyword = “<INPUT type = checkbox CHECKED”
num_checked = 0
flag = false
scripts = $ ie.document.getElementById (“name_iframe”). contentWindow.document.body.getElementsByTagName (“tr”)
scripts.each do | script_tag |
script2 = script_tag.invoke (“innerHTML”)
if script2.index (keyword) and script2.match (<\ / TABLE >/)== nil
print_message (script2)
num_checked = num_checked +1
if script2.index ($ name)
flag = true
break
end
end
end
#————————–

Note:

index () which can be variable
match () which can not be a variable

a = ‘ddddddsdoddsfsf’
b = ‘s’
puts a.match (/ do /)
puts a.index (b)

gets the radio group selected value – watir

def sel_list_opt(sel_list)

arr=sel_list.getAllContents.to_a
d=arr.length.to_i
i=0
while 0 .. d
if sel_list.selected?(arr[i].to_s) == true
return arr[i].to_s
else
i=i+1
end
end
end

ruby test cases

That’s how it’s supposed to work!

Each test should be completely isolated from the rest, so the setup and tear_down methods are executed once for every test-case. There are cases, however, when you might want more control over the execution flow. Then you can group the test-cases in suites.

In your case you could write something like the following:

<code><span class="pln">require </span><span class="str">'test/unit'</span><span class="pln"><br />require </span><span class="str">'test/unit/ui/console/testrunner'</span><span class="pln"><br /><br /></span><span class="kwd">class</span><span class="pln"> </span><span class="typ">TestDecorator</span><span class="pln"> </span><span class="pun">&lt;</span><span class="pln"> </span><span class="typ">Test</span><span class="pun">::</span><span class="typ">Unit</span><span class="pun">::</span><span class="typ">TestSuite</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> initialize</span><span class="pun">(</span><span class="pln">test_case_class</span><span class="pun">)</span><span class="pln"><br />&nbsp; &nbsp; </span><span class="kwd">super</span><span class="pln"><br />&nbsp; &nbsp; </span><span class="kwd">self</span><span class="pln"> </span><span class="pun">&lt;&lt;</span><span class="pln"> test_case_class</span><span class="pun">.</span><span class="pln">suite<br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> run</span><span class="pun">(</span><span class="pln">result</span><span class="pun">,</span><span class="pln"> </span><span class="pun">&amp;</span><span class="pln">progress_block</span><span class="pun">)</span><span class="pln"><br />&nbsp; &nbsp; setup_suite<br />&nbsp; &nbsp; </span><span class="kwd">begin</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; </span><span class="kwd">super</span><span class="pun">(</span><span class="pln">result</span><span class="pun">,</span><span class="pln"> </span><span class="pun">&amp;</span><span class="pln">progress_block</span><span class="pun">)</span><span class="pln"> &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; </span><span class="kwd">ensure</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; tear_down_suite<br />&nbsp; &nbsp; </span><span class="kwd">end</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">class</span><span class="pln"> </span><span class="typ">MyTestCase</span><span class="pln"> </span><span class="pun">&lt;</span><span class="pln"> </span><span class="typ">Test</span><span class="pun">::</span><span class="typ">Unit</span><span class="pun">::</span><span class="typ">TestCase</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> test_1<br />&nbsp; &nbsp; puts </span><span class="str">"test_1"</span><span class="pln"><br />&nbsp; &nbsp; assert_equal</span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span class="lit">1</span><span class="pun">)</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> test_2<br />&nbsp; &nbsp; puts </span><span class="str">"test_2"</span><span class="pln"><br />&nbsp; &nbsp; assert_equal</span><span class="pun">(</span><span class="lit">2</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span class="pun">)</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">class</span><span class="pln"> </span><span class="typ">MySuite</span><span class="pln"> </span><span class="pun">&lt;</span><span class="pln"> </span><span class="typ">TestDecorator</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> setup_suite<br />&nbsp; &nbsp; puts </span><span class="str">"setup_suite"</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> tear_down_suite<br />&nbsp; &nbsp; puts </span><span class="str">"tear_down_suite"</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="typ">Test</span><span class="pun">::</span><span class="typ">Unit</span><span class="pun">::</span><span class="pln">UI</span><span class="pun">::</span><span class="typ">Console</span><span class="pun">::</span><span class="typ">TestRunner</span><span class="pun">.</span><span class="pln">run</span><span class="pun">(</span><span class="typ">MySuite</span><span class="pun">.</span><span class="pln">new</span><span class="pun">(</span><span class="typ">MyTestCase</span><span class="pun">))</span><span class="pln"><br /></span></code>

The TestDecorator defines a special suite which provides a setup and tear_down method which run only once before and after the running of the set of test-cases it contains.

The drawback of this is that you need to tell Test::Unit how to run the tests in the unit. In the event your unit contains many test-cases and you need a decorator for only one of them you’ll need something like this:

<code><span class="pln">require </span><span class="str">'test/unit'</span><span class="pln"><br />require </span><span class="str">'test/unit/ui/console/testrunner'</span><span class="pln"><br /><br /></span><span class="kwd">class</span><span class="pln"> </span><span class="typ">TestDecorator</span><span class="pln"> </span><span class="pun">&lt;</span><span class="pln"> </span><span class="typ">Test</span><span class="pun">::</span><span class="typ">Unit</span><span class="pun">::</span><span class="typ">TestSuite</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> initialize</span><span class="pun">(</span><span class="pln">test_case_class</span><span class="pun">)</span><span class="pln"><br />&nbsp; &nbsp; </span><span class="kwd">super</span><span class="pln"><br />&nbsp; &nbsp; </span><span class="kwd">self</span><span class="pln"> </span><span class="pun">&lt;&lt;</span><span class="pln"> test_case_class</span><span class="pun">.</span><span class="pln">suite<br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> run</span><span class="pun">(</span><span class="pln">result</span><span class="pun">,</span><span class="pln"> </span><span class="pun">&amp;</span><span class="pln">progress_block</span><span class="pun">)</span><span class="pln"><br />&nbsp; &nbsp; setup_suite<br />&nbsp; &nbsp; </span><span class="kwd">begin</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; </span><span class="kwd">super</span><span class="pun">(</span><span class="pln">result</span><span class="pun">,</span><span class="pln"> </span><span class="pun">&amp;</span><span class="pln">progress_block</span><span class="pun">)</span><span class="pln"> &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; </span><span class="kwd">ensure</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; tear_down_suite<br />&nbsp; &nbsp; </span><span class="kwd">end</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">class</span><span class="pln"> </span><span class="typ">MyTestCase</span><span class="pln"> </span><span class="pun">&lt;</span><span class="pln"> </span><span class="typ">Test</span><span class="pun">::</span><span class="typ">Unit</span><span class="pun">::</span><span class="typ">TestCase</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> test_1<br />&nbsp; &nbsp; puts </span><span class="str">"test_1"</span><span class="pln"><br />&nbsp; &nbsp; assert_equal</span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span class="lit">1</span><span class="pun">)</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> test_2<br />&nbsp; &nbsp; puts </span><span class="str">"test_2"</span><span class="pln"><br />&nbsp; &nbsp; assert_equal</span><span class="pun">(</span><span class="lit">2</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span class="pun">)</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">class</span><span class="pln"> </span><span class="typ">MySuite</span><span class="pln"> </span><span class="pun">&lt;</span><span class="pln"> </span><span class="typ">TestDecorator</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> setup_suite<br />&nbsp; &nbsp; puts </span><span class="str">"setup_suite"</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> tear_down_suite<br />&nbsp; &nbsp; puts </span><span class="str">"tear_down_suite"</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">class</span><span class="pln"> </span><span class="typ">AnotherTestCase</span><span class="pln"> </span><span class="pun">&lt;</span><span class="pln"> </span><span class="typ">Test</span><span class="pun">::</span><span class="typ">Unit</span><span class="pun">::</span><span class="typ">TestCase</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> test_a<br />&nbsp; &nbsp; puts </span><span class="str">"test_a"</span><span class="pln"><br />&nbsp; &nbsp; assert_equal</span><span class="pun">(</span><span class="str">"a"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"a"</span><span class="pun">)</span><span class="pln"><br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">class</span><span class="pln"> </span><span class="typ">Tests</span><span class="pln"><br /><br />&nbsp; </span><span class="kwd">def</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">suite<br />&nbsp; &nbsp; suite </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Test</span><span class="pun">::</span><span class="typ">Unit</span><span class="pun">::</span><span class="typ">TestSuite</span><span class="pun">.</span><span class="pln">new<br />&nbsp; &nbsp; suite </span><span class="pun">&lt;&lt;</span><span class="pln"> </span><span class="typ">MySuite</span><span class="pun">.</span><span class="pln">new</span><span class="pun">(</span><span class="typ">MyTestCase</span><span class="pun">)</span><span class="pln"><br />&nbsp; &nbsp; suite </span><span class="pun">&lt;&lt;</span><span class="pln"> </span><span class="typ">AnotherTestCase</span><span class="pun">.</span><span class="pln">suite<br />&nbsp; &nbsp; suite<br />&nbsp; </span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="kwd">end</span><span class="pln"><br /><br /></span><span class="typ">Test</span><span class="pun">::</span><span class="typ">Unit</span><span class="pun">::</span><span class="pln">UI</span><span class="pun">::</span><span class="typ">Console</span><span class="pun">::</span><span class="typ">TestRunner</span><span class="pun">.</span><span class="pln">run</span><span class="pun">(</span><span class="typ">Tests</span><span class="pun">.</span><span class="pln">suite</span><span class="pun">)</span><span class="pln"><br /></span></code>

The Test::Unit documentation documentation provides a good explanation on how suites work.

Watir Cheat sheet

 

ie = Watir::Browser.new
ie.goto(‘http://www.bitlinkit.com‘)

if ie.h1(:text, ‘System Management’).exists?
 puts ‘it exists’
end

assert(ie.text.include?(“hello world”))

ie.speed = :fast

  

checks if a radio button is set:

xx=@b.radio(:id, /ctl00_MainContent_ClinicDataEntry1_YesNoFirstAttendance_0/ )
puts (‘isSet?’)
puts (xx.isSet?)
puts (‘checked?’)
puts (xx.checked?)
puts (‘getState’)
puts (xx.getState)
#they all give the same result
# other objects: .class_name .exists? .enabled?

———————

text_field:
.class_name (not sure if exists)
.exists? .enabled? .value .verify_contains(“Hello World”) .readonly? .append(“Some Text”) .set(“pass”) .id .name .disabled .type .title .maxlength

assert_raises(ObjectReadOnlyException) { browser.text_field(:id, “readOnly2″).append(“Some Text”) } 
    assert_raises(ObjectDisabledException) { browser.text_field(:name, “disabled”).append(“Some Text”) } 
    assert_raises(UnknownObjectException) { browser.text_field(:name, “missing_field”).append(“Some Text”) } 

 

———————-

browser.button(:value, “Clear Events Box”).click

————————

select_list (dropdowns)

    assert_equal(['Option 3'], browser.select_list(:name, “sel1″).getSelectedItems)
    browser.select_list(:name, “sel1″).set(‘Option 2′)
    assert_equal(['Option 2'], browser.select_list(:name, “sel1″).getSelectedItems)

browser.select_list(:name, “sel1″).option(:text, /missing/).select
browser.select_list(:name, “sel3″).select_value(/2/)

.clearSelection .class_name .select .selected

assert browser.select_list(:name, ‘sel1′).includes?(‘Option 1′)
assert browser.select_list(:name, ‘sel1′).selected?(/option/i)
assert(@b.select_list(:id, “ctl00_MainContent_ClinicDataEntry1_List_Antibiotics_Gonorrohoea_AntibioticsDropDown1″).includes?(“Cefoxitin”))
@b.select_list(:id, “ctl00_MainContent_ClinicDataEntry1_List_Countries1_CountriesDropDown1″).set(“Bolivia”)

————————

browser.checkbox(:name, “noName”).isSet?
.clear .getState .set .locate .flash

————————

talbes:

assert(@b.table(:id, ‘ctl00_MainContent_ClinicDataEntry1_List_Antibiotics_Gonorrohoea_GridView1′).text.include?(‘Cefoxitin’))

 

————————

images:
@b.image(:id, “ctl00_MainContent_ClinicDataEntry1_List_Antibiotics_Gonorrohoea_ImageButtonCreate”).click

————————

.innerText -?

assert(browser.text.include?(“PASS”))

 

b.select_list(:id => "entry_6").option(:index, 2).select
puts b.select_list(:id => "entry_6").selected_options #Internet Explorer
puts b.select_list(:id => "entry_6").value #Internet Explorer

————————

block comments:

=begin
 this is a block comment
 make sure the next line is completely left indented !!

=end

————————

 

——————————————

Assertions

assert
assert_block
assert_equal
assert_in_delta
assert_instance_of
assert_kind_of
assert_match
assert_nil
assert_no_match
assert_not_equal
assert_not_nil
assert_not_same
assert_nothing_raised
assert_nothing_thrown
assert_operator
assert_raise
assert_respond_to
assert_same
assert_send
assert_throws
—————————-

Ways Available To Identify HTML Element:
http://wiki.openqa.org/display/WTR/Ways+Available+To+Identify+HTML+Element

references:
basic tutorial
unit testing
cheatsheet
tutorials
examples
ruby-watir-API
watir-containers
watir-methods support

automated testing:
selenium

cubic test

supported methods
ook supported stuff

source code of test scripts:
C:\Ruby192\lib\ruby\gems\1.9.1\gems\watir-2.0.2\unittests

 

installing cubic test:
http://www.cubictest.org/installDetails.html
get java 5 – so i got

Java SE Development Kit 5.0u22
from http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-javase5-419410.html#jdk-1.5.0_22-oth-JPR

i downloaded:

Windows Offline Installation 53.20 MB jdk-1_5_0_22-windows-i586-p.exe

and Eclipse IDE for Java Developers

http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/indigo/SR1/eclipse-java-indigo-SR1-win32-x86_64.zip

link: eclipse-java-indigo-SR1-win32-x86_64.zip

watir for .net: http://watin.org/

no such file to load — watir/ie (LoadError)

getting the following ruby error?

no such file to load — watir/ie (LoadError)

solve as follows:

gem update --system
gem install watir

then try running your ruby file again.

 

watir file upload tutorial

The problem when trying to run the following Ruby test script under Watir:
(It should simply open a webpage and select a file for upload)

require 'watir'
require 'test/unit'
require 'watir/windowhelper'

b = Watir::Browser.new
b.goto("http://localhost:65519/data/UploadHIVData.aspx")
b.file_field(:name, "ctl00$MainContent$FileUpload1").set("c:\\test\\here.txt")
assert_equal "c:\\test\\here.txt", b.file_field(:name,"ctl00$MainContent$FileUpload1").value
exit

But it fails with the following error:

>ruby GRASP_Upload_File.rb
C:/Program Files/Ruby/lib/ruby/gems/1.8/gems/watir-1.6.6/lib/watir/windowhelper.rb:44:in `check_autoit_installed’: The AutoIt dll must be correctly registered for this feature to work properly (Watir::Exception::WatirException)
from C:/Program Files/Ruby/lib/ruby/gems/1.8/gems/watir-1.6.6/lib/watir/input_elements.rb:492:in `set’
from GRASP_Upload_File.rb:16
>Exit code: 1

Here is my Solution (tested and working):

gem update
gem install autoit

rem – goto your installation folder e.g.
cd C:\Program Files\Ruby\lib\ruby\gems\1.8\gems\watir-1.6.6\lib\watir

regsvr32 AutoItX3.dll

Then I receive the following confirmation message:

DllRegisterServer in AutoItX3.dll succeeded.

clicked ok and it all works after that.