Featured Posts

[Travel][feat1]

How To Convert JSON File To XML format In UFT in VBScript way

April 02, 2018
image credit:https://www.pexels.com/photo/
auto-automobile-car-chrome-461824/
So long, we were testing webservices using XM format. Traditional frameworks are written is such a way that it can handle XML request and response.The current framework was working fine until the recent upgradation of few services to JSON. As the request type is JSON. The response also what we get is JSON. In few cases, the input file is XML, but output is pure JSON. Tests are failing due to this format change. The possible solution is to reach out to developer to get an XML response. The developer may or may not agree on this aspect as we are moving forward towards JSON. The another solution is to upgrade our framework to handle JSON response.

To get a solution, I started googling about the topic.There were many solution given but not in VBScript. So it was very tough to handle the JSON format. So I thought to design a parser on my own.
While further searching in stackoverflow , I found a reply from Stephen Quan. he has come up with an excellent lightweight pure VBscript way to parse JSON to XML.
His solution is given below:

Function JSONtoXML(jsonText)
  Dim idx, max, ch, mode, xmldom, xmlelem, xmlchild, name, value

  Set xmldom = CreateObject("Microsoft.XMLDOM")
  xmldom.loadXML "<xml/>"
  Set xmlelem = xmldom.documentElement

  max = Len(jsonText)
  mode = 0
  name = ""
  value = ""
  While idx < max
    idx = idx + 1
    ch = Mid(jsonText, idx, 1)
    Select Case mode
    Case 0 ' Wait for Tag Root
      Select Case ch
      Case "{"
        mode = 1
      End Select
    Case 1 ' Wait for Attribute/Tag Name
      Select Case ch
      Case """"
        name = ""
        mode = 2
      Case "{"
        Set xmlchild = xmldom.createElement("tag")
        xmlelem.appendChild xmlchild
        xmlelem.appendchild xmldom.createTextNode(vbCrLf)
        xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
        Set xmlelem = xmlchild
      Case "["
        Set xmlchild = xmldom.createElement("tag")
        xmlelem.appendChild xmlchild
        xmlelem.appendchild xmldom.createTextNode(vbCrLf)
        xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
        Set xmlelem = xmlchild
      Case "}"
        Set xmlelem = xmlelem.parentNode
      Case "]"
        Set xmlelem = xmlelem.parentNode
      End Select
    Case 2 ' Get Attribute/Tag Name
      Select Case ch
      Case """"
        mode = 3
      Case Else
        name = name + ch
      End Select
    Case 3 ' Wait for colon
      Select Case ch
      Case ":"
        mode = 4
      End Select
    Case 4 ' Wait for Attribute value or Tag contents
      Select Case ch
      Case "["
        Set xmlchild = xmldom.createElement(name)
        xmlelem.appendChild xmlchild
        xmlelem.appendchild xmldom.createTextNode(vbCrLf)
        xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
        Set xmlelem = xmlchild
        name = ""
        mode = 1
      Case "{"
        Set xmlchild = xmldom.createElement(name)
        xmlelem.appendChild xmlchild
        xmlelem.appendchild xmldom.createTextNode(vbCrLf)
        xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
        Set xmlelem = xmlchild
        name = ""
        mode = 1
      Case """"
        value = ""
        mode = 5
      Case " "
      Case Chr(9)
      Case Chr(10)
      Case Chr(13)
      Case Else
        value = ch
        mode = 7
      End Select
    Case 5
      Select Case ch
      Case """"
        xmlelem.setAttribute name, value
        mode = 1
      Case "\"
        mode = 6
      Case Else
        value = value + ch
      End Select
    Case 6
      value = value + ch
      mode = 5
    Case 7
      If Instr("}], " & Chr(9) & vbCr & vbLf, ch) = 0 Then
        value = value + ch
      Else
        xmlelem.setAttribute name, value
        mode = 1
        Select Case ch
        Case "}"
          Set xmlelem = xmlelem.parentNode
        Case "]"
          Set xmlelem = xmlelem.parentNode
        End Select
      End If
    End Select
  Wend

  Set JSONtoXML = xmlDom
