Showing posts with label QTP Interview Questions. Show all posts
Showing posts with label QTP Interview Questions. Show all posts

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

Wednesday, September 5, 2012

How to reverse a string without using any built-in function

As per the question/requirement you should not use any built-in methods like mid, instr etc.

We can reverse the string using the regular expression.

Find the script below:

'The below script prints the input string in reverse order without using any built-in methods

str="Hello Uday"

Set regExpObj=new RegExp
regExpObj.pattern="[a-z A-Z]"
regExpObj.global=true

Set matches=regExpObj.execute(str)
For each letter in matches
    result=letter.value&result
Next

print result

The above code prints "yadU olleH"

Actually i got this code from one netizen, but i didnt understand the logic when i look at it. Then i started nailing down the logic.

Here is how it goes:
The regular expression "[ ]" stands for matches one character in a list at a time, so [a-z A-Z] bring only one character from "a-z" or "A-Z".



So upon executing this regular expression did you see there are 10 characters(length of the string).

So in the next for loop we are iterating through these 10 characters from 1-10.

Because we are appending the the new character to the result, the first char will become last and second character becomes last but one and so on.

At the end of the loop, you will get the reversed string.

I hope you will be clear now.