Tool Independent Method to Automate Web Services

 

There are various methods to automate and validate web services. Here we will going to see Web Services automation using Microsoft XML HTTP object model (Refer https://msdn.microsoft.com/en-us/library/ms535874%28v=vs.85%29.aspx) for more details on the same.

This Microsoft XML HTTP object model can be used to automate web services using any of automation tools. Here in this article we have created various generic reusable functions that performs various operations on web services.

1. Function to Retrieve Whether Service API Response is Successfully generated or not
‘+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*

Public Function ValidateServiceAPI(ByVal svcAPIName, ByVal svcAPIURL, ByVal svcToken, ByVal requestHeaderPrefix)
    Set oReq = CreateObject(“Microsoft.XMLHTTP”)
    oReq.Open “GET”, svcAPIURL,False
    oReq.SetRequestHeader “Authorization”, requestHeaderPrefix & ” ” & svcToken
    oReq.Send
    iServiceStatusCode = oReq.Status
   
    If iServiceStatusCode = 200 Then
        Reporter.ReportEvent micPass, svcAPIName & ” API Configuration Service – Success”,svcAPIName & ” API Configuration Service Success with Status Code – ” & iServiceStatusCode
        ValidateServiceAPI = True
    Else
        Reporter.ReportEvent micFail, svcAPIName & ” API Configuration Service – Failure”, svcAPIName & ” API Configuration Service Failure with Status Code – ” & iServiceStatusCode
        ValidateServiceAPI = False
    End If
    Set oReq = Nothing
End Function

 

2. Function to Retrieve Service API Response
‘+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*

Public Function GetServiceAPIResponse(ByVal svcAPIName, ByVal svcAPIURL, ByVal svcToken, ByVal requestHeaderPrefix)
    Set oReq = CreateObject(“Microsoft.XMLHTTP”)
    oReq.Open “GET”, svcAPIURL,False
    oReq.SetRequestHeader “Authorization”, requestHeaderPrefix & ” ” & svcToken
    oReq.Send
    iServiceStatusCode = oReq.Status
    sServiceAPIResponse = oReq.ResponseText
   
    If iServiceStatusCode = 200 Then
        GetServiceAPIResponse = sServiceAPIResponse
        Reporter.ReportEvent micPass, svcAPIName & ” API Configuration – Success”,svcAPIName & ” API Configuration Service Success – ” & iServiceStatusCode & vbNewLine & “API Response Code – ” & sServiceAPIResponse
    Else
        GetServiceAPIResponse = sServiceAPIResponse
        Reporter.ReportEvent micFail, svcAPIName & ” API Configuration – Failure”, svcAPIName & ” API Configuration Service Failure – ” & iServiceStatusCode & vbNewLine & “API Response Code – ” & sServiceAPIResponse
    End If
    Set oReq = Nothing
End Function

Advertisements
By shalabhdixit

Connecting to a Database Using QTP

The below function can be used to connect to any database. The function establishes DSN Connection. Below are more details:

‘1. strConName – Name of Database connection
‘2. strDSNName – DSN (Datasource Name)
‘3. strServerName – Name of Database Server
‘4. strDataBaseName – Name of Database
‘5. strUserID – Database ID
‘6. strPassword- Database Password

Function ConnectDatabase(Byval strConName,Byval strDSNName,Byval strServerName,Byval strDataBaseName,Byval strUserID,Byval strPassword)
MsgBox "Inside 'ConnectDatabase' Function"
On Error Resume Next
'Assign Database Connection Information to the DB Variables
DataSource = strDSNName
Server = strServerName
UserID_DB = strUserID
Password_DB = strPassword
DatabaseName_DB = strDataBaseName
con=strConName
Dim connItems(2)
Dim connState
'Declare Database Recordset variable
conString = cstr("DSN="&DataSource&chr(59)&"Server="&Server&chr(59)&"UID="&UserID_DB&chr(59)&"Password="&Password_DB&chr(59)&"DATABASE="&DatabaseName_DB)
'Create Database Connection Object
Set conn = CreateObject("Adodb.Connection")
'Open Database Connection
conn.Open conString
'Verify the Successful Database Connection Establishment
If conn.State=1 Then
Reporter.ReportEvent micPass,"Database Connection Success"," Database connection for "&DatabaseName_DB &"Database has been established Successfully"
'MsgBox "Connection Estblished Successfully"
Else
Reporter.ReportEvent micFail,"Database Connection Failure"," Database connection for "&DatabaseName_DB &"Database is not been established successfully"
'MsgBox "Connection Not Estblished"
End If
connItems(0) = conn.State
connItems(1) = conn
ConnectDatabase = connItems
End Function
'*****************************************************************************************

By shalabhdixit

Automating a N languages website using single QTP Script – The Realtime usage of Repository Collection Object

You might face a situation during automation where you given a website which can be accessed in say 10 different languages. Now the question that arises here is how should you proceed to automate it. Many of us would think to create 10 different scripts for each language isn’t it..!! :).
But the same can be implemented using one script only. How.? See below for details:
QTP provides collection object called Repository Collection that enables you to programmatically associate or attach the Object Repository dynamically at run-time during test run. So to automate the website in different languages, we can simply create 10 object repositories and associate them either one by one or one Iteration of test run.
We can parametrize the object-repositories path either through Data-Table or using files etc and test the application easily. i.e.
Parameterzing Object Repository Path
One way to do is adding all the object repositories paths into datatable as shown below.
repos_Path = DataTable.Value("Repo_Path","Global")

