Thursday, June 13, 2013

VBS : Tutorial 2 (Conditional Statements)

VBS : Tutorial 2 (Conditional Statements)

 
Conditional Statements
1) If…Then…Else Statement
2) Select Case Statement
 
 
If...Then...Else
example :Basic IF
 
Dim myDate
    myDate = #2/13/98#
    If myDate < Now Then myDate = Now
 
example : If with multiple statements inside
Dim x
x= 20
If x>10  Then
              
               msgbox "x value is: "&x
               msgbox "Bye Bye"
End If
 
example: If Else
Dim x
x= Inputbox (" Enter a value")
If x>100  Then
               Msgbox "X is a Big Number"
               Msgbox "X value is: "&X
Else
    Msgbox "X is a Small Number"
   Msgbox "X value is: "&X
End If
 
 
example : If ...ElseIf ...Else
fee="Visa"
 
if fee="Cash" then
            msgbox "pay cash!"
 
elseif fee="Visa" then
            msgbox "pay with visa."
 
elseif fee="American Express" then
            msgbox "pay with American Express."
 
else
            msgbox "Unknown method of Payment."
end If
 
 
Select Case
example :Basic
Dim x
x=10
Select case x
            case 10
                        msgbox "yes"
end select
           
example:  Full Eg
Option explicit
Dim x,y, Operation, Result
x= Inputbox (" Enter x value")
y= Inputbox ("Enter y value")
Operation= Inputbox ("Enter an Operation")
 
Select Case Operation
 
            Case "add"
                                    Result= x+(y)
                                    Msgbox "Addition of x,y values is "&Result
 
            Case "sub"
                                    Result= x-y
                                    Msgbox "Substraction of x,y values is "&Result
 
            Case "mul"
                                    Result= x*y
                                    Msgbox "Multiplication of x,y values is "&Result
 
            Case "div"
                                    Result= x/y
                                    Msgbox "Division of x,y values is "&Result
 
            Case "mod"
                                    Result= x mod y
                                    Msgbox "Mod of x,y values is "&Result
 
            Case "expo"
                                    Result= x^y                
                                    Msgbox"Exponentation of x,y values is "&Result
 
            Case Else
                                   
                                    msgbox "Wrong Operation"
 
End Select
 
 
1 .Grade students based on total marks.
2. Eligible to vote.
 
String
 
1.      Len
2.      Instr
3.      mid
4.      strReverse
5.      Replace
6.      strcomp
7.      split
8.      join
9.      space
10.  left
11.  right
12.  Lcase
13.  Ucase
 
 
Len
msgbox len("asdsa")
 
Instr
msgbox Instr(1,"asd","s")
 
mid
msgbox mid("asdzxc",1,3)
 
strReverse
msgbox strReverse("reverse")
 
Replace
msgbox Replace("reverse","e","1")
 
strcomp
                        -1 (if string1 < string2)
                        0 (if string1 = string2)
                        1 (if string1 > string2)
                        Null (if string1 or string2 is Null)
     msgbox strcomp("reverse1","reverse")
 
    Split
a=split("1 2 3"," ")
msgbox ubound(a)
 
join
a=split("1 2 3"," ")
msgbox ubound(a)
 
str=join(a," ")
msgbox isarray(str)
         
-------------------------------------------------------------------------------------
If condition types
 
if condition=true  then '------------1
      stat1   
      stat2
end if
 
 
 
if condition then '------------2
      stat1   
      stat2
else
      stat1
      stat1
     
end if
 
 
 
if condition1 then '------------3
      stat1   
      stat2
 
elseif condition2 then
      stat1
      stat1
     
end if
 
 
 
 
if condition1 then '------------4
      stat1   
      stat2
 
elseif condition2 then
      stat1
      stat1
 
elseif....
 
elseif....
 
 
 
else
      stat1   
      stat2
                 
end if
 
------------------------------------Select Case
Dim x
 x=30
 
