一个WebLoad 脚本范例

//initial the Agenda
function InitAgenda()
{
    wlGlobals.SaveHeaders = true;
    wlGlobals.SaveSource = true;
    IncludeFile("myFunctionLib.js");  
// include an external js file
    var FolderPath = getAgendaFolder(); //get the folder path which scripts locate in
    //get 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 Client
function 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 column 
    fileMap_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 header
wlHttp.Header["UID"] = fileMap_UID .getValue()
 
// set the cookie if needed
//wlCookie.Set("mstar", "V655O2O527L47", "http://mercury-stg.morningstar.com","/", "")
 
//set the request content type
wlHttp.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/lists
var createURL = "http://mercury-"+ getEnv()+".morningstar.com/DataService/api/v2/entity/lists/" 
 
// send the request
wlHttp.Post(createURL)
 
// receive the response header and response content
var responseHeader = document.wlHeaders
var responseSource = document.wlSource
 
// convert the text-format response source to json-format response 
var responseJSON = eval("(" + responseSource + ")");
// extract the EntityId from the resonseJSON; the EntityId will be the input in the next request - Delete API 
var 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 be stored in the myFunctionLib.js and call it directly
function verificationFunction() 
{
    // verify if the respone contents contain text "Entity"
    var verifyContent = checkContent(responseSource, "Entity" )
    // verify if the response header status is 201 
    var verifyHeader = checkHttpStatus201(responseHeader )
 
    if (WLSuccess == verifyContent && WLSuccess == verifyHeader)
        return  WLSuccess
    else
        return  WLMinorError
}
 
EndTransaction("TransactionName",verificationFunction(), false, "Verification failed")
 
--------------------------------------------------------------------------------
Define the myFunctionl.js 
Note: myFunction.js is located in where scripts are located
 
//define the check header 200 status function
function checkHttpStatus200(responseheader)
{
     if(responseheader[0].value == "HTTP/1.1 200 OK")
         return WLSuccess
     else
         return WLMinorError
}
 
//define the check header 201 status function 
function checkHttpStatus201(responseheader)
{
     if(responseheader[0].value == "HTTP/1.1 201 Created")
         return WLSuccess
     else
         return WLMinorError
}
 
// define a function to check if the response contents contain a specific text
function checkContent(responseSource, textToBeVerified)
{
     var strSource = responseSource.toString();
     if (strSource.indexOf (textToBeVerified) != -1)
         return WLSuccess;
     else
         return WLMinorError;
}
 
// define a function to check if the response content is null
function SourceCheck(responseSource)
{
     if (responseSource != "")
         return WLSuccess
     else
         return WLMinorError
}
 
// define a function a get the test environment
function getEnv(envStr)
{
    var env = ""; 
    switch(envStr.toLowerCase()){ 
        case "stg": 
            env = "-stg"; 
                break;
        case "live": 
            env = ""; 
            break; 
        case "qa": 
            env = "-qa"; 
            break; 
        case "dallas":
            env = "-dallas";
            break;
    } 
    return env; 
}
 
// get the path of current folder where the project/scripts are loated 
function getAgendaFolder()
{
     var path = "D:/Load Test/Mercury/agenda/"
     var checkPath = folderExist(path)
     if(checkPath == true)
         return path
     else
         return "C:/Codes_PMS/Performance/PFSAPI/"
}
 
// check if a folder exist
function folderExist(folderPath)
{
     var fso = new ActiveXObject("Scripting.FileSystemObject");
     if(fso.FolderExists(folderPath))
          return true;
    else
          return false;
}
 
------------------------------------------------------------------------------
目录结构:
Agenda > 脚本,TestData, myFunction.js
TestData> uat, stg, qa > CreateEntity.txt
posted @ 2014-12-19 17:18  tomweng  阅读(448)  评论(0编辑  收藏  举报