tsrc/testing/tools/wshTestRun_local.vbs
branchRCL_3
changeset 23 befca0ec475f
parent 0 96612d01cf9f
equal deleted inserted replaced
22:839377eedc2b 23:befca0ec475f
       
     1 '
       
     2 ' WScript that will invoke the test run execute functionality at ATS3 web server without installing any ATS3
       
     3 ' specific programs on the local PC.
       
     4 '
       
     5 ' Copies the test run zip into ATS3 server over HTTP before starting the test run
       
     6 '
       
     7 ' Usage cscript wshRunX.vbs <username> <password> <server hostname> <path to testDrop.zip>
       
     8 '
       
     9 
       
    10 ' Get the command line arguments
       
    11 set args = WScript.Arguments
       
    12 
       
    13 ' Check that all arguments have been specified
       
    14 Set objShell = WScript.CreateObject("WScript.Shell")
       
    15 Set env = objShell.Environment("Process")
       
    16 checkEnvVars(env)
       
    17 
       
    18 ' Invoke the web application and write the result to stdOut
       
    19 WScript.StdOut.Write doTestRunX(env("ats3.username"), env("ats3.password"), env("ats3.host"), env("ats3.pathToDrop"), URLEncode( env("ats3.schedule") ))
       
    20 
       
    21 ' Quit the script
       
    22 Wscript.Quit
       
    23 
       
    24 Function checkEnvVars(env)
       
    25 	if env("ats3.username") = "" then
       
    26 		WScript.Echo "Environment variable ats3.username not specified"
       
    27 		WScript.Quit 1
       
    28 	elseif env("ats3.password") = "" then
       
    29 		WScript.Echo "Environment variable ats3.password not specified"
       
    30 		WScript.Quit 1
       
    31 	elseif env("ats3.host") = "" then
       
    32 		WScript.Echo "Environment variable ats3.host not specified"
       
    33 		WScript.Quit 1		
       
    34 	elseif env("ats3.pathToDrop") = "" then
       
    35 		WScript.Echo "Environment variable ats3.pathToDrop not specified"
       
    36 		WScript.Quit 1	
       
    37 	end if
       
    38 End Function
       
    39 
       
    40 ' Invoke the ATS3 web application in given host with the specified username, password and file path
       
    41 Function doTestRunX(uname, password, hostName, pathToDrop, schedule)
       
    42 	Set objxmlHTTP = CreateObject("MSXML2.ServerXMLHTTP.3.0")
       
    43 	objxmlHTTP.setTimeouts 0,60000,3600000,3600000
       
    44 	
       
    45 	Call objxmlHTTP.open("POST", "http://" & hostName & "/ats3/XTestRunExecute.do?username=" & uname & "&password=" & password & "&schedule=" & schedule, False)
       
    46 	objxmlHTTP.setRequestHeader "Content-Type", "multipart/form-data; boundary=AaB03x"
       
    47 	
       
    48 	Set BinaryStream = CreateObject("ADODB.Stream")
       
    49 	BinaryStream.Type = 1
       
    50 	BinaryStream.Open
       
    51 	BinaryStream.LoadFromFile pathToDrop
       
    52 	
       
    53 	objxmlHTTP.Send BuildFormData(BinaryStream.Read,"AaB03x","testDrop.zip","testDrop")
       
    54 
       
    55 	doTestRunX = objxmlHTTP.ResponseText
       
    56 	
       
    57 	BinaryStream.Close
       
    58 End Function
       
    59 
       
    60 Function BuildFormData(FileContents, Boundary, FileName, FieldName)
       
    61   Dim FormData, Pre, Po
       
    62   Const ContentType = "application/upload"
       
    63   
       
    64   'The two parts around file contents In the multipart-form data.
       
    65   Pre = "--" + Boundary + vbCrLf + mpFields(FieldName, FileName, ContentType)
       
    66   Po = vbCrLf + "--" + Boundary + "--" + vbCrLf
       
    67   
       
    68   'Build form data using recordset binary field
       
    69   Const adLongVarBinary = 205
       
    70   Dim RS: Set RS = CreateObject("ADODB.Recordset")
       
    71   RS.Fields.Append "b", adLongVarBinary, Len(Pre) + LenB(FileContents) + Len(Po)
       
    72   RS.Open
       
    73   RS.AddNew
       
    74     Dim LenData
       
    75     'Convert Pre string value To a binary data
       
    76     LenData = Len(Pre)
       
    77     RS("b").AppendChunk (StringToMB(Pre) & ChrB(0))
       
    78     Pre = RS("b").GetChunk(LenData)
       
    79     RS("b") = ""
       
    80     
       
    81     'Convert Po string value To a binary data
       
    82     LenData = Len(Po)
       
    83     RS("b").AppendChunk (StringToMB(Po) & ChrB(0))
       
    84     Po = RS("b").GetChunk(LenData)
       
    85     RS("b") = ""
       
    86     
       
    87     'Join Pre + FileContents + Po binary data
       
    88     RS("b").AppendChunk (Pre)
       
    89     RS("b").AppendChunk (FileContents)
       
    90     RS("b").AppendChunk (Po)
       
    91   RS.Update
       
    92   FormData = RS("b")
       
    93   RS.Close
       
    94   BuildFormData = FormData
       
    95 End Function
       
    96 
       
    97 Function mpFields(FieldName, FileName, ContentType)
       
    98   Dim MPTemplate 'template For multipart header
       
    99   MPTemplate = "Content-Disposition: form-data; name=""{field}"";" + _
       
   100    " filename=""{file}""" + vbCrLf + _
       
   101    "Content-Type: {ct}" + vbCrLf + vbCrLf
       
   102   Dim Out
       
   103   Out = Replace(MPTemplate, "{field}", FieldName)
       
   104   Out = Replace(Out, "{file}", FileName)
       
   105   mpFields = Replace(Out, "{ct}", ContentType)
       
   106 End Function
       
   107 
       
   108 Function StringToMB(S)
       
   109   Dim I, B
       
   110   For I = 1 To Len(S)
       
   111     B = B & ChrB(Asc(Mid(S, I, 1)))
       
   112   Next
       
   113   StringToMB = B
       
   114 End Function
       
   115 
       
   116 Function URLEncode(data)
       
   117 	data = replace(data,"\","/")
       
   118 	data = replace(data,"$","%24")
       
   119 	data = replace(data,"&","%26")
       
   120 	data = replace(data,"+","%2B")
       
   121 	data = replace(data,",","%2C")
       
   122 	data = replace(data,"/","%2F")
       
   123 	data = replace(data,":","%3A")
       
   124 	data = replace(data,";","%3B")
       
   125 	data = replace(data,"=","%3D")
       
   126 	data = replace(data,"?","%3F")
       
   127 	data = replace(data,"@","%40")
       
   128 	URLEncode = data
       
   129 End Function