Select Case x                                 'no break
     
      Case 10                                    'no colon
                  msgbox "x="& 10
     
      Case 20
                  msgbox "x="& 20
     
      Case else                     'default if nothing matches
     
                  msgbox "not exist"
 
End Select
-------------------------------------------------------
 
 
'len-----------------
'instr(starting_from , "Input String ", "To Search")----------
'mid("Input String ", starting_from , how_many_char )---------
'strreverse----------
      'msgbox ( len("piyush tiwari")  -           len(replace("piyush tiwari","i","")) )
'strcomp----
'split----
'join-----
'space
 
'left
'right
'lcase--
'ucase--
'replace------
 

VBS : Tutorial 1 (variables and datatypes)

VBS : Tutorial 1 (variables and datatypes)

 
Input/ Output Operations
Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns the contents of the text box.
Example:
Dim Input
Input = InputBox("Enter your name")
MsgBox ("You entered: " & Input))
 
 
Variable
o Must begin with an alphabetic character.
o Cannot contain period.
o Must not exceed 255 characters.
o Must be unique
Ex:          dim variable_name
                variable_name =InputBox("Enter your name:")
 
A variable containing a single value is a scalar variable.
A variable containing a series of values, is called an array variable.
 
 
VB Script Data Types--VARIANT
1.       One data type called a VARIANT
2.       Special kind of data type
3.       It can contain different kinds of information, depending on how it is used
4.       Only data type in VBScript
5.       Variant behaves in a way that is most appropriate for the data it contains.
6.       VarType
 
 
-------------------------------VarType ----------------------------------
Example 1 :
Dim a
a="hi"
msgbox vartype(a)
 
Example 2 :
Dim a
a="3"
msgbox vartype(a)
 
 
·         0 = vbEmpty - Indicates Empty (uninitialized)
·         1 = vbNull - Indicates Null (no valid data)
·         2 = vbInteger - Indicates an integer
·         3 = vbLong - Indicates a long integer
·         4 = vbSingle - Indicates a single-precision floating-point number
·         5 = vbDouble - Indicates a double-precision floating-point number
·         6 = vbCurrency - Indicates a currency
·         7 = vbDate - Indicates a date
Example1:
                        Dim a
                        a="1/1/2013"
                        msgbox vartype(a)
                        a=cdate(a)
                        msgbox vartype(a)
Example2:
                        Dim a
                        a="10:55:05"
                        msgbox vartype(a)
                        a=cdate(a)
                        msgbox vartype(a)
 
·         8 = vbString - Indicates a string
·         9 = vbObject - Indicates an automation object
·         10 = vbError - Indicates an error
·         11 = vbBoolean - Indicates a boolean
·         12 = vbVariant - Indicates a variant (used only with arrays of Variants)
·         13 = vbDataObject - Indicates a data-access object
·         17 = vbByte - Indicates a byte
·         8192 = vbArray - Indicates an array
--------------------------------------------------------------------------------------------
 
Keywords -
Date-                    Returns the current date
Time-                    Returns the current time 
Example : msgbox Time
 
Day -                      Returns day from a date
Month -              Returns month from a date
Weekday -          Returns weekday from a date
Year -                    Returns year from a date
Hour -                   Returns hour from a time
Minute-               Returns minute from a time
Second-               Returns seconds from a time
Example : msgbox Year(Date)
                msgbox second(time)
 
Now-                    Returns current date and time
Example : msgbox Now
 
Abs                        Returns absolute value of a number
Asc                         Returns the ASCII code of a character
Chr                         Returns a character from an ASCII code
msgbox abs(-1.3)
msgbox asc("a")
 
CBool                   Converts a variant to a boolean
CByte                    Converts a variant to a byte
msgbox CByte(123.33) > 123
CDate                    Converts a variant to a date
CDbl                      Converts a variant to a double
Cint                       Converts a variant to an integer
CLng                      Converts a variant to a long
CSng                      Converts a variant to a single
CStr                        Converts a variant to a string
--------------------------------------------------------------------------------
Constant
Example :
Dim a
                a="hi"
                const x="xxx"
                msgbox x
                x=10
