Monday, July 20, 2009

Retrieve the Top N records from a DataBase Table?

Returning TOP N Records

Returning only the first N records in a SQL query differs quite a bit between database platforms. Here's some samples:

Microsoft SQL Server
SELECT TOP 10 column FROM table

PostgreSQL and MySQL
SELECT column FROM table
LIMIT 10

Oracle
SELECT column FROM table
WHERE ROWNUM <= 10

Sybase
SET rowcount 10
SELECT column FROM table

Firebird
SELECT FIRST 10 column
FROM table

Due to these differences if you want to keep your code database independent you should use the maxrows attribute in the cfquery tag in ColdFusion. The tradeoffs to database independance is performance, I would expect maxrows to be slower than specifying the rows in the SQL.


SELECT column FROM table

PostgreSQL and MySQL have a cool feature that will let you return an arbitrary range of rows (eg return rows 10-20). This is very handy for displaying pages of records:

SELECT column FROM table
LIMIT 10 OFFSET 20
The above query will return rows 20-30




courtesy :http://www.petefreitag.com/item/59.cfm

Sunday, July 19, 2009

VBScript Functions..

follow the link :

http://www.w3schools.com/VBscript/vbscript_ref_functions.asp

Do ...Loop statements in VB Script of QTP??

Do...Loop
If you don't know how many repetitions you want, use a Do...Loop statement.

The Do...Loop statement repeats a block of code while a condition is true, or until a condition becomes true.

Repeat Code While a Condition is True
You use the While keyword to check a condition in a Do...Loop statement.

Do While i>10
some code
Loop

If i equals 9, the code inside the loop above will never be executed.

Do
some code
Loop While i>10

The code inside this loop will be executed at least one time, even if i is less than 10.

Repeat Code Until a Condition Becomes True
You use the Until keyword to check a condition in a Do...Loop statement.

Do Until i=10
some code
Loop

If i equals 10, the code inside the loop will never be executed.

Do
some code
Loop Until i=10

The code inside this loop will be executed at least one time, even if i is equal to 10.

Exit a Do...Loop
You can exit a Do...Loop statement with the Exit Do keyword.

Do Until i=10
i=i-1
If i<10 Then Exit Do
Loop

The code inside this loop will be executed as long as i is different from 10, and as long as i is greater than 10.

VBScript procedures in QTP?

VBScript Procedures
In VBScript, there are two kinds of procedures:

Sub procedure
Function procedure

--------------------------------------------------------------------------------

VBScript Sub Procedures
A Sub procedure:

is a series of statements, enclosed by the Sub and End Sub statements
can perform actions, but does not return a value
can take arguments
without arguments, it must include an empty set of parentheses ()
Sub mysub()
some statements
End Sub

or

Sub mysub(argument1,argument2)
some statements
End Sub


--------------------------------------------------------------------------------

VBScript Function Procedures
A Function procedure:

is a series of statements, enclosed by the Function and End Function statements
can perform actions and can return a value
can take arguments that are passed to it by a calling procedure
without arguments, must include an empty set of parentheses ()
returns a value by assigning a value to its name

Function myfunction()
some statements
myfunction=some value
End Function

or

Function myfunction(argument1,argument2)
some statements
myfunction=some value
End Function


--------------------------------------------------------------------------------

How to Call a Procedure
The line below shows how to call a Function procedure:

carname=findname()

Here you call a Function called "findname", the Function returns a value that will be stored in the variable "carname".

Or, you can do like this:

msgbox "Your car is a " & findname()

Here you also call a Function called "findname", the Function returns a value that will be displayed in the message box.

When you call a Sub procedure you can use the Call statement, like this:

Call MyProc(argument)

Or, you can omit the Call statement, like this:

MyProc argument

Variables in VBScript of QTP?

VBScript Variables
As with algebra, VBScript variables are used to hold values or expressions.

A variable can have a short name, like x, or a more descriptive name, like carname.

Rules for VBScript variable names:

Must begin with a letter
Cannot contain a period (.)
Cannot exceed 255 characters
In VBScript, all variables are of type variant, that can store different types of data.


--------------------------------------------------------------------------------

Declaring (Creating) VBScript Variables
Creating variables in VBScript is most often referred to as "declaring" variables.