End Function
His working principle was o walk through JSON String and turn that to Microsoft XML DOM document.From there he used Microsoft XML API including xpath queries to pluck ot any values.This solution was working for a welformatted JSON file. So the modification , I have done further:
Step-1: I made sure the response is stored in a file called Response.JSON from the service.
Step-2: Implement the below code to read the file:

responseFilePath="C:\Test\Response.JSON"
// this is the path from where we need to read the file.
OutputPath="C:\Test\"
// This path is where we will store the output XML file.
Set objFso=CreateObject("Scripting.FileSystemObject")
Set ObjTextFile=objFso.openTextFile(responseFilePath,1)
sContent=ObjTextFile.ReadAll();
// this is to read the whole file at one go.
Step-3:Modify his code

Function JSONtoXML(jsonText,OutputPath)
  Dim idx, max, ch, mode, xmldom, xmlelem, xmlchild, name, value

  Set xmldom = CreateObject("Microsoft.XMLDOM")
  xmldom.loadXML "<xml/>"
  Set xmlelem = xmldom.documentElement

  max = Len(jsonText)
  mode = 0
  name = ""
  value = ""
  While idx < max
    idx = idx + 1
    ch = Mid(jsonText, idx, 1)
    Select Case mode
    Case 0 ' Wait for Tag Root
      Select Case ch
      Case "{"
        mode = 1
      End Select
    Case 1 ' Wait for Attribute/Tag Name
      Select Case ch
      Case """"
        name = ""
        mode = 2
      Case "{"
        Set xmlchild = xmldom.createElement("tag")
        xmlelem.appendChild xmlchild
        xmlelem.appendchild xmldom.createTextNode(vbCrLf)
        xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
        Set xmlelem = xmlchild
      Case "["
        Set xmlchild = xmldom.createElement("tag")
        xmlelem.appendChild xmlchild
        xmlelem.appendchild xmldom.createTextNode(vbCrLf)
        xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
        Set xmlelem = xmlchild
      Case "}"
        Set xmlelem = xmlelem.parentNode
      Case "]"
        Set xmlelem = xmlelem.parentNode
      End Select
    Case 2 ' Get Attribute/Tag Name
      Select Case ch
      Case """"
        mode = 3
      Case Else
        name = name + ch
      End Select
    Case 3 ' Wait for colon
      Select Case ch
      Case ":"
        mode = 4
      End Select
    Case 4 ' Wait for Attribute value or Tag contents
      Select Case ch
      Case "["
        Set xmlchild = xmldom.createElement(name)
        xmlelem.appendChild xmlchild
        xmlelem.appendchild xmldom.createTextNode(vbCrLf)
        xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
        Set xmlelem = xmlchild
        name = ""
        mode = 1
      Case "{"
        Set xmlchild = xmldom.createElement(name)
        xmlelem.appendChild xmlchild
        xmlelem.appendchild xmldom.createTextNode(vbCrLf)
        xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
        Set xmlelem = xmlchild
        name = ""
        mode = 1
      Case """"
        value = ""
        mode = 5
      Case " "
      Case Chr(9)
      Case Chr(10)
      Case Chr(13)
      Case Else
        value = ch
        mode = 7
      End Select
    Case 5
      Select Case ch
      Case """"
        xmlelem.setAttribute name, value
        mode = 1
      Case "\"
        mode = 6
      Case Else
        value = value + ch
      End Select
    Case 6
      value = value + ch
      mode = 5
    Case 7
      If Instr("}], " & Chr(9) & vbCr & vbLf, ch) = 0 Then
        value = value + ch
      Else
        xmlelem.setAttribute name, value
        mode = 1
        Select Case ch
        Case "}"
          Set xmlelem = xmlelem.parentNode
        Case "]"
          Set xmlelem = xmlelem.parentNode
        End Select
      End If
    End Select
  Wend

  xmlDom.Save OutputPath&"Response.xml"
