Descriptive programming-reproduced by 51 testing: Zhou Jian

Source: Internet
Author: User

【Abstract】 automated function testing is an enterprise-level functional testing tool used to check whether applications run as scheduled. Through automatic capture, detection, and repeated user interaction, the system can identify defects and ensure that business processes that span multiple applications and databases can avoid faults at the first release, and maintain long-term reliable operation. The most widely used products in the market include winrunner of mercury, quicktest professional, and rational robot of IBM. I am relatively familiar with quicktest professional and hope to have the opportunity to gradually introduce some key points and skills in quicktest professional. This article mainly introduces descriptive programming, a core content of quicktest professional, and hopes to help you learn from it. For convenience, quick test professional is short for qtp. (This article is based on quick test professional v8.0 ).

【Key words]

Descriptive programming: Descriptive Programming

Function test functional test

Expert View

Keyword view keyword View

Object Model

Runtime object run-time object

Test Object

[Body]

Basic methods for testing qtp Functions

We will briefly introduce the basic functional testing methods, which are the same for all automated functional testing products. In general, the basic methods for functional testing using qtp mainly include three main stages:

1. Create a test or build

First, you can create a test or component by recording sessions on an application or website, or creating an object library and manually adding steps to the keyword view using the keyword driver function. In qtp, we can add steps to create a test or component in two ways:

  • Recording sessions on an application or website.
  • Create an object library and use these objects to manually add steps in the keyword view or expert view

Then, insert a checkpoint in a test or component to check the specific values or features in the page, object, or text string to identify whether the website or application is running properly. Or extend the range of the test or component by replacing the fixed value with the parameter. Provides data in a data table, defines Environment Variables and values, defines tests, components, or operation parameters and values, or generates random numbers or current users and test data using qtp.

Finally, if necessary, use the numerous functional testing functions in qtp to enhance testing or components or add write statements to achieve more complex testing objectives.

2. Run tests and build

Control running sessions to help identify and eliminate defects in tests or components. Run the test or component step by step using the "single-step execution", "skip one step", and "Exit one step" commands, or set breakpoints to suspend the test or component at the predefined point. When a test or component stops at a breakpoint, you can view its variable values in the debug viewer.

3. analysis results

After a test or component is run, you can view the result in two ways: view the result in the results window; automatically report the defects detected during the running session, report to other defect management products if possible.

. Try to interact with the object model

Before introducing descriptive programming in qtp, we need to first introduce expertview and some basic knowledge about coding in expertview.

Two views are provided in qtp. The first is called keywordview (the keyword view, which is called Treeview in earlier versions), and the second is named expertview (expert view ), the two views are used by two types of users.

1. keywordview (keyword view)

Through the keyword view, qtp provides a modular table format to create and view tests or components. Each step is a line in the keyword view, so that you can easily modify any part.

During the recording session, each step performed by the user on the application is recorded as one line in the keyword view. For example, perform the following three steps on the 51testing page:

  • Enter zhoda02 in the "username" edit box.
  • In the "password" edit box, enter the encryption string 41c630a213508cd49eb35089db1b893144b9.
  • Click "Log on.

The keyword view contains the following rows:

Obviously, the keyword view is very intuitive and effective. Users can clearly view the recording level and running steps of the recorded object, which is suitable for those who are familiar with the business operation process. However, if you need some enhanced operations, you need to switch to the expert view.

2. expertview (expert view)

Each node in the keyword view corresponds to a script line in the expert view. The script corresponding to the above example is as follows: (the last sentence is deleted because it is repeated before and after and can be explained)

Browser ("51testing Software Testing Network: Professional Software Testing website "). page ("51testing Software Testing Network: Professional Software Testing website "). webedit ("username "). set "zhoda02"

Browser ("51testing Software Testing Network: Professional Software Testing website "). page ("51testing Software Testing Network: Professional Software Testing website "). webedit ("password "). setsecure "41c630a213508cd49eb35089db1b893144b9"

