QTP/UFT tutorial for beginners: Returning a specific portion of a string using descriptive programming

Hello and welcome to another amazing blog post on our amazing QTPtutorial.net website! I've been getting a lot of emails and questions asking me how to return only a specific portion of a string. Let's get right into it! As usual, I have create a practice object below for you to work on.

Practice Value: 16728

Let's say that in your application, you have a specific page that conducts certain calculations after entering specific data. And let's say that the calculation always comes out in the format listed above. Practice Value: 16728 The Practice Value: is a constantly there, and the calculated value always comes dynamically after it. In this example, the calculated value is 16728. To verify that this value is correct, we have to pull the overall string value Practice Value: 16728 and extract the 16728 value in the process.

1. Use the object spy tool and click on the object above. It's a webelement with a unique property value for the innertext property.

 Browser("Index:=0").Page("title:=.*").WebElement("innertext:=Practice Value: 16728", "Index:=0")

2. We want to extract the value 16728 from the innertext object, so we can use the GetROProperty method.

Browser("Index:=0").Page("title:=.*").WebElement("innertext:=Practice Value: 16728", "Index:=0").GetROProperty("innertext")

3. Next, let's set a variable that will store this particular value.

strTestValue=Browser("Index:=0").Page("title:=.*").WebElement("innertext:=Practice Value: 16728", "Index:=0").GetROProperty("innertext")

Now we can use MsgBox and see that the value is extracted correctly.

MsgBox strTestValue

4. In our example scenario, since the 16728 number may change based on the calculation, we will use regular expressions to identify this object dynamically.

strTestValue=Browser("Index:=0").Page("title:=.*").WebElement("innertext:=Practice Value:.*", "Index:=0").GetROProperty("innertext")

I did this by replacing the static value 16728 with .* Now, no matter what value is set there, we will identify and extract that dynamic value.

5. Lastly, let's extract the number from the full string.

arrTestValue = Split(strTestValue, "Practice Value: ")
MsgBox arrTestValue(1)

I am using the split method on the value that we receive for the variable strTestValue. And I am splitting this variable by Practice Value: This method creates an array for me with two values. Our extracted number value can be seen using the MsgBox.

MsgBox arrTestValue(1)

Now run the full code and you will see the magic of automation!

strTestValue=Browser("Index:=0").Page("title:=.*").WebElement("innertext:=Practice Value: 16728", "Index:=0").GetROProperty("innertext")
arrTestValue = Split(strTestValue, "Practice Value: ")
MsgBox arrTestValue(1)

Post all of your amazing comments and questions below. We appreciate all of your feedback!!

Pin It on Pinterest

Clef two-factor authentication