//initial the Agendafunction Initagenda ()
{wlglobals.saveheaders = true;Wlglobals.savesource = true;
Includefile ("Myfunctionlib.js"); //Include an external JS filevar folderpath = Getagendafolder(); //get the folder path which scripts locate inGet testing enviroment for the scripts
var Env = GETENV ()
Copy the test data file into scripts
Filemap_file= CopyFile (FolderPath + "data/" + GETENV () + "/clientid.txt")
}Initial the Virtual Clientfunction Initclient ()
{
//read the test data file and define the data file parameter
Filemap_file_datafileparam = Wldatafileparam ("Filemap", Filemap_file, 1, "|", Wlparamrandom,wlparamglobal,wlparamupdateround , wlparamcycle); //get The data file from the data file:get the first columnfilemap_user_name = Wldatafilefield (Filemap_file_datafileparam, 1); //get The second column Filemap_uid = Wldatafilefield (Filemap_datafileparam, 2);}BeginTransaction ("Transactionname") //Read the start time//var startTime = time () //define the request headerwlhttp.header["UID"] = Filemap_uid. GetValue ()//Set the cookie if needed//wlcookie.set ("MStar", "v655o2o527l47", "http://mercury-stg.morningstar.com", "/", "" ") //set the request content typewlHttp.DataFile.Type = "Application/json; Charset=utf-8 " //read The post contents from an external file "CreateEntity.txt"wlHttp.DataFile.Filename = "D:\\load test\\mercury\\testdata\\createentity.txt" //compose the url:http://mercury-stg.morningstar.com/dataservice/api/v2/entity/listsvar createurl = "http://mercury-" + GETENV () + ". morningstar.com/dataservice/api/v2/entity/lists/"//Send the requestWlhttp.post (CreateURL) //Receive the response header and response contentvar responseheader = document.wlheadersvar responsesource = Document.wlsource //Convert the Text-format response source to Json-format responsevar Responsejson = eval ("(" + Responsesource + ")");//Extract the EntityId from the Resonsejson; the EntityId would be the input in the next Request-delete APIvar EntityId = Responsejson.entityid //Read the end time and calculate the total time it takes to finish the transaction//var endTime = time ()//var totaltime = (endtime-starttime)/1000//infomessage ("Total time of the" + CreateURL + "is" + totaltime) //verificationfuncation can stored in the myfunctionlib.js and call it directlyfunction verificationfunction (){Verify if the Respone contents contain text "Entity"var verifycontent = checkcontent (Responsesource, "Entity")//Verify if the response header status is 201var verifyheader = checkHttpStatus201 (Responseheader) if (wlsuccess = =Verifycontent&& wlsuccess = = Verifyheader)return wlsuccessElse ReturnWlminorerror} endtransaction ("Transactionname",Verificationfunction (), False, "verification failed") --------------------------------------------------------------------------------Define the Myfunctionl.jsNote:myFunction.js is located in where scripts is located //define The check header, status functionfunction checkHttpStatus200 (Responseheader){if (Responseheader[0].value = = "http/1.1 OK")return wlsuccessElsereturn Wlminorerror} Define the Check Header 201 Status function function checkHttpStatus201 (responseheader){if (Responseheader[0].value = = "http/1.1 201 Created")return wlsuccessElsereturn Wlminorerror} //define a function to check if the response contents contain a specific textfunctionCheckcontent(Responsesource, texttobeverified){var strsource = responsesource.tostring ();if (Strsource.indexof (texttobeverified)! =-1)return wlsuccess;Elsereturn wlminorerror;} //define a function to check if the response content is nullfunction Sourcecheck (Responsesource){if (Responsesource!= "")return wlsuccessElsereturn Wlminorerror} //Define a function a get the test environmentfunction GetEnv (){ //return "Dallas" //return "Uat" return "STG" //return "QA"} //Get the path of current folder where the project/scripts is loatedfunction Getagendafolder (){var path = "D:/load test/mercury/agenda/"var checkpath = folderexist (path)if (Checkpath= = true)return pathElsereturn "c:/codes_pms/performance/pfsapi/"} //Check if a folder existfunction Folderexist (folderpath){var fso = new ActiveXObject ("Scripting.FileSystemObject");if (FSO. FolderExists (FolderPath))return true;Elsereturn false;} ------------------------------------------------------------------------------directory structure:Agenda > scripts, TestData, Myfunction.jstestdata> uat, STG, QA > CreateEntity.txt
An example of a webload script