Browser ("51testing Software Testing Network: Professional Software Testing website"). Page ("51testing Software Testing Network: Professional Software Testing website"). webbutton ("login"). Click

For qtp, its core encoding language is Visual BASIC script. Therefore, if you are familiar with VBScript, You can freely add and update statements and enhance the test and script through programming, this must be done in the expert view.

More importantly, some operations must be completed in the expert view, for example, to process dynamic objects, Custom reports, and get the run-time value of objects) and so on. These must be encoded using VBScript in the expert view.

However, all operations in qtp are based on objects. Therefore, we must have a basic understanding of the object model before performing descriptive programming in the expert view.

3. Test Object Model

The test object model is a large set of object types or classes. qtp uses these object types or classes to represent objects in applications. Each test object class has an attribute list that uniquely identifies an object belonging to this class, and a set of qtp methods for recording it. It includes test object and runtime object ).

The test object is the object created by qtp in the test or component to represent the actual object in the application. Qtp stores information about this object, which helps it identify and check this object during running sessions.

The running object is the actual object in the website or application, and the method for this object is executed during the running session.

If you perform corresponding operations on the application during recording, qtp generally performs the following operations:

  • Identify the qtp test object class (the object that has performed the operation) and create the corresponding test object
  • Read the current value of the Object Property in the application, and store the attribute and property value list with the test object.
  • Select a unique name for the object. Generally, the value of an important attribute of the object is used.
  • Use the appropriate qpt test object method to record the operations performed on the object.

For example, if the following HTML source code is used, click "Search:

<Input type = "Submit" name = "find" value = "find">

Qtpl identifies the clicked object as a webbutton test object. It creates a webbutton object named find, records the following attributes and attribute values for the find webbutton object, and records the click Method for webbuttion.

In the keyword view and expert view, the displayed content is:

Browser ("Mercury Interactive"). Page ("Mercury Interactive"). webbutton ("find"). Click

When a test or component is run, qtp identifies each object in the application through its test object class and its description (a group of test object attributes and attribute values that uniquely identify the object. The list of test objects, their attributes, and attribute values is stored in the object library. For example, in the preceding example, qtp searches for the webbutton object in the object library during the running session, and finds its description using the name "find. Qtp searches for the webbutton object in the application based on the description found. The object contains HTML-marked input, type-submit, and value-find. After finding the object, it will execute the click Method on it.

Based on such an object model, qtp provides a set of methods and attributes for various application objects, such as web objects, Windows objects, sapgui objects, ActiveX, and Java. The following are some web objects methods and examples:

Object Method
Browser

Check

Frame

Click

Image

Exist

Link

Getcelldata

Page

Getproperty

Webarea

Getroproperty

Webbuttion

Mouseover

Webcheckbox

Rowcount

Webedit

Select

Weblist

Set

Webradiogroup

Setproperty

Webtable Submit

Example 1: Get the value in the cell

Thistext = browser (...). Page (...). Frame .(...). Webtable ("sample"). getcelldata (2, 1)

Example 2: Get the image name

Objectname = browser (...). Page (...). Image ("find"). getproperty ("name ")

Example 3: Check whether an object exists. If a dialog box is displayed, the object exists.

If browser ("Browser"). Page ("page"). Applet ("login.html"). javaedit ("username"). exist then

Msgbox ("the object exists .")

End if

Descriptive Programming)

1. descriptive programming Overview

Generally, when recording an operation, qtp adds the operation object to the object repository ). Once an object exists in the object library, you can add Related Object methods in the expert view to operate the object. We can add corresponding methods by referencing the object description in the hierarchical object library.

Because each object in the qtp Object Library has a unique name, the object name must be specified during reference. Then, during the test, qtp searches for the object based on the object name and parent object in the object library, and uses the test object description stored for the test object, identifies an object on a website or application.

