Friday, 7 March 2014

QTP Control Structures & Built-In Functions Based Examples

VBScript Examples 1. Write a program to find out whether the given number is Even number or Odd number? 1.             Dim num 2.    ... thumbnail 1 summary
VBScript Examples

1. Write a program to find out whether the given number is Even number or Odd number?

1.             Dim num

2.             num=inputbox ("Enter a number")

3.             n = cint(n) 

4.             If num mod 2=0 Then

5.             msgbox "This is a Even Number"

6.             Else

7.             msgbox "This is a Odd Number"

8.             End If

Line 1: Declare variable n.
Line 2: Displays dialog box for the user to input text , returns a string value.
Line 3: Converts input expression into an Integer and assigns that value to n.
Line 4: If the number is divided by 2 then number is an even number else it is an odd number.
Line 5: Prints if the number is an Even number
Line 7: Prints if the number is an Odd number.

Line 8: End of If statement.

Input: Enter a number: 6
Output: This is a Even Number

2. Find the factorial of a given number.


1.   Dim N,factorial

2.   N = inputbox(" Enter the Number")

3.   N = Cint(N)

4.   duplicateNumber = N

5.   factorial = 1

6.   if N<0 Then

7.   Msgbox  (" No factorial for Negative Numbers ")

8.   elseif (N=0) or (N=1) then

9.   Msgbox (" Factorial of  " &cstr(N) + " is 1")

10. else

11. while N>1

12. factorial = factorial * N

13. N = N-1

14. Wend

15. Msgbox(" Factorial of " & CStr(duplicateNumber) & " is " & CStr(factorial))

16. end if

Line 1-5: Variable declaration, assignment and Input Reading
Line 6-7: If the Number entered is a negative number then no factorial
Line 8-9: If the Number entered is 0 or 1 then factorial value is 1
Line10-13: If N>1 then factorial = factorial times N (factorial = factorial * N)
Line 15: Prints the factorial value

For input value = 5
Iteration                                  Value of N                                  Factorial
1                                                    5                                            1*5
2                                                    4                                            1*5*4
3                                                    3                                            1*5*4*3
4                                                    2                                            1*5*4*3*2

Output: Factorial of 5 is 120


3. Verify whether the given two Strings are equal.

1.             Dim string1,string2 

2.             string1 = inputbox (" enter the first string ")

3.             string2 = inputbox (" enter the second string ")

4.             MsgBox(" First string is :" +string1)

5.             MsgBox(" Second string is :" +string2)

6.             if StrComp(string1,string2) = 0 Then

7.             MsgBox ( " The Strings are Equal ")

8.             else

9.             MsgBox(" The strings are not Equal ")

10.          End if

Line 1: Declaration of variables
Line 2-3: Initialises the String
Line 4-5: Displays the given two strings
Line 6: StrComp function compares the given two strings. if the two strings are the same the StrComp function returns 0.

Input:         
enter the first string: hello
enter the second string: hello

Output:
First string is: hello
Second string is: hello

The Strings are Equal

4. How many times repeat the “a” character in the given string “Anand”

          Dim TargetStr, tmpArray 
TargetStr="Anand" 
tmpArray=split(lcase(TargetStr), "a" ) 
msgbox "Count of t in string is:" & ubound(tmpArray)
                 (or)
Dim Str, Schar
Str = inputbox("Enter String: ")
Schar = inputbox("Enter search char.: ")

cnt = len(str)
for i=1 to cnt
char = mid(Str, i, 1)

If ucase(char) = ucase(Schar) then
counter = counter+1
end if
next

msgbox "No of " & Schar & " character in the string " & Str &": " & counter
                            (or)
strsting = "HelLo"
    strnew = ""
    For i = 1 To Len(strsting)
        If InStr(1, strnew, Mid(strsting, i, 1), vbTextCompare) = 0 Then
         strnew = Mid(strsting, i, 1) & strnew
         strarr = Split(strsting, Mid(strsting, i, 1), -1, vbTextCompare)
            If UBound(strarr) > 1 Then
               MsgBox Mid(strsting, i, 1) & " is repeated " & _
                   UBound(strarr) & "times"
            End If
        End If
    Next

  
5. How to Find a character count in Given String using QTP
Hint:     first split the given string for the given char and the find out the length of array.

Dim TargetStr
Dim tmpArray
TargetStr="Software Testing: Char Count"
tmpArray=split(TargetStr,"t")
msgbox "Count of t in string is:" & ubound(tmpArray)  

Output: Count of t in string is: 3

