Search This Blog

Sunday, August 8, 2010

VB Script - 1

What is VBScript?
VBScript is a subset of Visual Basic 4.0 language. It was developed by Microsoft to provide more processing power to Web pages. VBScript can be used to write both server side and client side scripting. (If you already know Visual Basic or Visual Basic for Applications (VBA), VBScript will be very familiar. Even if you do not know Visual Basic, once you learn VBScript, you are on your way to programming with the whole family of Visual Basic languages.)
Data types
VBScript supports only one data type called ‘Variant’. The variant data type is a special kind of data type that can contain different kinds of information. It is the default data type returned by all functions in VBScript. A variant behaves as a number when it is used in a numeric context and as a string when used in a string context. It is possible to make numbers behave as strings by enclosing them within quotes.
Variables
A variable is a placeholder that refers to a memory location that stores program information that may change at run time. A variable is referred to by its name for accessing the value stored or to modify its value.
Variable Declaration
Variables in VBScript can be declared in three ways:
Dim Statement
Public Statement
Private Statement
For example:
Dim No_Passenger

Multiple variables can be declared by separating each variable name with a comma. For example: Dim Top, Left, Bottom, Right

You can also declare a variable implicitly by simply using its name in your script.That is not generally a good practice because you could misspell the variable name in one or more places, causing unexpected results when your script is run. For that reason, the Option Explicit statement is available to require explicit declaration of all variables. The Option Explicit statement should be the first statement in your script.
Note:
Variables declared with Dim at the script level are available to all procedures within the script. At the procedure level, variables are available only within the procedure.

Public statement variables are available to all procedures in all scripts.

Private statement variables are available only to the script in which they are declared.

Naming Convention

There are standard rules for naming variables in VBScript. A variable name:
• Must begin with an alphabetic character.
• Cannot contain an embedded period.
• Must not exceed 255 characters.
• Must be unique in the scope in which it is declared.


Assigning Values to Variables

Values are assigned to variables creating an expression as follows: the variable is on the left side of the expression and the value you want to assign to the variable is on the right. For example: B = 200

Scalar Variables and Array Variables

Much of the time, you only want to assign a single value to a variable you have declared. A variable containing a single value is a scalar variable. Other times, it is convenient to assign more than one related value to a single variable. Then you can create a variable that can contain a series of values. This is called an array variable. Array variables and scalar variables are declared in the same way, except that the declaration of an array variable uses parentheses ( ) following the variable name. In the following example, a single-dimension array containing 11 elements is declared:
Dim A(10)

Although the number shown in the parentheses is 10, all arrays in VBScript are zero-based, so this array actually contains 11 elements. In a zero-based array, the number of array elements is always the number shown in parentheses plus one. This kind of array is called a fixed-size array.

Constants

A constant is a meaningful name that takes the place of a number or a string, and never changes. VBScript in itself has a number of defined intrinsic constants like vbOK, vbCancel, vbTrue, vbFalse and so on.

You create user-defined constants in VBScript using the Const statement. Using the Const statement, you can create string or numeric constants with meaningful names and assign them literal values.
For example:
Const MyString = "This is my string."
Const MyAge = 49
Note that the string literal is enclosed in quotation marks (" "). Also note that constants are public by default.
Within procedures, constants are always private; their visibility can't be changed.

Wednesday, August 4, 2010

How to send a key command to a Web object in QTP

Some Web objects will perform actions when certain key commands, such as ALT+RETURN, are entered. These Web objects do not have a type method associated with them that can be used to replay these keys. How can the key combination be replayed?

________________________________________
Solution: Use the Windows Scripting SendKeys method
1. Create a WScript.Shell object.
2. Activate the browser in which you want to execute the keys.
3. Use the SendKeys method to type the key combination.
Example:
‘ This code executes the CTRL+F key combination (search) on a browser.
Set WshShell = CreateObject(”WScript.Shell”)
WshShell.AppActivate “Put the label of the browser” ‘ Activate the browser window
wait(3)
WshShell.SendKeys “^f” ‘ The caret (^) represents the CTRL key.
wait(2)
object.SendKeys(string)
object A WshShell object.
string The string value indicating the keystroke(s) you want to send.
The SendKeys method will send keystroke(s) to the active window. To send a single character (for example, x), use “x” as the string argument. To send multiple characters (for example, abc), use “abc” as the string argument. You can also send special characters such as SHIFT, CTRL, and ALT, which are represented by the plus sign (+), the caret (^), and the percent sign (%), respectively.
For more information on the SendKeys method and other special keys, such as the backspace or a function key, please refer to the MSDN SendKeys Method page.
====================================================================================================
‘ Name: KeyboardShortCut

‘ Input:
‘ ByRef Key As Variant - The keyboard key to be typed.
‘ ByRef Pane As Variant - The window/pane to type in.
‘ Purpose: To type keyboard input onto the open window.
‘====================================================================================================
Function KeyboardShortCut (Key)
dim obj
Set obj = Window(”Window Name” ).WinObject(”Object”)
obj.type Key
Environment(”LastResult”) = “Success”
End Function
Solution2: Pressing Function keys should be recorded automatically
Directly recording on the application and pressing the Function Keys (F1, F2, etc.) should generate code for replay.
Example:
‘Here is an example recorded against Notepad:
Window(”Notepad”).Activate
Window(”Notepad”).WinEditor(”Edit”).Type micF5
If the above does not work, then you can use DeviceReplay to simulate pressing keyboard keys.
Example:
‘Here is an example that does the same thing above, but used DeviceReplay.
Set obj = CreateObject(”Mercury.DeviceReplay”)
Window(”Notepad”).Activate
obj.PressKey 63
Note:
The PressKey method uses the appropriate ASCII or IBM Scan Code value for the key. “63″ is the IBM Scan Code value for F5.

Sample Selenium Test Suite

TestSuite.html
Test suite for the whole application
Access main page
Login to application
Change address
Logout from application











Open fire fox not selenium IDE and paste this link in the address bar

chrome://selenium-ide/content/selenium/TestRunner.html?baseURL=http://localhost&test=file:///dir/testsuite.html&auto=true



replace http://localhost with the baseURL of your server






Running Test Suite from command line
If you want to run a number of tests from the command line - for example, as an acceptance test step, or as a part of the checkin, you have a couple of options.
1) Selenium RC provides convenient command-line support for running HTML Selenese tests from the command-line or with Ant: http://www.openqa.org/selenium-rc/selenese.html
2) Create a batch file that launches Firefox pointed to the suite. For example, on Windows, my batch file looks like:
"C:\Program Files\Mozilla Firefox\firefox.exe" -chrome "chrome://selenium-ide/content/selenium/TestRunner.html?test=file:///C:/tests/AllTests.html&auto=true&baseURL=http://mysite.com " -height 900 -width 900

Selenium Commands - Part 9

U

uncheck(locator)
Arguments:
• locator - an element locator
Uncheck a toggle-button (checkbox/radio)


uncheckAndWait(locator)
Generated from uncheck(locator)
Arguments:
• locator - an element locator
Uncheck a toggle-button (checkbox/radio)