For example, when we use qtp to record Yahoo Mail logon, we need to enter the user name, so during recording, we will record a webedit object whose default logic is "login ", the editing field is named "Yahoo! Mail-the best page, and the page uses the name Yahoo in the browser! Recording ., That is, the content of the Object Library during recording:

If you want to apply this object, you can enter the following information in the expert view:

Browser ("Yahoo! "). Page (" Yahoo! Mail-the best "). webedit (" login "). Set" XXX"

Alternatively, we can call some methods to obtain the object name when the modified object is running, for example:

Browser ("Yahoo! "). Page (" Yahoo! Mail-the best "). webedit (" login "). getroproperty (" name ")

However, we can find that when processing an object in the above example, the object already exists in the object library, so we can apply the logic name of this object. In actual use, the situation is often not so simple, we often encounter a lot of dynamically generated objects on the page, in other words, the object library does not have these objects, we do not have to reference. Therefore, we must use other technologies to perform such operations. This is the descriptive programming that we need to explain.

To solve the problem of handling dynamic objects mentioned above, qtp allows users to dynamically identify objects by encoding object attributes to test scripts. This is what we call descriptive programming in the general sense. In this way, we can instruct qtp not to reference the object library and object name to operate on the actual object. In specific operations, we only need to provide a set of attributes and values for the qtp object, so that qtp can identify and operate the corresponding object. This is equivalent to telling qtp to identify some key features of an object. Based on these features, qtp can match one by one and then identify the object.

Furthermore, more importantly, this descriptive programming method also enables qtp to recognize objects with the same attributes. Let's take an example to look at it: assume that the current Windows system has opened several Yahoo homepage (more than one), and now we want to close all browsers that are browsing the Yahoo homepage.

For the above example, let's take a look at a simple situation. Suppose there is only one Yahoo homepage: Then we can use the following method.

Window ("text: = Yahoo! -Microsoft Internet Explorer "). Close

We can see that the object we are looking for in the statement is the window title "Yahoo! -Microsoft Internet Explorer, and disable it. The specific syntax is described later. However, the preceding statement is only applicable to the aforementioned condition "only one Yahoo Homepage". If there are multiple identical windows, an error occurs, the reason is that the statement can match multiple objects, and qtp does not know which object to close. We need to further narrow the Matching Scope:

Dim I

I = 0

While (window ("text: = Yahoo! -Microsoft Internet Explorer "," index: = "& I). exist)

Window ("text: = Yahoo! -Microsoft Internet Explorer "," index: = "& I). Close

I = I + 1

Wend

We can see that for objects with the same attributes, we can use the index parameter to differentiate them. The first object is Index = 0, and the second object is Index = 1, and so on. Of course, we can also use the creationtime and location parameters to locate objects, which will not be described in detail here.

Through the above example, we have a basic understanding of descriptive programming. The following describes descriptive programming in detail: In the specific implementation, we have two types of descriptive programming methods. You can list the set of attributes and values that are directly described in the test statement. Alternatively, you can add a set of attributes and values to the description object and enter the name of the description object in the statement. The following is an example.

2. Enter the programming description directly in the statement.

You can directly describe an object in a statement by specifying the property: = value pair of the object. This is the most direct and effective method.

The general syntax is:

Testobject("Propertyname1: = propertyvalue1", "...", "propertynamex: = propertyvaluex "}

Testobject-test object class.

Propertyname: = propertyvalue-attributes and values of the test object. VariousProperty: = ValueSeparate the pair with commas (,) and quotation marks.

For example, the following statement specifies the webedit test object named author and with an index value of 3 on the mercury tours page. When the test is run, qtp searches for the webedit object with matching attribute values and enters the text Jojo.

Browser ("Mercury Tours"). Page ("Mercury Tours"). webedit ("Name: = Author", "index: = 3"). Set "Mark Twain"

