CodeWeblog.com » inheritance,ruby,current user » ruby watir organize

ruby watir organize

5. Abnormal object recognition (Watir application solutions)

Watir-based treatment, inheritance in web document object to provide better support, but vis-à-vis the weaker object handle windows. on the current from the website to collect the object of view, basically, for example in the IE object objects open web standards to identify and be able to successfully operate Other's from the current IE window open all the pop-up box can not be a normal identification. Among them, the pop-up box unusual object occupies a large part of the object recognition.
Currently encountered by the pop-up box, I put them into three types:
1, Alerts warning role, such as: sorry, the current user does not have permission to operate
2, Confirm button required to operate, such as: Do you confirm that you want to delete the current record?
3, Prompt + select + confirm the need for user input, operation query or click on the final confirmation, such as: Download / upload (browser + select files)

Give below the top of several pop-up box watir achieve recognition and operation methods,
5.1. Pop-up box autoIT + thread-based implementation approach

Such an approach, for first, two kinds of pop-up box to operate more effectively,
By clicking on a link / button, etc., while the pop-up window (most of the pop-up box can be used to achieve this way.)

1, the definition of Ways

def check_for_popups
autoit = WIN32OLE.new ( 'AutoItX3.Control')
#

# Do forever - assumes popups could occur anywhere / anytime in your application.
loop do
# Look for window with given title. Give up after 1 second.

ret = autoit.WinWait ( 'message - the page the dialog box','', 1)
# ret = WinActivate ( "Microsoft Internet Explorer", "")
# autoit.ControlClick ( "Microsoft Internet Explorer", "", "[CLASS: Button; INSTANCE: 1]", 2)

puts (ret)
#
# If window found, send appropriate keystroke (eg (enter), (Y), (N)).

if (ret == 1) then autoit.Send ( "(Enter)") end

#
# Take a rest to avoid chewing up cycles and give another thread a go.
# Then resume the loop.
sleep (3)
end
end

2, the procedure body code

ie.button (: name, "signin"). click_no_wait

sleep (20)

$ popup = Thread.new (check_for_popups) # start popup handler

at_exit (Thread.kill ($ popup))



5.2. Call another ruby file

For the second pop-up box, like the security warning click, not through the operation and click on any object, that is, a pop-up window.
I try to use 3.1 methods to achieve, unsuccessful. Ways to use the following alternatives:

1, in the watir / WindowHelper.rb document Ways to increase

def push_security_alert_yes_cn
@ autoit.WinWait "Security Alert", ""
@ autoit.Send "(TAB)"
@ autoit.Send "(TAB)"
@ autoit.Send "(SPACE)"
End

2, the definition of another document called tmp.rb
require 'watir / WindowHelper'
helper = WindowHelper.new
helper.push_security_alert_cn_yes

3, in the open before the safety of URL, start calling ruby file

require 'rubygems'
require 'watir' # the watir controller
require 'win32ole'
require 'watir / WindowHelper'

