Saturday, January 16, 2010

QTP SetTOProperty

QTP SetTOProperty

The SetTOProperty method enables you to modify a property value that QuickTest uses to identify an object.

Because QuickTest refers to the temporary version of the test object during the run session, any changes you make using the SetTOProperty method apply only during the course of the run session, and do not affect the values stored in the test object repository.

The Object Repository window is read-only during record and run sessions.

Syntax of SetTOProperty method

Object(description).SetTOProperty Property, Value

Example 1 of SetTOProperty

I opened a new test in QTP and opened Calculator (Start -> All Programs -> Accessories->Calculator). I started Recording in QTP.

I clicked on button (7), then button (+), then button (3) and finally button (=). Finally I closed the Calculator. It recorded the first six lines of the script as seen below. I added the following lines afterward.

Object Repository shows that the text property of button named "7" has a value of 7.



'QuickTest refers to the temporary version of the test object during the run session.

Window("Calculator").Activate
Window("Calculator").WinButton("7").Click
Window("Calculator").WinButton("+").Click
Window("Calculator").WinButton("3").Click
Window("Calculator").WinButton("=").Click
Window("Calculator").Close
below statement retrieves a value of the text property of a button named "7" using GetTOProperty from memory.
x=Window("Calculator").WinButton("7").GetTOProperty("text")
msgbox x
the following statement would set the button's (named "7") text property value to seven (remember temporarily)
Window("Calculator").WinButton("7").SetTOProperty "text", "seven"
below statement retrieves a value of the text property of a button named "7" using GetTOProperty from memory.
x=Window("Calculator").WinButton("7").GetTOProperty("text")
msgbox x

After running the above statements the Object Repository will still be the same as it was before running the above statements. (See screenshot above)

Example 2 of SetTOProperty

Open a new Notepad.

Write the below text in the Notepad and save it as .html file. I saved it as a.html



When you open the file in IE it will look like as shown below:



Make sure that this above file (a.html) is open in IE and QTP is open.

Click on Record.

While recording click on Link 1.

Stop Recording. It will record the below line of code:

Browser("Browser").Page("Page").Link("Link 1").Click

So for this test, Object Repository contains information only for Link 1 as can be seen below:



And now if we run this test it will click only Link 1. [Object Repository does not contain information on Link 2]

Now if you write the below line in this test, removing the above line (which clicks link 1) and run, it will show error:

Browser("Browser").Page("Page").Link("Link 2").Click



But if you use SetTOProperty as below, it will click on Link 2 although Link 2 is not in Object Repository.

Browser("Browser").Page("Page").Link("Link 1").Click
Browser("Browser").Back
Browser("Browser").Page("Page").Link("Link 1").SetTOProperty "text", "Link 2"
Browser("Browser").Page("Page").Link("Link 1").Click

Also See:
QTP GetROProperty & GetTOProperties