We can also use descriptive programming from a specific location in the description (starting from the page object description.

Browser ("Mercury Tours"). Page ("title: = mercury Tours"). webedit ("Name: = Author", "index: = 3"). Set "Jojo"

In addition, if we want to use the same descriptive programming multiple times in a test or component, we can assign the created object to the variable, which is much easier to use.

For example, we need to complete the following operations:

Window ("text: = hypersna"). winbutton ("caption: = date"). Click

Window ("text: = hypersna"). winbutton ("caption: = Time"). Click

Window ("text: = hypersna"). winbutton ("caption: = OK"). Click

To make it easy to see, we can assign a value to window ("text: = hypersna") to a variable before using it. For more information, see the following code:

Set winhyper = Window ("text: = hypersna ")

Winhyper. winbutton ("caption: = date"). Click

Winhyper. winbutton ("caption: = Time"). Click

Winhyper. winbutton ("caption: = OK"). Click

If the with statement in VBScript is used, the following code can be simplified:

With window ("text: = hypersna ")

. Winbutton ("caption: = date"). Click

. Winbutton ("caption: = Time"). Click

. Winbutton ("caption: = OK"). Click

End

Here is a more detailed example. The default installation of qtp comes with an online ticket booking example called Mercury tour. Let's take a look at when descriptive programming is required during the ticket booking process.

First, after logging on to the system, if you need to book a ticket, you need to first search for flights. At this time, the system requires you to enter the number of passengers for booking tickets. Assume that we selected one passenger when recording the script for the first time, the ticket is successfully booked. Then, we need to parameterize the number of passengers to test the ticket booking system. We will find that playback will fail. The reason is that the number of passengers leads to the need to enter the name of each passenger during the ticket booking, while only the name of one passenger is entered during the recording. The input box of a passenger's name is dynamically generated as the number of passengers changes. Therefore, descriptive programming must be used because we cannot get the objects without recording from the object library.

When recording a single passenger, we get the recording statement:

Browser ("Welcome: Mercury Tours"). Page ("book a flight: Mercury"). webedit ("passfirst0"). Set "Michael"

Browser ("Welcome: Mercury Tours"). Page ("book a flight: Mercury"). webedit ("passlast0"). Set "Wang"

Obviously, webedit ("passfirst0") and webedit ("passlast0") are the objects generated during recording and stored in the object library. Through the object library, we can see that the object attributes are as follows:

When multiple firstnames are generated, the naming rules are passfirst0, passfirst1... And so on. Therefore, a simple descriptive programming can be used to identify the dynamic firstname and lastname. Here we assume that the parameterized Number of passengers has been assigned to intpassnum. Below are the key statements in the script:

Counter = 0

For I = 0 to (intpassnum)

Browser ("Find a flight:"). Page ("book a flight:"). webedit ("Name: = passfirst" & I). Set "Michael"

Browser ("Find a flight:"). Page ("book a flight:"). webedit ("Name: = passlast" & I). Set "Wang"

Counter = counter + 1

Next

3. Use the description object

You can use the description object to return the properties set object that contains a group of property objects. The property object consists of the property name and value. Then, you can specify in the statement to replace the object name with the returned properties set. (Each property object contains a property name and value ).

To create a properties set, enter the description. Create statement using the following syntax:

SetMydescription= Description. Create ()

Create a properties object (for exampleMydescriptionYou can enter a statement to add, edit, delete, or retrieve attributes and values in the properties object during the running session. In this way, you can use a dynamic method to determine which attribute and how many attributes should be included in the object description during the running session.

After filling in a set of property objects (attributes and values) in the properties set, you can specify the properties object in the test statement to replace the object name.

For example, assume that we need to complete the following operation:

Window ("error"). winbutton ("text: = OK", "index: = 1"). Click

We can use the description object to implement the same function. participate in the following code:

Set mydescription = description. Create ()

Mydescription ("text"). value = "OK"

Mydescription ("Index"). value = 1

Window ("error"). winbutton (mydescription). Click

Set mydescription = nothing

The image is missing. If you want to see the original, please go to http://www.51testing.com/html/51/133.html

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.