Thread.new (system ( 'ruby c: \ tmp.rb')) # you define the path of tmp files
ie = Watir:: IE.new
ie.goto ( "http://www.alipay.com.cn/
")
5.3. To amend the framework of the bottom

Such an approach, in view of the pop-up box 3.

For one, upload download document
1, modify the underlying code input_elements.rb documents,
FileField category, the methods set.
Support for English, there is a replacement amended to read:

system ( "rubyw-e \" require 'win32ole'; @ autoit = WIN32OLE.new ( 'AutoItX3.Control'); waitresult = @ autoit.WinWait 'Choose file','', 15; sleep 1; if waitresult == 1 \ "-e \" @ autoit.ControlSetText 'Choose file','', 'Edit1', '# (setPath)'; @ autoit. ControlSend 'Choose file','', 'Button2', '(ENTER)'; \ "-e \" end \ "")

2, the definition of Ways
def save_file (filepath)
ai = WIN32OLE.new ( "AutoItX3.Control")
ai.WinWait ( "File Download", "", 5)
ai.ControlFocus ( "File Download", "", "save (& S)")
sleep 1
ai.ControlClick ( "File Download", "", "save (& S)", "left")
ai.WinWait ( "Save As", "", 5)
sleep 1
ai.ControlSend ( "Save As", "", "Edit1", filepath)
ai.ControlClick ( "Save As", "", "save (& S)", "left")
ai.WinWait ( "download completed", "", 5)
ai.ControlClick ( "download completed", "", "turn off")
end

3, procedure body code:
ie.file_field (: name, "xlsfile"). set ( "c: \ \ samenameobject.html")
# Upload the file you specified

Or
ie.span (: text, "Export Excel Table"). click_no_wait
save_file ( "C: \ \ abc.xls")
# Download to your specified path



3 for the pop-up box, the query and user intervention required to operate:
For two, in the Medium Category for crm and customer selection of the pop-up box, with the first implementation in different ways is that
Medium WinActivate used autoit and ControlClick method

require 'rubygems'
require 'watir' # the watir controller
require 'watir / WindowHelper'

require 'win32ole'

def check_for_popups

autoit = WIN32OLE.new ( 'AutoItX3.Control')

loop do
ret = autoit.Winwait ( "select - page dialog", "", 1)
puts ret
if (ret == 1) then
autoit.WinActivate ( "[CLASS: Internet Explorer_TridentDlgFrame]", "")
autoit.Send ( "(TAB)")
autoit.Send ( "(TAB)")

# autoit.Send ( "Huzhou")
# autoit.Send ( "(ASC 2709)")
# autoit.SendUnicode ( "a")
# autoit.ClipPut ( "hang")
# autoit.ClipGet
# autoit.ToolTip "This is a tooltip Hangzhou", 0, 0
# autoit.Sleep 2000

autoit.Send ( "(TAB)")
autoit.Send ( "(TAB)")
autoit.Send ( "(Enter)")

autoit.WinActivate ( "[CLASS: Internet Explorer_TridentDlgFrame]", "")
autoit.ControlClick ( "select - page dialog", "", "[CLASS: Internet Explorer_TridentLstBox; INSTANCE: 2]", 2)

autoit.Send ( "(TAB)")
autoit.Send ( "(Enter)")

autoit.Send ( "(TAB)")
autoit.Send ( "(TAB)")
autoit.Send ( "(TAB)")
autoit.Send ( "(TAB)")
autoit.Send ( "(Enter)")
end
sleep (3)
end

end

ie = Watir:: IE.new
ie.goto ( " http://10.2.6.4:5100/bin/member/signin ")
# ie.goto ( " http://10.2.6.4:5100/ ")

# ie.element_by_xpath ( "/ / span [contains (text (), 'Main Road Nijmegen')]"). click

ie.text_field (: name, "id"). set ( "mhl1805")
ie.text_field (: name, "password"). set ( "zxcvbnm")

ie.button (: name, "signin"). click

ie.frame (: name, "left"). link (: url, " http://10.2.6.4:5100/v3/help_cust.htm "). click

ie.frame (: name, "left"). link (: text, "for new customers to sign in"). click

# Start popup handler

ie.frame (: name, "right"). frame (: name, "rtop"). button (: value, "Customer Choice"). click_no_wait
sleep (20)

$ popup = Thread.new (check_for_popups)

at_exit (Thread.kill ($ popup))

For the third popup window, if required with user interaction, and the need to input Chinese characters, if treated with autoit sendkey Supported English (but official documentation says that it supports Chinese input, on-line there is a lot of same questions and I met), there is no to find an effective program, pending further follow-up.

In addition to the above pop-up box outside the required special treatment, watir also there is some expansion of tag, or third-party control of the required special treatment,
Like:
5.4. Web HTML editor html editor of the English Station provides: operator ID, or name more convenient to operate directly

require 'rubygems'
require 'watir' # the watir controller
require 'watir / WindowHelper'
require 'win32ole'
ie = Watir:: IE.new

ie = Watir:: IE.attach (: title, / Ali aides /)

ie.text_field (: name, "_fmo.a._0.s"). set ( "mhl1805")
ie.document.getElementByid ( "_editor"). contentWindow.focus ()
ie.send_keys ( "abcsadfasfd")

But also encountered some web pages, does not provide any ID, can only be used to send tab Ways autoIT implementation

require 'rubygems'
require 'watir' # the watir controller
require 'watir / WindowHelper'
require 'win32ole'



ie = Watir:: IE.new

# ie = Watir:: IE.attach (: title, / Alibaba /)

ie.goto ( 'http://us.my.alibaba.com/product/buyoffer/post_buying_lead.htm')

ie.text_field (: name, "_fmp.bu._0.su"). set ( "mhl1805")
ie.text_field (: name, "_fmp.bu._0.su"). set ( "China")

ie.checkbox (: id, "detailDesc"). set
ie.checkbox (: id, "detailDesc"). focus ()
ie.send_keys ( "\ t" * 9)
ie.send_keys ( 'Hello Worldabcdef')

5.5. Object identification other methods commonly used built-TAG quote

Such as: IE.div, ie.span, ie.cell, ie.table way to achieve click operation, value operation.
In addition the provision of QTP descriptive programming similar, with the type of object statistics:

ie.buttons.each (| b | puts b.to_s)
puts ie.checkboxes.length
puts ie.links.length
and so on puts ie.buttons.length

For commonly used InnerText property, has been at watir package to methods can be directly quoted. Such as:
ruby at the bottom of the object to identify, package the InnerText
Implementation, called Ways text can be:
Such as: puts ie.div (: id, "intelligentWord"). Link (: id, index.to_s). Text

Finally: return text and source code, use the following methods:
puts ie.text ()
puts ie.html ()

5.6. Identification of the target regular expression support
Of course, ruby to provide powerful support for regular expressions, such as: property identification is then
ie = Watir:: IE.attach (: title, / Alibaba /)

Ruby's regular expressions with "/ /" as the basic framework for the content of expression is located in "/" between. Expressions return a RegExp object.
Expression of the general rules:
/ a / matches the characters a.
/ \? / Matching special characters?. Special characters, including ^, $,?,., /, \, [,], (,), (,), +, *.
. Matches any character, such as / a. / matches ab and ac.
/ [ab] c / matching ac and bc, [] between the scope of representation. For example: / [az] /, / [a-zA-Z0-9] /.
/ [^ a-zA-Z0-9] / matches are not within the scope of the string.
/ [\ d] / on behalf of any figure, / [\ w] / on behalf of any letters, numbers or _, / [\ s] / on behalf of whitespace characters, including spaces, TAB, and newline.
/ [\ D ]/,/[ \ W ]/,/[ \ S] / are the above-mentioned negative situation.

The other is about the usage, please refer to "watir Technical Highlights"
5.7. Finally Raiders

In short, for object recognition and operation, it is necessary to help the above-mentioned document, and flexibility in the use of autoit, xpath object and abnormal operation. Watir does not support the control of windows, I was thinking about the first method is the adoption of third-party autoit technology, to simulate the keyboard or mouse operation.
For the operation of web objects, watir does not support the special tag, except the expansion of its underlying code, the only deep look at the xpath.
Finally,搞不定, only to watir group in a consultative expert, huh, huh.

There is no other better way to encounter. . .







Some errors

If the error AutoITX3, it helps to make automation happen

Then run at windows

regsvr32 "C: \ ruby \ lib \ ruby \ gems \ 1.8 \ gems \ watir-1.6.2 \ lib \ watir \ AutoItX3.dll"



Unknown OLE server: `AutoItX3.Control '(WI2OLERuntimeError)

that might mean you have not resisted the AutoItX3.dll in your computer,
in current Watir version, AutoItX3.dll has been included in the watir folder,
so Run the command in "Run":

regsvr32 "C: \ ruby \ lib \ ruby \ gems \ 1.8 \ gems \ watir-1.6.2 \ lib \ watir \ AutoItX3.dll"
then you will not get the errors:)

learn AutoItX3, please go to http://www.autoitscript.com/autoit3/index.shtml
I just want to use it to handle the popup windows when writing Watir scripts
Digg Technorati StumbleUpon Mixx del.icio.us Reddit BlinkList Furl YahooMyWeb feedburner

Tags: inheritance (RSS), ruby (RSS), current user (RSS), two kinds (RSS), quot quot quot quot (RSS), internet explorer (RSS), implementation approach (RSS), application solutions (RSS), pop up window (RSS), document object (RSS), open web standards (RSS), input operation (RSS), class button (RSS), object recognition (RSS), link button (RSS), web document (RSS), keystroke (RSS), dialog box (RSS), autoit (RSS), confirmation (RSS)

Permalink: http://www.codeweblog.com/ruby-watir-organize/

Leave a reply