Tuesday, 6 November 2012

QTP without using QTPScript



Write VBScript code without using QTPScript for below scenario
Ø  Launch google website
Ø  Enter a word in search box
Ø  Select third word in cache
Ø  Search the selected word
Ø  Close site

Option explicit
Dim bro, url, wsho, i
‘Launch google website
Bro= “C:\\Program Files\Internet Explorer\iexplore.exe
invokeapplication (bro&” “&url)
‘Keyboard automation using VBscript
Set wsho=createobject (“wscript.shell”)
Wait (10)
Wsho.sendkeys (“QTP”)
Wait (5)
For i= 1 to 3 step 1
Wsho.sendkeys(“{DOWN}”)
Wait (5)
Wsho.sendkeys(“{ENTER}”)
Wait (5)
‘closing google website
Wsho.sendkeys (“%{F4}”)

Yahoo Project on QTP



Write QTPScript along with VBScript code in QTP tool for below yahoo mail scenario

Ø  Launch Yahoo mail website
Ø  Do login
Ø  Click inbox link
Ø  Select “new mail” to compose a mail
Ø  Fill fields in compose page
Ø  Send a mail
Ø  Do logout
Ø  Close the yahoo site



Option explicit
Dim bro, url, wsho (windows shell object)
‘Launch Yahoomail website using VBscript
Bro= “C:\Program Files\Internet Explorer\iexplore.exe”
invokeapplication (bro&” “&url)
‘Do login using QTPscript in DP
With browser (“title:=.*)
          With.page (“title:=.*)
                .webedit (“name:=login”) .set “mindqtestingtools”
                .webedit (“name:=passwd”) .setsecure “509791ddb446a18e57e8cde98362f
    00483c1534594797788ed42”
                .webbutton(“name:=signin”) .click
          End with
End with
‘Go to inbox link using QTPscript in Dp
With browser (“title:=.*)
          With.page (“title:=.*)
                .link(“name:=inbox”, “index:=0”) .click
          End with
End with
‘Go to new message using QTPscript with DP
With browser (“title:=.*)
          With.page (“title:=.*)
                .webbutton (“name:=new”) .click
                .link (“name:=email message”).click
          End with
End with
‘Fill mail compose page using QTPscript with DP
With browser (“title:=.*)
          With.page (“title:=.*)
                .webedit(“name:=to”) .set mindq@yahoo.com
                .webedit(“name:=cc”) .set Johnatmindq@yahoo.com
                .webedit(“name:=subj”) .set “QTP Tool”
 ‘Automate Keyboard input for filling body using VBScript
                .webedit(“name:=subj”) .click
           Set wsho=createobject(“wscript.shell”)
           Wsho.sendkeys (“{TAB}”)
           Wsho.sendkeys (“{TAB}”)
           Wsho.sendkeys (“Hi Mindq, How are you doing………”)
‘Keyboard automation completed
                .webbutton(“name:=send”, “index:=0”) .click          
            End with
End with
‘Do logout using QTPscript in DP
With browser(“title:=.*”)
          With.page(“title:=.*”)
                .link(“name:=Signout”) .click
            End with
End with
‘Close Yahoo website using QTPscript in DP
Browser(“title:=.*”) .close


NOTE:  In above test script (VBScript and QTPScript), we automated yahoo mail compose page “body object” using keyboard automation indirectly because the body object is a webelement and visible as webedit.  For keyboard automation, we used “sendekeys” ( ) method along with “wscript.shell” components in the script.

This sendkeys ( ) method can take keyboard keys as input while automating misbehaving objects, and cache objects in webpages.  Cache means, a list of items depends on given text in an editbox.  Exp:  Google search box

SYNTAX:  set wsho=createobject(“wscript.shell”)
                 wsho.sendkeys(“send any keys”)

In above syntax, keyboard keys are two types, general keys and functional keys.  Functional keys must be enclosed by curly braces { }, and keys are case sensitive.

                                                KEYBOARD INPUTS
KEYNAME
SENDKEYS METHOD
BACK SPACE
{BACKSPACE} OR {BS} OR {BKSP}
BREAK
{BREAK}
CAPS LOCK
{CAPSLOCK}
DEL OR DELETE
{DEL} OR {DELETE}
DOWN ARROW
{DOWN}
END
{END}
ENTER
{ENTER} OR ~
ESC
{ESC}
HELP
{HELP}
HOME
{HOME}
INS OR INSERT
{INS} OR {INSERT}
LEFT ARROW
{LEFT}
NUM LOCK
{NUMLOCK}
PAGE DOWN
{PGDN}
PAGE UP
{PGUP}
PRINT SCREEN
{PRTSC}
RIGHT ARROW
{RIGHT}
SCROLL LOCK
{SCROLLLOCK}
TAB
{TAB}
UP ARROW
{UP}
F1 TO F16
{F1} TO {F16}
SHIFT
+
CONTROL
^
ALT
%
+
\+
^
\^
%
\%
~
\~
a-z
a-z
0-9
0-9
BLANK SPACE

