How to test a Restful service which returns a JSON Response using UFT? - hp-uft

I have been using QTP 11 and we have had to migrate to UFT 12.02 now.
We have some REST services which have come for testing and problem is that it gives a JSON Response when done manually. I have not worked on C# before so is there any way I can configure this service using UFT API and validate the response using VB script which I am accustomed to.
I tried the following:
Dim lib, url, xmlHttp, json, JsonConvert, xmlDoc
lib = "C:\Bin\Net35\Newtonsoft.Json.dll"
url = "http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo"
Set xmlHttp = CreateObject("Microsoft.XMLHTTP")
xmlHttp.open "POST", url, False
xmlHttp.send
json = "{ ""?xml"": { ""#version"": ""1.0"", ""#standalone"": ""no"" }, ""root"":" & xmlHttp.responseText & "}"
Set JsonConvert = DOTNetFactory.CreateInstance("Newtonsoft.Json.JsonConvert", lib)
Set xmlDoc = JsonConvert.DeserializeXmlNode(json)
MsgBox xmlDoc.InnerXml
But I do not have the dll available with me, I am unable to test the URL with this.
If this is the correct way then can anyone let me know how can I get this dll installed.

UFT internally uses Newtonsoft.Json framework (like many other HPE products), so it already includes such DLL.
You just need to update the path to point to UFT\bin directory, which by default is:
lib = "C:\Program Files (x86)\HP\Unified Functional Testing\bin\Newtonsoft.Json.dll"
Now, from API testing perspective, you can use checkpoints to validate the results. See Image

Related

How to retrieve URL dynamically?

I am using a webservice in my project. I am giving URL like this:
WebServicename.asmx/Methodname
Locally I used webservice in VBScript and it is running good. But when I deployed same code in a server the webservice is not running. So before that webservice I want to give full URL. So for that reason I want to get that URL dynamically and add before of my URL.
I am writing Vb script and I call webservice like this
Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.3.0")
Set oXMLDoc = CreateObject("MSXML2.DOMDocument")
'Msgbox("Calling WebService To Approve Leave")
strEnvelope ="varCarrierType="&varCarrierType &varCarrierID& "&Filename="&Filename& "&varC22Cnt="&varC22Cnt&"&varCurrentDateTime="&varCurrentDateTime&"&varTotalCommissionAmt="&varTotalCommissionAmt&"&vpath="&vpath
oXMLHTTP.onreadystatechange = getRef("HandleStateChange")
call oXMLHTTP.open("POST","/VBScriptService.asmx/InsertDBDetails",False)
call oXMLHTTP.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
call oXMLHTTP.send(strEnvelope)
MsgBox "COM File successfully generated from the Template given. Verify the output file in " & vpath,0, "Com File Generation"
MsgBox "Page About to Refresh, Click ok to proceed", 0, "ComPage Refresh"
Set wShell=CreateObject("WScript.Shell")
wShell.SendKeys "{F5}"

XMLHTTP-Request (AJAX) with VBSkript does not work with proxy connection

I'm using a XMLHTTP-Request to get data from a website to work with them in VBS. This works great with a default internet connection. But currently I've to deal with a customer, which uses a "proxy" connection.
Unfortunately I'm not very familiar to different proxy-solutions. The behavior is: I'm opening the browser and I'll get a authorisation dialog (username/password). After entering username/password it can be accessed to all websites. If I close the browser and start it again, the autorisation dialog appears again.
Here a little bit of code. I'm working with a "better notepad", so I can't see other object properties:
Set XMLHTTP = CreateObject("Microsoft.XMLHTTP")
XMLHTTP.Open "GET", "http://www.mywebsite.de/getData.php" ,0
XMLHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
XMLHTTP.Send("content=1")
MsgBox(XMLHTTP.responseText)
If I execute this code on the machine with the forced proxy, I'll get a "access denied" error immediately.
How can I add proxy authorisation support here? If indeed such a thing is possible ...
Thank you!
I've found the solution: I have to change the class, because Microsoft.XMLHTTP does not support proxy configurations:
set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.4.0")
xmlhttp.Open "POST","http://www.yourwebsite.com",false
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.setProxy 2, "192.168.0.222:8080"
xmlhttp.setProxyCredentials "your_username" , "password_for_username"
xmlhttp.send
MsgBox(xmlhttp.responseText)
Notice some important details:
set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.4.0")
setProxy and setProxyCredentials methods requires version number:
It's important to use a version number at the end of the parameter. If I don't use a version number, it will look for an old version of the class without the setProyx and setProxyCredentials methods. An error message appears ("object does not have the method ...").
Version number isn't trivial:
But the version number itself isn't trivial as well. In my local enviroment (W7x64) I'll have to take version number "4.0", but at my client's server it doesn't work ("could not create an object ..."). I'll have to take version number "6.0". If you're not sure, which version is installed, you can look it up in the registry, just search for "MSXML2.ServerXMLHTTP". In my case there are various keys with the right version number:

Visual Studio Express 2012 - Windows Service VB.net - Access URL on timer

I'm trying to create a windows service in VB.Net using visual studio express 2012. Essentially all I want is for the service to sent a HTTP GET request to a pre-determined URL every N minutes. Kind of like a heart beat, so we know that the server is running and online.
I've got as far as creating the windows server, building the project to an .EXE and then using InstallUtil to install it as a service. This appears to be working, I following the tutorial here: http://www.dotheweb.net/2009/11/creating-services-with-vb-express/
I removed some of the code from the tutorial which writes to the windows system logs (or rather I think it creates a new log) as that doesn't work with my version of Visual Studio for some reason.
I am using the following code to send the HTTP request:
Dim url As String = "http://someURL.com/test.php"
Dim request As WebRequest = WebRequest.Create(url)
request.Method = "GET"
Dim response As WebResponse = request.GetResponse()
The PHP file simply sends me an email when it is accessed.
The code works fine when I run the project from within visual studio (if I ignore the message informing me a windows service project should not be run like this and leave it going I do start to get emails).
However, when I fire up the windows service itself I don't get any emails however I don't get any errors appearing anywhere either.
My guess is you are using a System.Windows.Forms.Timer . That timer will not work without a System.Windows.Forms.Form . The timer to use in a windows service is System.Timers.Timer
Declare it in your Service Class
Private WithEvents m_timer As System.Timers.Timer
Start it in the OnStart method:
m_timer = New System.Timers.Timer(300000) ' 5 minutes
m_timer.Enabled = True
And handle the Elapsed event of the timer
Private Sub m_timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles m_timer.Elapsed
m_timer.Enabled = False
Dim url As String = "http://someURL.com/test.php"
Dim request As WebRequest = WebRequest.Create(url)
request.Method = "GET"
Dim response As WebResponse = request.GetResponse()
m_timer.Enabled = True
End Sub
Don't forget to stop the timer in the OnStop method.

Read out Ajax response from XMLHTTP using simple VBScript

I am trying to download a set of websites using a script:
dim xmlhttp : set xmlhttp = createobject("Msxml2.ServerXMLHTTP")
xmlhttp.open "GET", "http://www.website.com/tags/search-term?page=1, false
xmlhttp.send
text= xmlhttp.ResponseText
Firebug tells me the response is in DOM "_result". But how can I read this out? I know this is a very stupid question, but I just need to run a lot of automated searches for a research project and am struggling with implementing this.
Just read the response from your object like so
response=xmlhttp.responseText

Lotus Domino Designer: AJAX or some other way to open a URL to retrieve a data

Following Lotus Domino Designer: Add a button in the default calendar
The link is actually a SSO link (single sign-on) which is delivered by our API to connect the user to our application.
We must execute the url http://example.com/api.asp?cas=GETUSERTOKEN&rdn=9428521&login=mike&logincnx=adminAPIHays&pwdcnx=app
which returns the time-limited SSO link http://example.com/hays/array.asp?key=750EA68A476C9F8D26F9704B203205FDF64D2B849B688F169B398D217FFF570F54C1CB6B4635A494E504E624EDF3266D0D5C2A1210AA43EE3CD2098AE8B42DBF which connects the user.
By what means I can program this in Domino Designer?
If necessary we can put the button in a new something (not necessarily in the default calendar in fact).
Because you want to obtain another URL from an initial URL request, you will need to use a java agent to open a URL connection, and process the response to determine. The best approach is to use the J2SE URLConnection class. Once you're in a java agent, you can capture responses and store them in Documents.
If you need to do something in the UI after receiving the response, then you'll need to call the Java Agent from within a LotusScript function either from the view action or a form button. But if this is not required, you can also call the Java agent directly in view actions and form buttons. Java Agents have no UI output apart from the Java Debug Console. In Domino circles, Bob Balaban is a guru and has recently started to revise his Java in Domino writings. It provides a good insight into how it "hangs together" with the Domino API.
There are numerous examples scattered across the internet about using Java in Domino. IBM has published information on Java Agents in Domino. Some good examples can be found here, here, and here. The latest version of Domino Designer runs under Eclipse, so this may be of help to.
I did this in LotusScript:
Sub Click(Source As Button)
Dim objHttp As Variant
Dim url As String
Dim response As String
Set objHttp = CreateObject("Microsoft.XMLHTTP")
url = "http://foo.com/api.asp?cas=GETUSERTOKEN&rdn=9428521&login=mike&logincnx=adminAPIHays&pwdcnx=app"
objHttp.open "GET", url, False
objHttp.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
objHttp.send
newUrl = objHttp.responseText
Set w = New NotesUIWorkspace
w.UrlOpen newUrl
End Sub

Resources