You can declare VBScript variables with the Dim, Public or the Private statement. Like this:

dim x;
dim carname;

Now you have created two variables. The name of the variables are "x" and "carname".

You can also declare variables by using its name in a script. Like this:

carname=some value

Now you have also created a variable. The name of the variable is "carname". However, this method is not a good practice, because you can misspell the variable name later in your script, and that can cause strange results when your script is running.

If you misspell for example the "carname" variable to "carnime", the script will automatically create a new variable called "carnime". To prevent your script from doing this, you can use the Option Explicit statement. This statement forces you to declare all your variables with the dim, public or private statement.

Put the Option Explicit statement on the top of your script. Like this:

option explicit
dim carname
carname=some value


--------------------------------------------------------------------------------

Assigning Values to Variables
You assign a value to a variable like this:

carname="Volvo"
x=10

The variable name is on the left side of the expression and the value you want to assign to the variable is on the right. Now the variable "carname" has the value of "Volvo", and the variable "x" has the value of "10".


--------------------------------------------------------------------------------

Lifetime of Variables
How long a variable exists is its lifetime.

When you declare a variable within a procedure, the variable can only be accessed within that procedure. When the procedure exits, the variable is destroyed. These variables are called local variables. You can have local variables with the same name in different procedures, because each is recognized only by the procedure in which it is declared.

If you declare a variable outside a procedure, all the procedures on your page can access it. The lifetime of these variables starts when they are declared, and ends when the page is closed.


--------------------------------------------------------------------------------

VBScript Array Variables
An array variable is used to store multiple values in a single variable.

In the following example, an array containing 3 elements is declared:

dim names(2)

The number shown in the parentheses is 2. We start at zero so this array contains 3 elements. This is a fixed-size array. You assign data to each of the elements of the array like this:

names(0)="Tove"
names(1)="Jani"
names(2)="Stale"


Similarly, the data can be retrieved from any element using the index of the particular array element you want. Like this:

mother=names(0)

You can have up to 60 dimensions in an array. Multiple dimensions are declared by separating the numbers in the parentheses with commas. Here we have a two-dimensional array consisting of 10 rows and 6 columns:

Dim x(9,5),z,y
For z = 0 to 9
For y = 0 to 5
x(z,y) = "Row:" & z & space(1) & "Col:" & y
Next
Next

Select Case Statement in VB Script of QTP?

Executes one of several groups of statements, depending on the value of an expression.

Select Case testexpression
[Case expressionlist-n
[statements-n]] . . .
[Case Else expressionlist-n
[elsestatements-n]]
End Select

Arguments
testexpression
Any numeric or string expression.
expressionlist-n
Required if Case appears. Delimited list of one or more expressions.
statements-n
One or more statements executed if testexpression matches any part of expressionlist-n.
elsestatements-n
One or more statements executed if testexpression doesn't match any of the Case clauses.
Remarks
If testexpression matches any Case expressionlist expression, the statements following that Case clause are executed up to the next Case clause, or for the last clause, up to End Select. Control then passes to the statement following End Select. If testexpression matches an expressionlist expression in more than one Case clause, only the statements following the first match are executed.

The Case Else clause is used to indicate the elsestatements to be executed if no match is found between the testexpression and an expressionlist in any of the other Case selections. Although not required, it is a good idea to have a Case Else statement in your Select Case block to handle unforeseen testexpression values. If no Case expressionlist matches testexpression and there is no Case Else statement, execution continues at the statement following End Select.

Select Case statements can be nested. Each nested Select Case statement must have a matching End Select statement.

The following example illustrates the use of the Select Case statement.

Dim arr(5)
arr(0) = "The"
arr(1) = "World"
arr(2) = "is"
arr(3) = "not"
arr(4) = "good"
arr(5) = "enough"
i=inputbox("enter value for i")

Select Case i
Case 0 :
msgbox arr(0)
Case 1 :
msgbox arr(1)
Case 2 :
msgbox arr(2)
Case 3 :
msgbox arr(3)
Case 4 :
msgbox arr(4)
Case 5 :
msgbox arr(5)
case else :
msgbox "i come if there is no match for your input"

End Select

"on error resume next" or Recovery Scenarios ?

