Wednesday, March 6, 2013

How to delete duplicate values in an array

The following script works both for integers and straing arrays.

Rather than using two many for loops and complex logic, it can be done with Dictionary and another Array object.

dim tempArr()
'arrName=Array(5,5,5,5,12,10,15,10,125,5,1,1,2,3,4,5,6,7,8,8,8,8,8,8,8)
arrName=Array("uday","naveen","kiran","usha","uday","vani","usha")
set d=createobject("Scripting.Dictionary")
n=0
For i = 0 To UBound(arrName)
        If Not d.Exists(arrName(i)) Then
     redim preserve tempArr(n)
            d.Add arrName(i), arrName(i)
            tempArr(n) = arrName(i)
     n = n + 1
        End If
Next
arrName = tempArr
for i=0 to ubound(arrName)
 msgbox arrName(i)
next

Sunday, February 10, 2013

Find the sum of the digits till the sum reaches to a single digit


no=54563 'You can give any number you want

While no>0
    modValue=no mod 10
    result=result+modValue
    no=int(no/10)
    If no=0 and result >9 Then
        no=result
        result=0
    End If
Wend
print result

Sunday, February 3, 2013

How to select more than one item from a Listbox using QTP

We can select more than one value from any list objects like JavaList, WinList, WebList, WpfList object ect.. by using "ExtendSelect" method.

The script would like below:
If the items are selected based on Item Names,
B( ).P( ).WL( ).select "Item Name1"
B( ).P( ).WL( ).extendselect "Item Name2"

If the items are selected based on Index, then
B( ).P( ).WL( ).extendselect "#2"

How to find all the selected items from a List object
B( ).P( ).WL( "Technology").select "Java"
B( ).P( ).WL( "Technology").ExtendSelect ".NET"
B( ).P( ).WL( "Technology").ExtendSelect "Testing"

sSelectedItems=B( ).P( ).WL( "Technology").GetROProperty("selection")
 print sSelectedItems

which prints selected items as
Jave;.NET;Testing

Saturday, February 2, 2013

Can virtual objects be definied using Descriptive Programming


Can virtual objects be defined using Descriptive Programming and also can we get the properties of Virtual Objects?

Overview of Virtual Objects:
Virtual Objects are objects in the application which behave like Standard objects but cannot be recognized by QTP.
So using Virtual Object Manager, we can map this Virtual Object to a Standard Class(based on its behavior) and its boundaries and can assign to a standard object(Parent).
By using Virtual Objects, you need to make sure that the application window should be at the same position and same size when you record and run your tests.

So come back to our question, can we define virtual objects using descriptive programming?
Answer is "Yes"
But we need to define x,y,width and height properties to define the virtual object.
It looks like something below:
window("Paint").WinObject("NetUIHWND").virtualobject("x:=256","y:=66","width:=22","height:=20").highlight
window("Paint").WinObject("NetUIHWND").virtualobject("x:=256","y:=66","width:=22","height:=20").Click 13,11

But how can we get these coordinates?
In a new QTP Test, add the virtual object and just record an action in the virtual object.
Once you get those coordinates write a script like above.
You can also these is "v" label added to the virtual object(see the attachment).


2nd Question: Can we get the properties of those virtual objects?
 Answer: Yes
If the virtual object is added to the Object Repository(OR), then you can use the GetTOProperty to retrieve whatever properties associated in the OR. You cannot get any values of the properties which are not in OR. So from the above screenshot, you can get only x, y, width, height and name property values only.

Friday, February 1, 2013

How to retrieve a string which have all types of alphabets and special characters

Say i have a string like "asasd^(&^*&skmw(_^_iolma%(&(%"

From the above string how to extract only alphabets?

The below code helps to retrieve/extract only alphabets from the input string
str="asasd^(&^*&skmw(_^_iolma%(&(%"

Set r=new regexp
r.pattern="[a-z A-Z]"
r.global=true
set s=r.execute(str)
'For each letter in s
'result= letter.value&result
'Next
For i=s.count -1 to 0 step -1
    Set oVal=s.item(i)
    result=oVal.value&result
next
print result

Now how to retrieve only special characters from the above string?

We can retrieve/extract by either of the below regular expressions.

str="asasd^(&^*&skmw(_^_iolma%(&(%"
Set r=new regexp
r.pattern="[^a-z A-Z]"
'r.pattern="\W"
r.global=true
set s=r.execute(str)
'For each letter in s
'result= letter.value&result
'Next
For i=s.count -1 to 0 step -1
    Set oVal=s.item(i)
    result=oVal.value&result
next
print result