Associating Object Repository
The Add method is used to add an object repository file to the specified index position in the run-time collection of shared object repository files associated with the current action.

RepositoriesCollection.Add(repos_Path)

Complete Script
''===========================================================================
'Get Total Number of Shared Repositories Associated with the current action
x = RepositoriesCollection.Count
Print "Total Repositories Associated With " & Environment("ActionName") & " = " & x
'Checkpoint to Add/Associate Shared Repositories if no repos is associated
If x = 0 Then
'Parameterizing Repository Path
repos_Path = DataTable.Value("Repo_Path","Global")
'Associating OR
RepositoriesCollection.Add repos_Path
End If

‘Get Total Updated Number of Shared Repositories Associated with the current action
x = RepositoriesCollection.Count
Print x
”===========================================================================

By shalabhdixit

VB Script Regular Expressions Utility Functions

”=====================================================================================================================================================================
”’Function to check if a string matches a Regular Expression Pattern
”=====================================================================================================================================================================
Function IsRegExMatch(strText, regEx_Pattern, blnMatchCompleteString)
Dim oRegExp, retVal
‘Create regular expression object.
Set oRegExp = New RegExp
‘Set pattern.
oRegExp.Pattern = regEx_Pattern
‘Ignore case
oRegExp.IgnoreCase = True
‘Match complete string or not
oRegExp.Global = blnMatchCompleteString
‘Test the regular expression
IsRegExMatch = oRegExp.Test(strText)
Set oRegExp = Nothing
End Function

”=====================================================================================================================================================================
”’Function to Match a Regular Expression and Replace with a particular text if a string matches a regular expression pattern.
”=====================================================================================================================================================================
Function RegExMatchandReplace(strText,regEx_Pattern,strReplacementText,blnMatchCompleteString)
Dim oRegExp, retVal,strReplacedText

‘ Create regular expression object.
Set oRegExp = New RegExp

‘Set pattern.
oRegExp.Pattern = regEx_Pattern
‘Ignore case
oRegExp.IgnoreCase = True
‘Match complete string or not
oRegExp.Global = blnMatchCompleteString

‘Test the regular expression
RegExMatch = oRegExp.Test(strText)
If RegExMatch = True Then
strReplacedText = oRegExp.Replace(strText,strReplacementText)
End If
RegExMatchandReplace = strReplacedText
Set oRegExp = Nothing
End Function

”=====================================================================================================================================================================
”’Function to Get Total Number of Matches of a Regular Expression Pattern in a particular string
”=====================================================================================================================================================================
Function GetRegExMatchCount(strText, regEx_Pattern, blnMatchCompleteString)
Dim oRegExp, retVal
‘Create regular expression object.
Set oRegExp = New RegExp

‘Set pattern.
oRegExp.Pattern = regEx_Pattern
‘Ignore case
oRegExp.IgnoreCase = True
‘Match complete string or not
oRegExp.Global = blnMatchCompleteString

‘Test the regular expression
RegExpMatch = oRegExp.Test(strText)
If RegExpMatch Then
Set oRegExMatchCount = oRegExp.Execute(strText)
GetRegExMatchCount = oRegExMatchCount.Count
End If
Set oRegExp = Nothing
End Function

”=====================================================================================================================================================================
”’Function to Trim white space characters from end of a string
”=====================================================================================================================================================================
Public Function RTrimW(ByVal Text)
‘String pattern ending with carriage return, tab, new line or a space character
RTrimPattern = “[\s]*$”

Dim oRegExp
Set oRegExp = New RegExp
oRegExp.Pattern =RTrimPattern
oRegExp.Global = False
RTrimW = oRegExp.Replace(Text,””)
Set oRegExp = Nothing
End Function

”=====================================================================================================================================================================
”’Function to Trim white space characters from start of a string
”=====================================================================================================================================================================
Public Function LTrimW(ByVal Text)
‘String pattern ending with carriage return, tab, new line or a space character
LTrimPattern = “^[\s]*”
Dim oRegExp
Set oRegExp = New RegExp
oRegExp.Pattern = LTrimPattern
oRegExp.Global = False
LTrimW = oRegExp.Replace(Text,””)
Set oRegExp = Nothing
End Function

”=====================================================================================================================================================================
”Function to Trim white space characters from both start and end of a string
”=====================================================================================================================================================================
Public Function TrimW(ByVal Text)
TrimW = LTrimW(RTrimW(Text))
End Function

By shalabhdixit