OTHERS
OTHERS

Wednesday, 31 October 2012

Loops in VBScript



LOOPS IN VBScript:  To execute a block of code more than one time, we can use loops in VBScript

a)      WHILE LOOP:  To execute a block of code more than one time as long as given condition is true

----------------
---------------- (1)
----------------
                       While Condition
----------------       (2)
----------------        True (Loop)
  (3) False (Exit)               ----------------
                                                Wend
----------------
----------------
----------------
NOTE:  While loop ends with “Wend” (Not case sensitive)


b)      DOWHILE LOOP:  To execute a block of code more than one time as long as the condition is true, In this loop, the code will be executed at least once at the beginning without a condition

----------------
----------------
----------------
                                                    Do    (1)                  
----------------       (2)
----------------        True (Loop)
                              ----------------
                                                While condition
----------------  (3) False (Exit)
----------------
----------------

c)       DOUNTIL LOOP:  To execute a block of code more than one time as long as given condition is false, we can use this loop


----------------
----------------
----------------
                                                 Do (1)
----------------       (2)
----------------        False (Loop)
                              ----------------
                                                Until condition
----------------  (3) True (Exit)
----------------
----------------
NOTE:  DOUNTIL loop is otherwise around to DOWHILE loop

VBScript has an option of “EXIT DO” statement to terminate from “DOWHILE” and “DOUNTIL” loops if required


----------------
----------------
----------------
                                                   Do                            
----------------      
----------------       
                              ----------------
                                                If condition then
                                                Exit do

                            True (Exit)                               While condition

----------------   False (Exit)
----------------
----------------




d)      FORLOOP:  To execute a block of code for specified number of times, we can use this loop

EXP:
For i = 1 to 10 step 1

                ------------------
                ------------------
                ------------------
                      Next (Exit)

The above loop executes 10 times

EXP:
For i = 0 to 9 step 1

                ------------------
                ------------------
                ------------------
                      Next (Exit)

The above loop executes 10 times (here “0” counts as one loop)


EXP:
For i = 10 to 1 step -1

                ------------------
                ------------------
                ------------------
                      Next (Exit)

The above loop executes 10 times


EXP:
For i = 1 to 10 step 3

                ------------------
                ------------------
                ------------------
                      Next (Exit)

The above loop executes 4 times (first execution is 1, then 4, then 7, and 10)

e)      FOREACH LOOP:  To run a block of code more than one time depends on each element in an array or list
EXP:                            
                X = array (123, “mindq”, “abc”, 789)
                                 X(0)       x(1)        x(2)     x(3)

------------------
                ------------------
                ------------------
Next (Exit)
This loop executes 4 times based on he values in X array
ARRAYS IN VBScript:  Variables are used to store one value at a time of any type, but arrays are used to store multiple values of any type.  To create arrays in VBScript, we can follow below code

Option explicit
Dim x(4)

In above example, X is an ARRAY with 5 values in it (0-4)

EXP:           x(0)      x(1)           x(2)         x(3)        x(4)
“abc”
123
“mindq”
10.5
“date”
       X=
From the above diagram, X is an array with 5 values or elements and each element will be accessible by using an array name and index, index starts with x(0)

EXP:  x(3)  = 4th element in “X” array

The first index x(0) is called LBOUND (lower bound)
The last index x(4) is called UBOUND (upper bound)

EXPAMPLE#1:  Write a VBScript code in QTP tool to create an array with 5 elements to store 5 subject marks into that array, and display total marks
Option explicit
Dim x(4), i, y
For i = 0 to 4 step 1
    x(i)=inputbox ("Enter a subject marks")
Next
    y=0
For i = 0 to 4 step 1
    y = y+x(i)
Next
    msgbox("Total Marks   "&y)

EXPAMPLE#2:  Write a VBScript code in QTP tool to create an array with 5 elements to store 5 Sentences, with at least one or two sentences with a word “INDIA”

Option explicit
Dim x(4), i
For i = 0 to 4 step 1
   x(i)=inputbox ("Enter a Sentence")
Next
For i = 0 to 4 step 1
If instr (x(i), "india")<>0 Then
   msgbox (x(i))
End If
Next

INSTR ( ):- This function is used to check the availability of sub string in main string

SYTAX:  instr(“mindq”, “testing”)
                (m-string)  (s-string)

     If available returns “position”
     If not available returns “0”