Aug
23
2008
0

Q. How do I enter the current date or time into a textfield?

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%"))
Written by Sameh Abdelhamid in: watir | Tags: , ,
Aug
21
2008
0

Q. How do I get the text displayed in a javascript popup?

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.

Written by Sameh Abdelhamid in: watir | Tags: , , ,
Aug
21
2008
0

Q. How do I attach to a current IE session?

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.

Written by Sameh Abdelhamid in: watir | Tags: ,
Aug
21
2008
2

Q. How do I deal with javascript popup’s?

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.

Written by Sameh Abdelhamid in: watir | Tags: , , ,
Aug
21
2008
0

How do I get the URL in IE?

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.

Written by Sameh Abdelhamid in: watir | Tags: , ,
Aug
14
2008
0

Q. How do I set a checkbox with a dynamic id attribute?

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
Written by Tim Koopmans in: watir | Tags: , , ,
Aug
12
2008
0

Q. How do I get to element X in frame Y?

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.

Written by Tim Koopmans in: watir | Tags:
Aug
12
2008
1

Q. How can extract information from script headers?

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
Written by Tim Koopmans in: watir | Tags:
Aug
12
2008
1

Q. How do I simulate a user pressing other keys?

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}")
Written by Tim Koopmans in: watir | Tags:
Aug
12
2008
0

Q. How do I simulate a user pressing ‘Enter’?

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}")
Written by Tim Koopmans in: watir | Tags:
Aug
07
2008
0

Q. Which method do I use with which element?

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
Written by Sameh Abdelhamid in: watir | Tags: ,
Aug
07
2008
0

Q. How do I click a radio button?

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
Written by Sameh Abdelhamid in: watir | Tags: ,
Aug
07
2008
0

Q. How do I check/uncheck a checkbox?

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)
Written by Sameh Abdelhamid in: watir | Tags: ,
Aug
07
2008
0

Q. How do I enter text in a text box?

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!")
Written by Sameh Abdelhamid in: watir | Tags: ,

Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes