Tuesday, 30 July 2013

Explain typename function in QTP.

Typename function is useful when you want to know the data type of the variable.

Vb script is a scripting language used in QTP. All variables used in QTP are by default variant.
When we assign a value to variable, its data type changes.

For example -
print typename(a)  'Empty

a=10
print typename(a)  ' Integer

a=10.2
print typename(a) 'Double

a="sagar"
print typename(a)  'String

a=null
print typename(a) 'Null

This is how we can use the typename function in QTP to find the current data type of the variable.


Please give your inputs, suggestions, feedback to Us. We value your thoughts.

Explain Ubound function in QTP.

Ubound stands for upper bound. This functions is used to find the upper bound of the array.

Syntax -

ubound(arrayname, dimension)

It returns the upper bound of the arrayname and dimension used is specified in second parameter.
By default dimension is 1.

Example -

Single dimension array

Dim a(2)
'Declare an array with upper bound (index) as 2.

a(0) = 0
a(1) = 0
a(2) = 0

print ubound (a)           'will print 2
print ubound (a,1)        'will print 2

'This will print the value 2 as upper bound of the array is 2.

Please note that size or length of the single dimension array is ubound + 1.

Multi dimension array

Dim b(2,3,5)

print UBound(b, 1)           ' will print 2

print UBound(b, 2)           '  will print 3

print UBound(b, 3)           '  will print 5


More interview questions and answers on arrays in QTP are given below.
  1. What is the difference between dictionary and array in QTP?
  2. How to create dynamic 2 dimensional array in QTP?
  3. How to sort array of strings in qtp?
  4. How to create array of dictionary in qtp?
  5. How to define or declare array in qtp?
  6. How to return array from function in qtp?
  7. How to find the length of array in qtp?
  8. How to find the size of array in qtp?
  9. Explain the ubound function in qtp.
  10. Explain the split function in qtp with example.
  11. What is dotnetfactory object in qtp?

Please give your inputs, suggestions, feedback to Us. We value your thoughts.

How to find the name of the week day for given date in QTP?

You can find the name of the weekday like sunday, monday etc from a given date using below code.

There are 2 functions which help you find the name of week day or weekday name.

1. Weekday(anyvalid_date)  -  returns the numeric representation of weekday.
2. Weekdayname(intX)         - returns human readable weekday name like monday, sunday etc

print weekdayname(weekday(now))

Above statement will print the today's day name.

print weekdayname(weekday("09-jan-1986"))
'Will print thursday

To get the day name in abbreviated form use below statement
print weekdayname(weekday("09-jan-1986"), true)
'Will print thu.
Thus when you pass the second parameter as true, you will get the name in 3 letter format like sun, mon etc.

Above statement will print the name of day on 09-jan-1986.

Numeric representation of weekday is given below


1 - Sunday (default)
2  - Monday
3  - Tuesday
4  - Wednesday
5  - Thursday
6  - Friday
7  - Saturday





You can also use above statement to find out the day on which you were born.

Example to find the date of birth weekday -

Assume that your birthday is 24-oct-1985

dob = #24-oct-1985#
print weekdayname(weekday(dob))

This is how you can find the name of the weekday in QTP and vbscript.


Please find below more questions and answers on date and time in QTP.

  1. How to extract date part from timestamp in QTP?
  2. How to find the name of month of given date in QTP?
  3. How to extract time part from given date in QTP?
  4. How to extract the second part from given time stamp in QTP?
  5. Explain dateadd function in qtp with Example.
  6. How to find future date in qtp?
  7. How to get the system date in qtp?
  8. How to verify the date format in qtp?
  9. How to find the yesterday's date in qtp
  10. How to find the date difference in qtp
  11. How to get the current system date in qtp?
  12. How to get the current year in qtp?
  13. How to find the name of week day for given date in QTP?
  14. Explain now function in qtp.
  15. Explain the datediff function in qtp.



Please give your inputs, suggestions, feedback to Us. We value your thoughts.

How to get the current year in QTP?

There are many scenarios where you will need to find the current year in QTP.

Example -

Consider a scenario where you have to verify that the year displayed on the web page is correct or not.
Please note that You have to just verify the year in yyyy in this format and not the entire date.

QTP (Vb script) provides below function to find the current year.

print year(now)  

Above statement will print the current year.

The only parameter year function takes is the valid date. So if you want to find the year part of any given date in QTP you can pass the date as a first parameter.

Example -

print year("3-jun-2011") 

Above statement will print 2011.

Real Life usage of Year function in QTP. (Practical Project application of Year function)

We can use year function to find the current date which can be used to create unique random numbers. Random numbers or strings can be used in many situations like creating automation reports with unique file names.

This is how you can get the year of any given date in vbscript and QTP.

Please find below more questions and answers on date and time in QTP.

  1. How to extract date part from timestamp in QTP?
  2. How to find the name of month of given date in QTP?
  3. How to extract time part from given date in QTP?
  4. How to extract the second part from given time stamp in QTP?
  5. Explain dateadd function in qtp with Example.
  6. How to find future date in qtp?
  7. How to get the system date in qtp?
  8. How to verify the date format in qtp?
  9. How to find the yesterday's date in qtp
  10. How to find the date difference in qtp
  11. How to get the current system date in qtp?
  12. How to get the current year in qtp?
  13. How to find the name of week day for given date in QTP?
  14. Explain now function in qtp.
  15. Explain the datediff function in qtp.



Please give your inputs, suggestions, feedback to Us. We value your thoughts.

What is the difference between Round, Int and Fix in QTP ?

To know what round function is used for in QTP click this link Round function

Difference between int and fix 

Case 1  - Positive numbers

Int and fix function return the integer part of positive number.
For example -
print int(77.4)     ' Will print 77
print fix(77.4)     ' Will print 77
So when the numbers are positive, both the functions int and fix behave in same manner.

Case 2  - Negative numbers

print int(-77.4)     ' Will print -78   (return value is less than or equal to number)
print fix(-77.4)     ' Will print -77   (return value is more than or equal to number)

Basically for positive numbers, int and fix return same value for a given number.
For negative numbers above logic applies.

Please give your inputs, suggestions, feedback to Us. We value your thoughts.

What is Round function in QTP?

QTP supports round function that can be used to round the decimal numbers.

For example -

mydecimal = 6.34344 
roundedvalue= Round(mydecimal , 3)
print roundedvalue ' it will print 6.343

The second parameter to round function determines how many places to the right of the decimal should be used.

Another example of rounding.

mydecimal = 6.34644 
roundedvalue= Round(mydecimal , 2)
print roundedvalue 

' it will print 6.35 because the third decimal place value (6)  is greater than or equal to 5.


Please give your inputs, suggestions, feedback to Us. We value your thoughts.

How to generate the random number in QTP ?

QTP supports the function called Rnd that generates random value between 0 and 1.

rnd function takes 1 input parameter which acts as a seed to generated further random numbers in sequence.

To initialize the seed with system timer, you can use below statement.

Randomize


To generate the random integers we can use below statement.

Method 1
min = 1
max = 100

Randomize
RandomInt1= (Int((max-min+1)*Rnd+min))
RandomInt2= (Int((max-min+1)*Rnd+min))

print RandomInt1  'print the first random integer between 1 and 100.
print RandomInt2  ' print the second  random integer between 1 and 100.


Method 2

x=RandomNumber (0,100)
print x

This is how we can generate random numbers in QTP.


Please give your inputs, suggestions, feedback to Us. We value your thoughts.

How to extract text displayed in status bar of internet explorer in QTP?

You can get the text displayed in status bar of internet explorer using below statement.


strText =  Browser("google").Object.StatusText

print strText

This is how we can read the status bar text from QTP.

Please note that StatusText is the native property of Browser.


Please give your inputs, suggestions, feedback to Us. We value your thoughts.

How to extract digits from a given string in QTP?


Below example will show how to extract digits from a given string in QTP.


Mystr = "Order number is 3445456"
Set myRegExp = New RegExp
'Create a regular expression object

myRegExp.Global = True
myRegExp.Pattern = "[^\d]"

