[Watir] exception Object Recognition

Source: Internet
Author: User
Tags tmp file

5. identify abnormal objects (watir application solution)

Watir is based on processing and inherits fromWebDocument objects provide better support methods, but are weaker than processing Windows objects. Currently, from the perspective of objects collected by various websites, basically, for Web standard objects opened on IE instance objects
Successfully identified and operated,OthersFrom the current IE window. The pop-up object occupies a large part of exception object recognition.
Currently, the pop-up box is divided into three types:
1. Alerts warning function, such as sorry. The current user does not have permission to operate
2. The confirm OPERATION button is required. For example, are you sure you want to delete the current record?
3. Prompt + select + confirm requires user input, Operation query or click, and final confirmation, such as: Download/upload (Browse + Select File)

The following shows how watir can recognize and operate in the pop-up box above,
5.1. The pop-up box is based on autoit + thread implementation

This method is more effective for the first and second pop-up box operations,
A pop-up window is displayed because you click an object such as link/button (this method can be applied in most pop-up boxes .)

1. Definition Method

Def check_for_popups
Autoit = win32ole. New ('autoitx3. control ')
#

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

Ret = autoit. winwait ('message-webpage 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 (e.g. {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. Program 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 anotherRubyFile

In the second pop-up box, a pop-up window is displayed, such as a security warning click, and no object is clicked or operated.
I tried to use the 3.1 method, but it was not successful. Use the following method instead:

1. Add the method to the watir/windowhelper. RB file.

Def push_security_alert_yes_cn
@ Autoit. winwait "security alarm ",""
@ Autoit. Send "{tab }"
@ Autoit. Send "{tab }"
@ Autoit. Send "{space }"
End

2. Define another call file TMP. Rb
Require 'watir/windowhelper'
Helper = maid. New
Helper. push_security_alert_cn_yes

3. before opening a secure URL, start calling the ruby file.

Require 'rubygems'
Require 'watir' # The watir Controller
Require 'win32ole'
Require 'watir/windowhelper'

Thread. New {system ('Ruby C:/tmp. rb')} # You define the tmp file storage path.
Ie = watir: IE. New
Ie. Goto ("http://www.alipay.com.cn /")
5.3. Modify the underlying framework

This method is applicable to the dialog box 3.

Example 1: upload and download an object
1. Modify the input_elements.rb file of the underlying code,
In the filefield class, the method is set.
To support Chinese characters, replace one sentence with the following:

System ("rubyw-E/" require 'win32ole'; @ autoit = win32ole. new ('autoitx3. control '); waitresult = @ autoit. winwait select file ',', 15; sleep 1; if waitresult = 1/"-E/" @ autoit. controlsettext 'select The file', '', 'edit1', '# {setpath}'; @ autoit. controlsend 'select File ', '', 'button2',' {enter} ';/"-E/"End /"")

2. Definition Method
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", "", "close ")
End

3. Program body code:
Ie. file_field (: name, "xlsfile"). Set ("C: // samenameobject.html ")
# Upload the specified file

Or
Ie. span (: text, "Export Excel table"). click_no_wait
Save_file ("C: // abc.xls ")
# Download to the path you specified

 

For Dialog Box 3, you need to intervene in the query and perform the following operations:
For example 2, the pop-up box for category and customer Selection in CRM is different from the first implementation method,
The winactivate and controlclick methods in autoit are used,

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 -- webpage dialog box", "", 1)
Puts RET
If (ret = 1) then
Autoit. winactivate ("[Class: Internet assumer_tridentdlgframe]", "")
Autoit. Send ("{tab }")
Autoit. Send ("{tab }")

# Autoit. Send ("Huzhou ")
# Autoit. Send ("{ASC 2709 }")
# Autoit. sendunicode ("")
# Autoit. clipput ("Hangzhou ")
# 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 assumer_tridentdlgframe]", "")
Autoit. controlclick ("select -- webpage dialog box", "", "[Class: Internet assumer_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 ")
# Internet Explorer. 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, "new signature for customers"). Click

# Start popup Handler

Ie. Frame (: name, "right"). Frame (: name, "rtop"). Button (: value, "select customer"). click_no_wait
Sleep (20)

$ Popup = thread. New {check_for_popups}

At_exit {thread. Kill ($ popup )}

For the third popup window, if you need to interact with users and enter Chinese characters, use autoit sendKey to support Chinese characters (but the official documentation supports Chinese input, many people have encountered the same problems on the Internet.) No effective solutions have been found and need to be further followed up.

In addition to the above pop-up box, watir also has some extended tags or third-party controls that require special processing,
For example:
5.4. Web HTML editor
The HTML editor of The Chinese site provides: The operation ID or name is easier to operate directly.

Require 'rubygems'
Require 'watir' # The watir Controller
Require 'watir/windowhelper'
Require 'win32ole'
Ie = watir: IE. New

Ie = watir: IE. Attach (: title,/Alibaba Assistant /)
 
Ie. text_field (: name, "_ FMO. A. _ 0.s"). Set (" mhl1805 ")
Ie.doc ument. getelementbyid ("_ Editor"). contentWindow. Focus ()
Ie. send_keys ("abcsadfasfd ")

However, some web pages do not provide any ID. You can only use the autoit method to send the tab.

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 (" Chinese ")

Ie. checkbox (: ID, "detaildesc"). Set
Ie. checkbox (: ID, "detaildesc"). Focus ()
Ie. send_keys ("/t" * 9)
Ie. send_keys ('Hello worldabcdef ')

5.5. Reference to other common tag built-in methods for Object Recognition

For example, the IE. Div, ie. span, ie. Cell, and IE. Table methods can be used to perform click operations and value operations.
In additionQtpSimilar to descriptive programming, statistics on objects of the same type:

Ie. Buttons. Each {| B | puts B. to_s}
Puts ie. checkboxes. Length
Puts ie. Links. Length
Puts ie. Buttons. length, etc.

Common innertext attributes are encapsulated in watir and can be directly referenced. For example:
Ruby encapsulates innertext at the underlying layer of Object Recognition
Implementation: Call the method text:
For example, puts ie. Div (: ID, "intelligentword"). Link (: ID, index. to_s). Text

Finally, return the text and source code. Use the following method:
Puts ie. Text ()
Puts ie.html ()

5.6. Regular Expression support for recognition objects
 
Of course, Ruby provides powerful regular expressions, such as attribute identity regular expressions.
Ie = watir: IE. Attach (: title,/Alibaba /)

The ruby Regular Expression uses "//" as the basic framework. The expression content is located. Returns a Regexp object.
General rules of expressions:
/A/match character.
//? /Match special characters ?. Special characters include ^, $ ,? ,.,/,/, [,], {,}, (,), + ,*.
. Match any character, such as/A./match AB and AC.
/[AB] C/matches the range between AC and BC, and. For example:/[A-Z]/,/[a-zA-Z0-9]/.
/[^ A-zA-Z0-9]/match strings not in this range.
/[/D]/represents any number,/[/W]/represents any letter, number or _,/[/S]/Represents blank characters, including spaces, tab and line feed.
/[/D]/,/[/W]/,/[/S]/are the above negative conditions.

For more information about regular expressions, see watir technical highlights.
5.7. Final strategy

In short, for Object Recognition and operations, we need to use the autoit, XPath, and abnormal object operation methods with the help of the above documents. For Windows controls not supported by watir, the first method is to use third-party autoit technology to simulate keyboard or mouse operations.
Watir does not support special tags for operating web objects. Apart from extending its underlying code, you can only take a deep look at XPath.
Finally, you can only consult senior personnel in the watir group.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.