6. QTP Script: How to Count Alpha Characters in a String

Hint:     isnumeric function is used to check the numeric values.
Dim myStr
Dim AlphaCharCount
myStr="Step by Step QTP Learning 123." 

AlphaCharCount=0
For i=1 to len(myStr)
    If not isnumeric (mid(myStr,i,1)) then
        AlphaCharCount=AlphaCharCount+1
    End if
Next
msgbox AlphaCharCount 

Output: 27

Total character in given string is 30


7) How Can I Find and Replace Text in a Text File?
There is no ReplaceText command directly that can open a text file and find and replace text inside the text file.

we can do the best thing is We can: 1) open up a text file; 2) read the text into a variable; 3) do a search-and-replace on that variable; and 4) re-save the text file. We can even do all that from the command line, although we’ll hold off on that for a moment. Instead, let’s start with a simple script that carries out the search and replace:

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "Jim ", "James ")

Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close

We start off by creating two constants (ForReading and ForWriting), which we’ll use for the two occasions when we’ll open our text file. (Yes, we said two occasions). We create an instance of the FileSystemObject, and then use the OpenTextFile method to open the file C:\Scripts\Text.txt for reading.

With the file open, we use the ReadAll method to read the contents of the entire file into the variable strText. We then close C:\Scripts\Text.txt even though we’ll almost immediately reopen it, this time for writing. Seems silly, yes, but that’s the way the FileSystemObject works: you can open a file for reading or you can open a file for writing, but you can’t perform both operations at the same time. (As you should know by now, the FileSystemObject works in mysterious ways.)

Having stored the contents of the file in the variable strText, we then use the VBScript Replace function to replace all instances of Jim with James. That’s what we do in this line of code:

strNewText = Replace(strText, "Jim ", "James ")

Notice that we’re looking for each instance of "Jim " (Jim followed by a blank space) and replacing those with "James " (James followed by a blank space). Though hardly foolproof, this gives our script a tiny bit of intelligence; if the script encounters the name Jimmy it won’t try to replace the Jim with James (resulting in Jamesmy). The new file we create - the one where each Jim has been replaced by James - is stored in memory in the variable strNewText.

Next we reopen our file (for writing), call the WriteLine method to write the contents of strNewText to the file, and then close the file a second time. The net effect? If we started off with a text file that looked like this:

Jim Jones
Mary Smith
Jim Doe
Jim Johnson
Mary Johnston
we’ll end up with a text file that looks like this:
James Jones
Mary Smith
James Doe
James Johnson
Mary Johnston

As for doing this all from the command-line, we simply need to modify the script so that it will accept - in this order - three command-line arguments: the name of the file to open; the text we want to search for; and the text we want to replace. Here’s a script that does just that. Note that we store the command-line arguments in the variables strFileName, strOldText, and strNewText, and we use those variables when opening and saving the text file and when calling the Replace function:

Const ForReading = 1
Const ForWriting = 2

strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strNewText = Wscript.Arguments(2)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText)

Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.WriteLine strNewText
objFile.Close

To use this revised script (which we’ll call replace.vbs) just type a command similar to this from the command prompt:

cscript replace.vbs "C:\Scripts\Text.txt" "Jim " "James "




8) Retrive the Total & Price from the Flight Application after open order; and find the sum

Dim total, price

total = Window("Flight Reservation").WinEdit("Total:").GetROProperty("text")
total = cint(total)

price = Window("Flight Reservation").WinEdit("Price:").GetROProperty("text")
price = cint(price)

sum = total + price

msgbox sum

Note:  Take the OR  and Run
In the above program take the test objects with value text and select GetROProperty  from step generator

Output: add without $ and displays

9) VarType Ex1:

Dim val
msgbox VarType(val)   '0 returns for String

val="India"
msgbox VarType(val)

val ="100"
msgbox VarType(val)   '8 returns for String

val =100
msgbox VarType(val)   '2 returns for String

val=100.234
msgbox VarType(val)  '5 returns for Double

val=#10/10/2011#
msgbox VarType(val)  '7 returns for Date

Set val = CreateObject("Scripting.FileSystemObject")
msgbox VarType(val)               '9 returns for automation object

msgbox vartype(a)      ‘9 returns for automation obect.

10) VarType Ex2:

Dim val, val2

val = inputbox ("enter a value:")
msgbox vartype(val)

val = cint(val)
msgbox vartype(val)

val2=inputbox ("enter a value:")
msgbox vartype(val2)

val2=cint(val2)
msgbox vartype(val2)

No comments

Post a Comment