End Function
How To Convert JSON File To XML format In UFT in VBScript way How To Convert JSON File To XML format In UFT in  VBScript way Reviewed by Animesh on April 02, 2018 Rating: 5

How To Decrypt an URL or String In VbScript or UFT

April 02, 2018
For encryption and decryption the best  options to avail to go to online site and transform what we need.
Few good sites could be as follows:
https://www.urldecoder.org/
https://meyerweb.com/eric/tools/dencoder/
https://urldecode.org/
https://codebeautify.org/url-decode-string and for base 64 decoding https://www.base64decode.org/
But I tried to understand what was th elogic behind it:
I searched google and found one link in StackOverflow.
https://stackoverflow.com/questions/4998715/does-vba-have-any-built-in-url-decoding
Mitch Wheat's Solution

Public Function URLDecode(ByVal strEncodedURL As String) As String
   Dim str As String
   str = strEncodedURL 
   If Len(str) > 0 Then
      str = Replace(str, "&amp", " & ")
      str = Replace(str, "&#03", Chr(39))
      str = Replace(str, "&quo", Chr(34))
      str = Replace(str, "+", " ")
      str = Replace(str, "%2A", "*")
      str = Replace(str, "%40", "@")
      str = Replace(str, "%2D", "-")
      str = Replace(str, "%5F", "_")
      str = Replace(str, "%2B", "+")
      str = Replace(str, "%2E", ".")
      str = Replace(str, "%2F", "/")

      URLDecode = str
  End If

End Function

This code was not perfect the writer of the code has given some idea how to do it. He has also mentioned one link for better approach- http://www.freevbcode.com/ShowCode.asp?ID=1512. But this is in VB. My objective is to get the same in VBscript so that I can work with UFT.

Markus Diersbock' solution:



Public Function URLDecode(sEncodedURL As String) As String

On Error GoTo Catch

Dim iLoop   As Integer
Dim sRtn    As String
Dim sTmp    As String

If Len(sEncodedURL) > 0 Then
    ' Loop through each char
    For iLoop = 1 To Len(sEncodedURL)
        sTmp = Mid(sEncodedURL, iLoop, 1)
        sTmp = Replace(sTmp, "+", " ")
        ' If char is % then get next two chars
        ' and convert from HEX to decimal
        If sTmp = "%" and LEN(sEncodedURL) + 1 > iLoop + 2 Then
            sTmp = Mid(sEncodedURL, iLoop + 1, 2)
            sTmp = Chr(CDec("&H" & sTmp))
            ' Increment loop by 2
            iLoop = iLoop + 2
        End If
        sRtn = sRtn & sTmp
    Next
    URLDecode = sRtn
End If

Finally:
    Exit Function
Catch:
    URLDecode = ""
    Resume Finally
End Function
But these solutions were not enough. I read in wiki about Percent encoding -
and the RFC 3986. I have also gone through the reserve character after percent encoding. When I was developing the same I found one more site with exactly the solution I was looking for: MarcusJT has provided the exact solution to implement the decode functionality in Vbscript. The code he has used is written below:

Function URLDecode(str)
str = unescape(str)
str = replace(str,"+"," ")
str = replace(str,"%2A","*")
str = replace(str,"%40","@")
str = replace(str,"%2D","-")
str = replace(str,"%5F","_")
str = replace(str,"%2B","+")
str = replace(str,"%2E",".")
str = replace(str,"%2F","/")
URLDecode = str
End Function

As per him, this functionality was given in Microsoft's Netscape JS in the form of escape() and unescape() function. There is a superior version available called server.URLEncode. VBscript's decode functionality is full of flaws. But this code served most of my URL or string decoding.
How To Decrypt an URL or String In VbScript or UFT How To Decrypt an URL or String In VbScript or UFT Reviewed by Animesh on April 02, 2018 Rating: 5

How to Call Reusable API Script From GUI Script in UFT

March 21, 2018
In my previous post -How To Configure SharePoint's Services In UFT ? 
I have shown to to configure a web service in UFT. In this post we are going to see how to call them from other script.



