The following examples demonstrate how to create an external component that appends an extractor values to a .txt file. Here are the steps:

  1. Implement the IExternalComponent.OnAfterIteration(RuntimeSession) method.
  2. Create an output file Output.txt
  3. Use a lock to lock the file for writing.
  4. Access the extractor value and save it.

 

    public class SaveExtractorValue : IExternalComponent
    {
        void IExternalComponent.OnTestStart()
        {
            //--- Not necessary.
        }
        void IExternalComponent.OnTestEnd()
        {
            //--- Not necessary.
        }
        void IExternalComponent.OnBeforeRequest(RuntimeSession session)
        {
            //--- Not necessary.
        }
        void IExternalComponent.OnAfterResponse(RuntimeSession session)
        {
            //--- Not necessary.
        }
        /// <summary>
        /// A write lock for multi-thread file writing
        /// </summary>
        readonly object s_writeLock = new object();

        void IExternalComponent.OnAfterIteration(RuntimeVU vu)
        {
            string idVal = vu.ExtractorRuntime.GetExtractorValue("MyExtractorName");
            if (!string.IsNullOrEmpty(idVal)) //check if this value indeed exists and no error occurred.
            {
                lock (s_writeLock)
                {
                    string outputPath = Path.Combine(
                        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                        "Output.txt"); //save to %Documents%\Output.txt
                    using (StreamWriter fs = new StreamWriter(outputPath, true))
                    {
                        fs.WriteLine(idVal);
                    }
                }
            }
        }
    }