The following examples demonstrate how to create an external component that saves requested excel files from the server. Here are the steps:

  1. Implement the IExternalComponent.OnAfterResponse(RuntimeSession) method.
  2. Determine the URL of the response that contains the excel file to save.
  3. Create a unique name of the location or file name of the file to be saved.
  4. Save the response body to disk.

 

public class SaveExcelResponse : IExternalComponent
{
    void IExternalComponent.OnTestStart()
    {
        //--- Not necessary.
    }
    void IExternalComponent.OnTestEnd()
    {
        //--- Not necessary.
    }
    void IExternalComponent.OnBeforeRequest(RuntimeSession session)
    {
        //--- Not necessary.
    }
    void IExternalComponent.OnAfterResponse(RuntimeSession session)
    {
        if (session.Url.LocalPath == "/file.xlsx" &&
            session.ResponseCode == 200)//find the proper response and make sure it's a success response.
        {
            byte[] response = session.GetRawResponseBody();
            if (response.Length > 0)
            {
                string fileName = string.Format("file_{0}_{1}.xlsx", session.VUNumber, session.IterationNumber); //give unique name
                string path = System.IO.Path.Combine("C:\\DownloadedFiles", fileName);
                using (System.IO.FileStream fs = System.IO.File.Open(path, System.IO.FileMode.CreateNew))
                {
                    fs.Write(response, 0, response.Length); //write file
                }
            }
        }
    }
}