Qtp VB tips (2)

Source: Internet
Author: User

1. generate random series
Method 1

Code:

Randomize' updates reverse data
Funcation rand (k, n)
N = int (k-1) * RND + 1)
Rand = N
End funcation

Method 2

Code:

N = randomnumber. Value (1,255)

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

Code:

Row = datatable. getcurrentrow
If ROW = "5" then
Datatable. Export ("D: \ data. xml ")
End if

3

Code:

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.

# Determine whether a window exists
If (Dialog (dialog_name). exist = true) then
Else
End if
Determines whether a window is activated.
If (window ("window_name"). Active = true) then
Else
End if
Import an Excel table
Datatable. Import (excel_name) 'import all the tables in the Excel file
Datatable. importsheet (excel_name, sheet_name) 'import the sheet table named sheet_name in Excel.
Export an Excel table
Datatable. exportsheet (excel_name, sheet_name) or
Datatable. exportsheet (excel_name, sheet_index)
Obtain the current number of rows in the table.
Datatable. getcurrentrow
Obtain the total number of rows in a table
Datatable. getrowcount
Returns the number of tables in an Excel worksheet.
Datatable. getsheetcount
Obtain or set a value in the running table.
Datatable. Value (colume_name, sheet_name) 'gets the value at the current cursor in the colume_name column.
Datatable. Value (colume_name, sheet_name) = new_value 'sets a new value.

Datatable. getsheet (sheet_name). getparameter (colume_name). valuebyrow (N)

Judge whether the current cursor is in the last column
Irow = datatable. getcurrentrow
If (datatable. Value (sheet_name). getparameter (colume_name). valuebyrow (irow) <> expected_value) then
For irow = 0 to irow <datatable. getrowcount
Rownum = irow
Next
End if
Get a column of data
Datatable (colume_name, sheet_name)
'A Login Dialog Box, first for an error judgment, and then enter the correct value and click the button_name button
Dialog_name. winedit (login_name). Set login_wrongname
Dialog (dialog_name). winedit (login_pwd). Set login_wrongpwd
Dialog_name. Dialog (wrongdialog_name). winbutton (button_name). Click
Dialog (dialog_name). winedit (login_name). Set login_correct_name
Dialog (dialog_name). winedit (login_pwd). Set login_correct_pwd
Dialog_name. winbutton (button_name). Click

Window methods and Properties
Window (win_name). Active

Code:

If browser ("web_name"). Dialog ("dialog_name"). exist (1) then' is not displayed = false
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.
5Datatable. Value ("num") is omitted only in the global form; Complete Form
Yes:

Code:

Datatable. Value ("num", dtlocalsheet)

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

Code:

Datatable. Value ("column_name", dtlocalsheet) = "Nanjing"

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

Code:

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:

Kk = datatable. addsheet ("sheet_name"). addparameter ("column_name", "value"). Name;

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

Code:

With Dialog ("name ")
C1 =. Button ("B _name"). click' // equivalent to Dialog ("name"). Button ("B _name"). Click
End

7Wintreeview operations

Code:

Select an entry: the root value of wintreeview. Select (item) is 0.
Root name: wintreeview. getitem (0)

8Database checkpoint module:

Code:

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

9Line Break

Code:

Vbcr ---- CHR (13) carriage return
Vblf ---- CHR (10) Line Break
Vbcrlf ---- CHR (13) + CHR (10)

10Run from step has two methods:

Code:

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.

11The attribute is changed. You can delete the attribute from the recognition object to solve the recognition problem.
12Operations on system files

Code:

------- Obtain information from system files and delete files
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

13Wait for an object to appear

Code:

Y =... waitproperty ("visible", true, 10000)

14Program interruption prevention methods

Code:

On Error resume next
On Error goto handle

15Array application:

Code:

Name = array (1, 2, "AA", "BB ")
Name (2) = "AA"

16Regular Expression Application Template

Code:

Perform a format check for the 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 elseMsgbox (result_message)
End select

17Returns the position of a string in another string.

Code:

Instr (string1, string2)

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

Code:

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:

Isarray' 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

Qtp provides a global error handling mode with popupMessage box, process next action iteration, stop run, and process next step.

Qtp also provides the local error handling mode on error resumt next/on error goto 0. You can implement the effect of process next step in a local range, and ignore errors to continue the subsequent steps. This local error processing mode takes precedence over global variables and is only valid in this function. The priority is higher than the global error processing mode, it takes effect only within the local range of the function (the action itself can be regarded as a function. It does not affect the error handling mode of the outer function, but changes the descendant function called in a local range and changes the error handling mode of the Child function to stop run!

The difference between the four global error handling modes is:

1. Process next step
In this mode, ignore errors and continue execution. You can use err. Number to determine whether an error has occurred.
Because every step of the Case function is closely related, it is impossible to ignore the error and continue to execute the next step.
It is also impossible to add an error check after each statement in each step, so there are too many error handling codes.
The check after many steps is not rigorous, and an error will be added and the first error message will be lost.
Therefore, this mode is not advisable.

2. Stop run
When an error occurs in this mode, an exception is thrown (the error message in the exception is obtained from the available ERR Object). The function is aborted and returned to the previous function layer by layer, after the final result reaches the action function (the action itself can be regarded as a function), the entire test execution will be stopped.
If an intermediate function has on error resumt next/on error goto 0, the system blocks the error, this intermediate function continues to be executed, does not stop the function, and returns the previous function.

3. PopupMessage Box
In this mode, when an error occurs, the pop-up dialog box allows the user to choose stop, retry, skip, and debug.
The master test must be fully automated and manual intervention is not required. Therefore, this mode is unavailable.

4. Process next action Iteration
This mode is similar to stop run, but it only exits this action loop and continues the next action loop.
The master test does not need to continue the next action loop, so this mode is not available.

After the above analysis, we can get the result. We use the Stop run mode for the master test and global error handling modes, at the same time, use the local error processing mode of on error resumt next/on error goto 0 in the master function to call the case function. In this way, when an error occurs in the case Function and Its subfunctions, the execution will stop and return to the master function layer by layer. The master function will intercept the error and record the error. Then, the main control function can continue to execute the next case function normally. Do not worry that the entire test will stop execution.

The detailed process of calling the case function of the main control function is as follows:

Err. Clear
On Error resume next
Call casefunctionname
If err. Number <> 0 then
Writelog err. Number'Error code
Writelog err. Description'Error description
Writelog err. Source'Error source object, but it seems useless
End
Err. Clear
On Error goto 0

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.