Step-1:
Go to File-->New-->Test-->GUI Test-->Provide Name of the GUI test-->Provide location of the Test-->Click on Create button -->Double click on Action

Step-2
Right Click on the Action-->Select Action-->Choose the last option (as per UFT 12.2) as Call to Existing API Test/Action

Step-3
In the new window click on the browse button to get the API test-->Call to -->Select <Entire Test >
In the parameter List, we can see it is asking for the parameters required for the API Test. In this case it is asking for ListName and FileName (These are the parameters we created during Start)

Step-4
Provide some value to each of the parameters
like- listName=ListName
fileName=FileName
Click on ok

Step-5
In GUI Action we can get the entry-

RunAPITest "MyAPI","ListName","FileName"
Step-6
Now we can give actual listName and FileName and execute.

for ListName you can follow my other blogpost-How to Get List Name of a SharePoint link in UFT
How to Call Reusable API Script From GUI Script in UFT How to Call Reusable API Script From GUI Script in UFT Reviewed by Animesh Chatterjee on March 21, 2018 Rating: 5

How To Configure SharePoint's Services In UFT ?

March 21, 2018
It all started when I tried to work with SharePoint webservices. In this post in stackOverflow , Gloria has defined that if we get a WSDL file then we will be able to get the services in UFT.There was another great post in stackoverflow, showing to define and call an API test from GUI test. I compiled all of them and came up with a set of steps that can be performed to configure Sharepoint Webservices and get data from it using UFT.

As per MSDN site if we get the sharepoint url , and add "_vti_bin/Lists.asmx?WDSL" to the end of the url ,I will be able to get the WSDL file for the corresponding url.
The WSDL will look like- http://server:port/ _vti_bin/Lists.asmx?WDSL.

By this way, I was able to transformed the sharepoint url to equivalent WSDL file. Precondition was to have access to the sharePoint.

Here is a step by step guide to configure the same in UFT and make it a Reusable API Call.
Step-1
Open UFT--> Click on New-->Test-->Select API Test-->Provide the Name of the script-->select the location to save the script-->Click ok

Step-2
Double Click on the Test Flow--> Click on Import WSDL(situated in the second ribbon , just under run)-->Select import from URL Checkbox and provide address as the WSDL link we have created above-->Click ok

Step-3
In the left hand side , we will find tool box. If we observe the toolbox, we will get two activities:
1. Local Activities if expanded we will get the webservices-->Lists SOAP-->Methods
2. Standard Activities if expanded we will get the list of standard operations

Step-4
Drag any services for example I have dragged- GetListAndView into test folder from the webservices  list. Also drag WriteToFile from standard activities. The objective here to get the response from the API call and store in a File for further processing.

Step-5
Go to right hand side of the test folder, we will get properties,under properties we will find a drop down . Select start there. Upon selecting start, we need to go to the test input/output parameters.
We need to create Input parameters as -
1. ListName
2. FileName
The objective is to call this API test from some other test and pass these values as parameter to this test.

Step-6
Select the same drop down to get GetListAndView(The service name) under properties.Under Test input/output parameter -->Click on Grid-->If we scroll further down, we will get Body tag. Under that we will find ListName.
Click on the anchor icon (Link to data source)-->This will open a new window-->Under Link from section-->select available steps radio-->inside Test--> Select Start-->right side , it will open all available parameters--->Select ListName-->Click ok
Now we can see under Body tag the listName value will be binded as
{Step.outputProperties.StartActivity.ListName}
equivalent xml will be:

<ListName>({Step.outputProperties.StartActivity.ListName}_Link(xyz))</ListName>
Step-7
In this step we need to tell UFT to store the output result to a file. Select writeToFile operation under properties from the dropdown.
We can see there are set of inputs. We need to set them one by one. I have set the below values:
Set Content-->
Similarly select the anchor icon(Link to data source)--> this content is nothing but our response xml of the webservice. So select the "available steps" radio in the "Link from" level
Select TestFlow-->GetListAndView(the service name) -->go to the output tab-->Expand the body till second level-->Select the node whose child is any.
For this case it was GetListAndViewResult-->Click ok
The key content will have the value as--
{Step.outputProperties.stServiceCallActivity18.Body.GetListAndViewResponse.GetListAndViewResult}