'set the pattern to non-digital characters

strDigits = myRegExp.Replace(Mystr, "")

'replace all non - digit characters by empty string.

Print strDigits

'Prints 3445456

More interview questions and answers on strings in QTP

Please give your inputs, suggestions, feedback to Us. We value your thoughts.

How to create a array of dictionary in QTP?

In QTP and vbscript we can create array of dictionary using below syntax/ example.


dim ArrayofDictionary(2)


'Now let us make first element of array as dictionary

Set ArrayofDictionary(0)= createobject("scripting.dictionary")
ArrayofDictionary(0).Add "key1", "temp1"
ArrayofDictionary(0).Add "key2", "temp2"

'Added keys in first dictionary


'Now let us make second element of array as dictionary

Set ArrayofDictionary(1)= createobject("scripting.dictionary")
ArrayofDictionary(1).Add "key1", "temp1"
ArrayofDictionary(1).Add "key2", "temp2"

'Added keys in second dictionary

and so on ............

To access the values inside dictionary, use below syntax.

print ArrayofDictionary(1)("key2")

This is how we can create an array of dictionary to store dictionaries in each element of Array.

More interview questions and answers on arrays in QTP are given below.
  1. What is the difference between dictionary and array in QTP?
  2. How to create dynamic 2 dimensional array in QTP?
  3. How to sort array of strings in qtp?
  4. How to create array of dictionary in qtp?
  5. How to define or declare array in qtp?
  6. How to return array from function in qtp?
  7. How to find the length of array in qtp?
  8. How to find the size of array in qtp?
  9. Explain the ubound function in qtp.
  10. Explain the split function in qtp with example.
  11. What is dotnetfactory object in qtp?
Please give your inputs, suggestions, feedback to Us. We value your thoughts.

Difference between dictionary and array in QTP

Here is the difference between Array and dictionary in QTP.
Below comparison illustrates when we should use array and dictionary.


Array
Dictionary
1
We can have dynamic array
We do not have concept called dynamic Dictionary.
2
Before use of array we must set the size of array
We do not need to set the size of dictionary.
3
Before adding extra element into dynamic array, We have to use redim  statement
We do not need to write any statement to add extra element. We just use add method.
4
We do not have any method to release the memory if particular element is not required
We can remove the element that is not required any longer.

Dictionary object has many in built functions that help us add, remove, access the elements by unique key. That is why we must use dictionary over array.

Please note that array does not have any methods to add/remove the elements.

How to create dynamic 2 dimensional array in QTP?

In QTP we can create dynamic array with any dimension.

Syntax -  To declare dynamic array is given below.

Dim a()

As shown above, we do not specify any dimension of array. 

To define the size of array we have to use below statement.
ReDim a(6,6)

In above statement, we have set the size of 2 dimensional array.

To preserve the existing data in array you must use below syntax.

ReDim preserve a(6,6)

Example to demonstrate 2 dimensional dynamic array is given below.


Dim a()

ReDim preserve a(6,6)

a(5,5) = array(12,33,454)

a(0,0) = array(2,3,4)

a(3,2) = 333

print a(5,5)(0)        ' prints 12



Please give your inputs, suggestions, feedback to Us. We value your thoughts.

Thursday, 25 July 2013

How to load ini file in QTP

Ini files are used to store global data required for testing.

Typical ini file looks like this.

[Environment]
                URL=http://xyz.com
                UserId=abc
                Password=pass

 [Environment]


Example - 

To load above ini file in QTP script, you have to use below lines of code

Environment.LoadFromFile “c:\Environment_Variable\Env.ini"

Print  Environment.Value("URL")

This is how you can read values from ini file in QTP.


Please give your inputs, suggestions, feedback to Us. We value your thoughts.

Best QTP Books

Everything About QTP

Hello Friends,
You can find QTP study material, Multiple choice questions (mcq), QTP question bank, QTP question papers, QTP notes, QTP questionnaire, scenario based QTP interview questions, QTP tutorial and QTP training on this site.

If you are a fresher or experienced QTP professional with (1/2/3/4) years of experience, this blog is just for you.