Basic qtp code-reproduced from kai_top in 51testing.

Source: Internet
Author: User
1. generate random series
Method 1

Code: [copy to clipboard] randomize 'updates the reverse data.
Funcation rand (k, n)
N = int (k-1) * RND + 1)
Rand = N
End funcation

Method 2

Code: [copy to clipboard] n = maid. Value (1,255)

2. When a row in the table is run, all data in the table is automatically exported.

Code: [copy to clipboard] ROW = datatable. getcurrentrow
If ROW = "5" then
Datatable. Export ("D: \ data. xml ")
End if

3

Code: [copy to clipboard] webedit ("txtpass"). setsecure "sdsdf ...."

If the parameter password is set, you can directly write an unencrypted password into the data table, which will be automatically identified, that is, you do not need to change setsecure to set
4. In the pop-up dialog box, you can obtain the above prompt information and compare it with the information in the table. This does not necessarily prove that the pop-up prompt has an error and is mainly used for verification.

Code: [copy to clipboard] If browser ("web_name"). Dialog ("dialog_name"). exist (1) then' if no = false appears
Error_message = browser ("web_name"). Dialog ("diaglog_name"). Static ("incorrect user password! ". Getroproperty (" text ")
If error_message <> (datatable. Value ("error_info") then
Msgbox (error_message)
End if
Browser ("web_name"). Dialog ("diaglog_name"). Close
End if

Here I have summarized two tips:
1. For dialog, although the prompt message object name is "User Password error", if the information object name is "this user does not exist", it will be automatically recognized without changes, I think the main reason is that when recording the first time, the "User Password error" only allows the runtime to find this control, no matter what it is, because in the object repository, text is not the property of the object.
Second, if the prompt information is relatively long, you can use mid (error_message, n, m) to obtain a part of the feature prompt information for verification, So that I want to save processing time, avoid handling characters such as length and space.
5 datatable. Value ("num") is omitted only in the global form; Complete Form
Yes:

Code: [copy to clipboard] datatable. Value ("num", dtlocalsheet)

----- Assign values to cells in a column:

Code: [copy to clipboard] datatable. Value ("column_name", dtlocalsheet) = "Nanjing"

----- Obtain the specific value of a row:

Code: [copy to clipboard] datatable. setcurrentrow (N)
Msgbox (datatable. getsheet ("Global"). getparameter ("column_name"). rawvalue)
Or KK = datatable. rawvalue ("column_name", "Action1 ")

---- Dynamically add tables and data during Run-Time

Code: [copy to clipboard] KK = datatable. addsheet ("sheet_name"). addparameter ("column_name", "value"). Name;

6. Simplify the code and clarify the structure of the with -- end with structure:

Code: [copy to clipboard] with Dialog ("name ")
C1 =. Button ("B _name"). click' // equivalent to Dialog ("name"). Button ("B _name"). Click
End

7. wintreeview operations

Code: [copy to clipboard] Select an entry: wintreeview. Select (item) 'root is 0
Root name: wintreeview. getitem (0)

8. Database checkpoint module:

Code: [copy to clipboard] sub database_check
Set con = Createobject ("ADODB. Connection ")
Con. Open "Description = ibm_odbc; driver = SQL Server; server = IBM; uid = sa ;"&_
"Pwd = 123456; APP = quick test pro; wsid = IBM; database = ibm_table"
'Access method: con. Open "driver = {Microsoft Access Driver (*. mdb)}; DBQ = D: \ test. mdb"
Orocle mode: con. open "driver = {oracle in orahome92}; server = Ceshi; uid = cnd_test; Pwd = CND; DBQ = Ceshi; DBA = W; APA = T; EXC = F; xsm = default; Fen = T; qto = T; FRC = 10; FDL = 10; lob = T; rst = T; GDE = f; FRL = lo; Bam = ifallsuccessful; MTS = f; MDI = Me; CSR = f; fwc = f; PFC = 10; tlo = O ;"
Set record = Createobject ("ADODB. recordset ")
SQL = "select * From ibm_one_table"
Record. Open SQL, con
Do
If (Record ("ibm_table_column") = "Kai") then' // find the number of Kai instances in the table
Num = num + 1;
End if
Record. movenext
Loop until record. EOF = true
Record. Close
Set record = nothing
Con. Close
Set con = nothing
End sub

9 linefeed

Code: [copy to clipboard] vbcr ---- CHR (13) carriage return
Vblf ---- CHR (10) Line Break
Vbcrlf ---- CHR (13) + CHR (10)

There are two ways to run from step:

Code: [copy to clipboard] In keyword view mode, the operation ends from this step to all actions
In the expert view mode, only the operation of this action is completed.

11. The attribute is changed. Sometimes the attribute can be deleted from the recognition object to solve the recognition problem.
12. Operations on system files

Code: [copy to clipboard] ------- obtain information from the system file and delete the file
Get_file_infor ("C: \ She. mpg ")
Function get_file_infor (URL)
Dim FSO, F
Set FSO = Createobject ("scripting. FileSystemObject ")
Set F = FSO. GetFile (URL)
F. Name: f. Size: f. Type: f. datacreated '// obtain the file information.
FSO. deletefile (URL) '//// delete an object
End Function
-------- Get all file information in the folder
Get_folder_infor ("C: \ kai ")
Function get_folder_infor (folder)
Dim FSO, F, F1, n
Set FSO = Createobject ("scripting, FileSystemObject ")
Set F = FSO. getfolder (folder)
Set fc = f. Files
For each F1 in FC
Select case f1.name
Case "Kai. mpg", "she. mpg", "dd1_" '// check whether the folder contains these files
End select
Next
End Function

13. Waiting for an object to appear

Code: [copy to clipboard] Y =... waitproperty ("visible", true, 10000)

14 defense against program interruptions

Code: [copy to clipboard] on error resume next
On Error goto handle

15 array applications:

Code: [copy to clipboard] Name = array (1, 2, "AA", "BB ")
Name (2) = "AA"

16. Regular Expression Application Template

Code: [copy to clipboard] format check for date YYYY-MM-DD:
Function regexptest (patrn, strng)
Dim RegEx, match, matches 'create variable.
Set RegEx = new Regexp 'create a regular expression.
RegEx. pattern = patrn 'set pattern.
RegEx. ignorecase = true' set case insensiti.pdf.
RegEx. Global = true 'set global applicability.
Set matches = RegEx. Execute (strng) 'execute search.
For each match in matches 'iterate matches collection.
Retstr = retstr & "match found at position"
Retstr = retstr & Match. firstindex & ". Match value is '"
Retstr = retstr & Match. Value & "'." & vbcrlf
Next
Regexptest = retstr
End Function
Date_pattern = "^ (19 | 20) ([02468] [048]) | ([13579] [26])-02-29 )) | (20 [0-9] [0-9]) | (19 [0-9] [0-9]) -(0 [1-9]) | (1 [0-2])-(0 [1-9]) | (1 \ D) | (2 [0-8]) | (0 [13578]) | (1 [02])-31) | (01,3-9]) | (1 [0-2])-(29 | 30) $"
Result_message = regexptest (date_pattern, inputbox ("Enter the time to check:") 'use other regular expressions to change this field.
Select case result_message
Case ""
Msgbox ("the date format you entered does not match the standard ")
Case else msgbox (result_message)
End select

17. returns the position of a string in another string.

Code: [copy to clipboard] instr (string1, string2)

18 sometimes, when the playback fails to find the object, it may not be because of your code, but because of your operating system and other settings;

Code: [copy to clipboard] Example 1:
For example, you can record a file action on the disk.
Will be recorded:
. Winlistview (""). drap 46, 99
. Winlistview (""). draponitem "she.mp3"
During the next recording, if your system file is changed to show no extended name, the next execution time, qtpwill not find she.mp3, and only she can be found;
Example 2:
Sometimes some windows cannot be identified due to different operating systems and different ie. For example, the title of the webpage dialog box popped up in 2000 is:
"Web dialog box", and "Web page dialog box" on 2003"

19 "is + *" Type Function

Code: [copy to clipboard] isarray' whether it is an array
Isconnected 'determines whether qtp is connected to TD
Isdate' is a valid date type
Isempty' determines whether to initialize
Isnull' determines whether it is null
Isnumeric 'determines whether it is a number
Isobject' determines whether a function object is used
Isready' determine whether the device is ready
Isrootfolder' is the root directory

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.