How to use a PowerShell script to help with automated testing of Statistica Live Score?

How to use a PowerShell script to help with automated testing of Statistica Live Score?

book

Article ID: KB0076940

calendar_today

Updated On:

Products Versions
Spotfire Statistica 13.3.1 and higher

Description

Automating the use of a script to test Live Score output is desirable.  This will help save time and resources as Live Score has to be tested manually.  

Issue/Introduction

This article discusses the use of a PowerShell script that can be used to test a response from a Live Score server.

Environment

Windows operating systems only.

Resolution

I  The PowerShell script
The following PowerShell script can be used, with slight modification, to test Live Score output:

$url = "http://localhost:8081/LiveScore"    #Typical URL to a Live Score install when testing from the server itself.

$user = "hostname\score"                          #Username and password for Live Score user
$pass = "Password"

$input  = "c:\temp\request.xml"                 
# Create the c:\temp\ location if it does not exist.  The request.xml is the Live Score xml that is used for testing.  
$output = "c:\temp\response.xml"             # This file is created and provides the response of the test
$outerr = "c:\temp\response-error.xml"     # File is created only if there is an error with the Live Score request
 
$body = (get-content $input)                   
  # Get the xml file to use in the test

$pair = "$($user):$($pass)"
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($Pair))
$headers = @{ Authorization = "Basic $encodedCredentials" }

# Invoke-WebRequest will throw an exception on 4xx/5xx HTTP response;
# make sure we still collect the response
# https://stackoverflow.com/questions/19122378/powershell-web-request-without-throwing-exception-on-4xx-5xx
# TODO: -ErrorAction Stop
# NOTE: "-OutFile $output" will prevent Invoke-WebRequest from returning the response object

Write-Host "Calling: $($url)"

#$res = (Invoke-WebRequest -Uri $url -Method Post -Headers $headers -Body $body) 
 #The main part of the Powershell script that makes the request
#Write-Host "Result: $res"
#$res

try

    Invoke-WebRequest -Uri $url -Method Post -Headers $headers -Body $body -OutFile $output
}
catch [System.Net.WebException]   
 #Catch any/all errors

    Write-Warning "An exception was caught: $($_.Exception.Message)"
    
    $res = $_.Exception.Response.GetResponseStream() 

    $reader = New-Object System.IO.StreamReader($res)
    $reader.BaseStream.Position = 0
    $reader.DiscardBufferedData()
    
    $responseBody = $reader.ReadToEnd()

    Write-Warning "Server response: $responseBody"

    # example of parsing the response (or error response in this case) for XML content
    $parsedData = $responseBody | Select-Xml '//faultstring' | % { $_.Node."#text" }

    Write-Warning "Live Score error: $parsedData"

    # e.g. send an email about this error with $parsedData as the explanation
    # or e.g. save the whole response body $responseBody to file

    $responseBody | Out-File -FilePath $outerr


Write-Host "Done"
Exit



# Notes:  The output of the response is sent to a file.  Also, any error messages are sent to a file as well.  

II.  Implementing the PowerShell script

1.  Open the Powershell ISE on the Live Score server by clicking Start and typing PowerShell.  Select the Windows PowerShell ISE:

PowerShell ISE (Intergrated Scripting Environment)

2.  Copy the request.xml file to the C:\temp\ folder.  The request.xml is specific to your Live Score model.  There is an attached request.xml, which is an example.  It is assumed that a model is built and deployed in Statistica Enterprise. Also, the WFID in the example request.xml will have to match the model deployed in Enterprise. For more information, see   https://support.tibco.com/s/article/How-to-build-a-workspace-with-Input-and-Output-nodes-for-Live-Score-deployment

3.  Open the .ps1 file and click the green error to run the script (an example of the type of ps1 script is attached, below, as InvkWebReqWithErrorHandling_Exp.ps1):

Opening PowerShell script in ISE

Note that the script can be modified in the PowerShell ISE and saved.  

4.  Check to see if the response.xml file has been created (note that the response-error.xml will only exist when an error is generated by the script):
Check to see if response.xml file is created

5.   Automate the PowerScript in Windows Task Scheduler:

Automate task in Task Scheduler

Now periodically the temp folder can be checked to see if a response-error.xml  has been created.  If so, the file can be opened with Notepad (or most any text editor) to view the contents:

View response-error.xml file with Notepad
 

Additional Information

https://stackoverflow.com/questions/19122378/powershell-web-request-without-throwing-exception-on-4xx-5xx
https://stackoverflow.com/questions/32167021/getting-full-response-body-from-system-net-webrequest
https://stackoverflow.com/questions/18771424/how-to-get-powershell-invoke-restmethod-to-return-body-of-http-500-code-response
https://stackoverflow.com/questions/11956960/parse-xml-and-find-all-instances-of-a-string

Attachments

How to use a PowerShell script to help with automated testing of Statistica Live Score? get_app
How to use a PowerShell script to help with automated testing of Statistica Live Score? get_app