Sunday, March 13, 2016

Key press before a QTP/UFT action.

At times there are situations where we need to hold a key (or key combination) before performing another action say click, select etc.


Here is a code example to press Alt+Ctrl+Shift before clicking an object.

Set deviceReplay = CreateObject("Mercury.DeviceReplay")
deviceReplay.KeyDown 29 ‘ ASCII for Ctrl key
deviceReplay.KeyDown 56 ‘ ASCII for Alt key
deviceReplay.KeyDown 42 ‘ ASCII for SHIFT key
Browser().Page().WebElement().Click

‘ Don’t forget to release the key oterwise it will continue to be pressed.

deviceReplay.KeyUp 29
deviceReplay.KeyUp 56
deviceReplay.KeyUp 42

That’s it. Happy coding!!

Friday, October 30, 2015

WebTable Row Click



More ofter than not, it is required to click a specific row in a WebTable. This click shall highlights entire specific row with all columns.

WebTable().ChildItem(row,column,micclass,index) simply doesn't work some time. An alternative to click the row is

Set Obj = Browser().Page().WebTable().Object.rows(row number)
Obj.Click

This might work as well.

Browser("B").Page("P").Frame("F").WebTable("T").Object.rows(i).cells(i).Click

Monday, November 24, 2014

Find instances of Sub string in a string. QTP, VB

Time and again, some or the other team member in my team encounters a situation where one need to find the number of instances of a substring in a given string. I have found the simplest, quickest and the smartes way to do so in just a line of code.
Here it is... (Method 1)
SubStringCount = Ubound(Split(StringName, delimeter))
Exampli gratia:
Finds the occurences of substring "m" in the string "Hiduism promotes harmony"
msgbox SubStringCount
returns 3 in this case.
Another reliable mehod would be to use a RegExp Object.
Method 2
str="Hiduism promotes harmony"
Set Regex=new RegExp
Regex.pattern="m"
Regex.global=true
Set Matches=Regex.execute(str)
msgbox Matches.count
returns 3 in this case again.
Another method (Method 3) would be use a Instr(Start, StringName, SubStringToSearch) function. Start can be replaced every time with the instances of SubStringToSearch. The couner shall be increased in parallel with every instance of SubString found.
This method (Method 4) is suggested by my team member Spoorthi K.
SubStringCount = Len(StringName) - Replace(StringName, SubStringToSearch, "")
Well, the above code need to be adjusted depending on the length of the SubString.

Pretty much in this post. See you again in another post.

Friday, November 14, 2014

Move/Set cursor position on screen



You can move/set the cursor position on screen using following code.
Set deviceReplayObject = CreateObject("Mercury.DeviceReplay")
Call deviceReplayObject.MouseMove(x, y)

Wednesday, November 12, 2014

Do... While/Do...Until riddle.



In my tryst with QTP for last 8, I have came across many people aspiring to use Do…while/Do…Until in their code but are surrounded with lots of confusion or lack of know how. Pavan (my buddy) is not an exception. He hasn’t used Do…Until in his programming so far and seems to be gravely obsessed with it. This post is for him and for any one who want to crack the Do..While/Do..Until riddle.

Let’s get started.

For loop is used in QTP (or VB for that matter) is used when a block of code need be repeated for certain number of times – but what if the number of lines of code that need to be repeated is based on certain condition or conditions. The Do…While and Do…until comes to rescue. These statements enables you to repeat a set of code while a certain condition is True, or until a certain condition is True.

Example Time:

Suppose you want to add series of numbers but you wouldn’t like to do it infinitely. So, a restrictive condition should be required say, sum shold not exceed 100.
Dim sum = 1
Do While sum < 100
  sum = sum + 10
Loop

Generic Version

Do While (Conditional statement)
Some code…( is executed while conditional statement is TRUE)
Loop
In this code, the Do…While loop evaluates the variable ‘sum ‘every time to check whether it is less than 100. If it is, the next chunk of code is run. If not, it moves to the next line of code following loop. The loop keyword tells the code to go back to the Do.. While line and evaluate the new value of sum.
Note: Multiple conditions can be added in Do…While using AND operator.

Unlike, the Do…While statement repeats a loop while the condition is true, the Do…Until statement repeats a loop until a condition becomes true.

Example Time:
 
Dim sum = 1
Do Until sum >= 100
  sum = sum + 10
Loop
 
Generic Version

Do Until (Conditional statement)
   Some code…( is executed until the conditional statement becomes TRUE)
Loop
 
The block of code resembles the Do…While statement, except that this time the code is evaluating the sum variable to check whether it is greater than equal to 100.
Please also note that there can be a natural variation of these loop statements as following.
 
Do

   some code …

Loop While i>10
------------
Do

   some code …

Loop Until i=10
 
I think it is evident that set of code is executed at least once without evaluation of condition and next iteration is dependent on True/False of condition.

Thursday, October 2, 2014

Get position of mouse:

Set controlObject = DotNetFactory.CreateInstance("System.Windows.Forms.Control")
currentX = controlObject.MousePosition.X
currentY = controlObject.MousePosition.Y


Debuggin DragAndDrop was tricky. I used DotNetFactory to get the mouse position. However, those co-ordinates dones't seemed to work when used in DragAndDrop method. Co-ordinate offset was the culpirt. Remember to try different offset when using such mehods.

Set DevRep = CreateObject( "Mercury.DeviceReplay" )
DevRep.DragAndDrop currentX +2, currentY+2, x + 190,  y + 90, 0