Set Mode->Select create new file. I always prefer this as output is not messed up. It always create a new file.

Set Encoding-->ASCII
Append new Line--> true
Set File Path--> This is where we will instruct UFT to store the file in the user defined name and path.Click on the anchor icon(Link to data source)-->It will open a data source to link-->select available steps radio in the link from area.
Expand Test-->Select Start-->right hand side, we will see all the parameters created under Start-->Select FileName-->Click ok
The key File path will be populated with the value--
{
Step.OutputProperties.StartActivity.FileName}

Now we have successfully created the reusable webservice Test.

How To Configure SharePoint's Services In UFT ? How To Configure SharePoint's Services In UFT ? Reviewed by Animesh Chatterjee on March 21, 2018 Rating: 5

How to Get List Name of a SharePoint link in UFT

March 14, 2018
SharePoint is the webApp designed by Microsoft. But as per HP passport site SharePoint is compatible with UFT. Also While browsing through advancedqtp site  I got an idea how to automate sharePoint using UFT's descriptive Programming.  Now getting list name from a sharePoint link is tricky. Here is an example how to do that:

msgbox getListName("your_SharePoint_URL")

The implementation will be as follows:
Function getListName("your_SharePoint_URL")
// this is to close the all open IE
SystemUtil.CloseProcessByName "IEEXPLORE.exe"
// open the sharePoint url
SystemUtil.run "iexplore.exe",your_SharePoint_URL
// This piece of code to navigate to the list settings
Browser("MicClass:=Browser").Page("MicClass:=Page").Link("html tag:=A","text:=LisList Tools Group").click
Browser("MicClass:=Browser").Page("MicClass:=Page").Link("html tag:=A","text:=LisSettings").click
Browser("MicClass:=Browser").Page("MicClass:=Page").Sync
//now the trick is to click on the address bar to get the url having List Name Until and unless we 
//click on the address bar sharepoint will not display the modified url with list name.
//The below code will click on the address bar
Browser("MicClass:=Browser").Page("MicClass:=Page").WinObject("name:=AddressDisplay Control").click
wait 2
urlwithListName=Browser("MicClass:=Browser").GetROProperty("url")
// now the url will look like= sharepoint link along with listname=some encoded string
//In order to get the list name we need to split the url with "List=" 
afterCut=split(urlwithListName,"List=")
//afterCut(0) will contain remaining part
//afterCut(1) will contain encoded list name. To get the actual name of the list,we can create our own 
//decoder or the easiest way to get that is to open bing's url decoder.
//I have coded to go to the bing decoder
Browser("MicClass:=Browser").navigate("https://www.bing.com/search?q=url+decoder")
//It will open the url decoder 
//setting the encoded list name in the text 
Browser("MicClass:=Browser").Page("MicClass:=Page").Frame("micClass:=Frame","html id:=third5302","html 
tag:=IFRAME").WebEdit("micClass:=WebEdit","html id:=text","name:=WebEdit").set afterCut(1)
//Click on the button
Browser("MicClass:=Browser").Page("MicClass:=Page").Frame("micClass:=Frame","html id:=third5302",
"html tag:=IFRAME").WebButton("miClass:=WebButton","html id:=decode",
"html tag:=Button","name:=Decode").Click
//get the decoded List name
LisName=Browser("MicClass:=Browser").Page("MicClass:=Page").Frame("micClass:=Frame","html id:=third5302",
"html tag:=IFRAME").WebEdit("micClass:=WebEdit","html id:=text","name:=WebEdit").getROProperty("value")
// this is to close the all open IE
SystemUtil.CloseProcessByName "IEEXPLORE.exe"
getListName=ListName
End Function

This is how we should be able to get the list name from a sharePoint.
How to Get List Name of a SharePoint link in UFT How to Get List Name of a SharePoint link in UFT Reviewed by Animesh Chatterjee on March 14, 2018 Rating: 5
Powered by Blogger.