----------------------------------------------------------------------------------
Operators
1.       Parentheses to override override the order of precedence
 
                Operator                                                                             Description
1) Exponentiation Operator (^)                 Raises a number to the power of an exponent.
2) Multiplication Operator (*)                     Multiplies two numbers.
3) Division Operator (/)                                 Divides two numbers and returns a floating-point result.
4) Integer Division Operator (\)                 Divides two numbers and returns an integer result.
5) Mod Operator                                              Divides two numbers and returns only the remainder.
6) Addition Operator (+)                               Sums two numbers.
7) Subtraction Operator (-)                          Finds the difference between two numbers or indicates the negative value of a numeric expression.
8) Concatenation Operator (&)                  Forces string concatenation of two expressions.
 
Operator
1) =        Equal to                5) <=      Less than or equal to
2) <>      Not equal to       6) >=      Greater than or equal to
3) <        Less than             7) Is        Object equivalence
4) >        Greater than                     
 
Operator                                                             Description
1) Addition Operator (+)                               Sums two numbers.
1.       1) Both expressions are numeric-       Add.
2.       2) Both expressions are strings-         Concatenate.
3.       3) One expression is numeric and the other is a string   -Add.
 
2) Concatenation Operator (&)  --->        Forces string concatenation of two expressions.
 
Operator                                             Description
               
1) Not-                  Performs logical negation on an expression.       
result= Not expression
 
2) And-                 Performs a logical conjunction on two expressions.
result= expression1 And expression2
 
3) Or      -             Performs a logical disjunction on two expressions.
                result= expression1 Or expression2
 
4) Xor    -              Performs a logical exclusion on two expressions.             
result= expression1 Xor expression2
 
5) Eqv                    -Performs a logical equivalence on two expressions.
result= expression1 Eqv expression2
                                                                                                (Ex-OR is given as "either A OR B but NOT both)
 
 
 


Wednesday, June 12, 2013

QTP : DataTables

QTP : DataTables
 
 
 
'Note in QTP  COLUMNS ARE CALLED AS "PARAMETERS "
 
'Setting Row to be operated
'----------------------------------------------------------------------------------
datatable.GetSheet("Global").SetCurrentRow 1
'----------------------------------------------------------------------------------                                                                                                              
 
 
'Write Data into a cell
'----------------------------------------------------------------------------------
datatable.GetSheet("Global").AddParameter "A","Hi" 
datatable.GetSheet("Global").AddParameter "B","Bye"                              
'----------------------------------------------------------------------------------
 
 
'Read Data into a cell
'----------------------------------------------------------------------------------
msgbox  datatable.GetSheet("Global").GetParameter ("A").Value                 
msgbox  datatable.GetSheet("Global").GetParameter ("B").Value
'----------------------------------------------------------------------------------
 
 
'Read Columns and Rows
''----------------------------------------------------------------------------------
msgbox "Row Count ="&datatable.GetSheet("Global").GetRowCount 
msgbox "Column Count ="&datatable.GetSheet("Global").GetParameterCount                              
'----------------------------------------------------------------------------------
 
 
'Display                Column name
'----------------------------------------------------------------------------------
msgbox "Column 1 Name="&  datatable.GetSheet("Global").GetParameter (1).Name 
'----------------------------------------------------------------------------------
 
 
'Number of Actions or Datatables
'----------------------------------------------------------------------------------
Msgbox "No of Actions ="&Datatable.GetSheetCount-1                       
'----------------------------------------------------------------------------------
 
 
'Add New Column
'----------------------------------------------------------------------------------
datatable.GetSheet("Global").AddParameter "xyz",""
'----------------------------------------------------------------------------------

'Datatable in Actions
'----------------------------------------------------------------------------------
RunAction "Action1",oneIteration,Datatable("A","Global")   'A=> 1-2
'----------------------------------------------------------------------------------