If you can predict that a certain event may happen at a specific point in your test or component, it is recommended to handle that event directly within your test or component by adding steps such as If statements or optional steps or "on error resume next", rather than depending on a recovery scenario. Using Recovery Scenarios may result in unusually slow performance of your tests.They are designed to handle a more generic set of unpredictable events which CANNOT be handled programmatically.

Difference between call by value and call by reference?

ByVal
Indicates that the argument is passed by value.
ByRef
Indicates that the argument is passed by reference.

Call Statement
Transfers control to a Sub or Function procedure.

[Call] name [argumentlist]
Arguments
Call
Optional keyword. If specified, you must enclose argumentlist in

parentheses. For example:
Call MyProc(0)

name
Required. Name of the procedure to call.
argumentlist
Optional. Comma-delimited list of variables, arrays, or expressions to

pass to the procedure.


Remarks
You are not required to use the Call keyword when calling a procedure.

However, if you use the Call keyword to call a procedure that requires

arguments, argumentlist must be enclosed in parentheses. If you omit

the Call keyword, you also must omit the parentheses around

argumentlist. If you use either Call syntax to call any intrinsic or

user-defined function, the function's return value is discarded.




var="Hello World"
Call MyFunction(var)
msgbox var
Function MyFunction(byval text)
text = "gopi"
End Function
'output of the above pgm is "Hello World"


var="Hello World"
Call MyFunction(var)
msgbox var
Function MyFunction(byref text)
text = "World is Changed"
End Function
'output of the above pgm is "World is Changed"


Passing by Value is just that. It passes a value to the function to use. It creates a new space to hold this value.

Passing by Reference passes the pointer to the memory space for the variable. This means that anything you do to the variable in the function changes the actual value of the passed variable. This means that if you changed the variable in the function it will be changed outside the function too.

Friday, July 17, 2009

Working with Associated Function Libraries

In QuickTest, you can create function libraries containing functions, subroutines, modules, and so forth, and then associate the files with your test. This enables you to insert a call to a public function or subroutine in the associated function library from that test. (Public functions stored in function libraries can be called from any associated test, whereas private functions can be called only from within the same function library.)

If a test can no longer access a function that was used in a step (for example, if the function was deleted from the associated function library), the icon is displayed adjacent to the step in the Keyword View. When you run the test, an error will occur when it reaches the step using the nonexistent function.

Note: Any text file written in standard VBScript syntax can be used as a function library.

You can specify the default function libraries for all new tests in the Test Settings dialog box (File > Settings > Resources tab). After a test is created, the list of default function libraries is integrated into the test. Therefore any changes to the default function libraries list in the Test Settings dialog box do not affect existing tests.

You can edit the list of associated function libraries for an existing test in the Test Settings dialog box. For more information, see Defining Resource Settings for Your Test.

Notes:

In addition to the functions available in the associated function libraries, you can also call a function contained in any function library (or VBscript file) directly from any action using the ExecuteFile function. You can also insert ExecuteFile statements within an associated function library. For more information, see Executing Externally-Defined Functions from Your Test.
You cannot debug a file that is called using an ExecuteFile statement, or any of the functions contained in the file. In addition, when debugging a test that contains an ExecuteFile statement, the execution marker may not be correctly displayed



Library files in QTP are used to declare functions that are needed to be used across actions

A Library File included on the Resource tab are placed in the test’s Global namespace at runtime, and are therefore shared and accessible by all Actions included in the test.

Including a library in a test Action, using the ExecuteFile(“libname”) QTP statement. Libraries included in this manner are placed in the including Action’s namespace, and therefore may only be used by the including Action’s code.


It is also possible to use one or more ExecuteFile(“libname”) statements within a library included via the Test Settings Resource tab as well. In this scenario all library files are placed in the test’s Global namespace.

And also a library file can be included from External VBA file directly by using QTA APPLICATION object.

Most frequent http error codes we see while testing

http error code :400
Its Client Error Code..The request was denied due to a syntax error in the request.

http error code :404
The document that has been requested either no longer exists, or has never existed on the server.

http error code :503
Its Server Error Code.The server is overloaded or down for maintenance and due to this was unable to process the client request.

http error code :502
Server Overload.Bad Gateway; a server being used by this Web server has sent an invalid response.

http error code :200
This is the normal response where a page has been successfully fetched