Example Programs

cancel
Showing results for 
Search instead for 
Did you mean: 

Passing Custom Command Line Arguments to a TestStand Operator Interface (.NET)

Overview

The TestStand Application Manager supports processing custom command line arguments through the ProcessUserCommandLineArguments event. This can prove useful when automating test execution from a CI server.

Description

This example is similar to the .NET Basic User Interface, but has been modified to accept the following command line arguments:

Option Purpose
/SimulateFailure <failures> When used with the included example sequence file, this suppresses the simulation dialog and instead simulate failures as specified. This argument accepts the following parameters:
  • Keyboard
  • Video
  • Power
  • CPU
  • ROM
  • RAM
If no parameters are specified, then all components are simulated to pass.

 

This example demonstrates the following concepts:

  • Using _ApplicationMgrEvents_ProcessUserCommandLineArgumentsEventHandler to handle custom command line arguments
    Spoiler
    private void axApplicationMgr_ProcessUserCommandLineArguments(object sender, NationalInstruments.TestStand.Interop.UI.Ax._ApplicationMgrEvents_ProcessUserCommandLineArgumentsEvent e)
            {
                int numParams = (e.arguments.Count) - e.currentArgument;
    
                if (e.arguments[e.currentArgument] == "/SimulateFailure")
                {
                    e.currentArgument++;
                    for (int i = 0; i < numParams; i++)
                    {
                        switch (e.arguments[e.currentArgument])
                        {
                            case "Keyboard":
                                if (e.processCommand == true)
                                    this.Fails.Keyboard = 4;
                                e.currentArgument++;
                                break;
                            case "Video":
                                if (e.processCommand == true)
                                    this.Fails.Video = 15;
                                e.currentArgument++;
                                break;
                            case "Power":
                                if (e.processCommand == true)
                                    this.Fails.Power = true;
                                e.currentArgument++;
                                break;
                            case "CPU":
                                if (e.processCommand == true)
                                    this.Fails.CPU = true;
                                e.currentArgument++;
                                break;
                            case "ROM":
                                if (e.processCommand == true)
                                    this.Fails.ROM = true;
                                e.currentArgument++;
                                break;
                            case "RAM":
                                if (e.processCommand == true)
                                    this.Fails.RAM = true;
                                e.currentArgument++;
                                break;
                            default:
                                break;
                        }
                    }
                }
                else
                {
                    e.errorProcessing = ProcessCommandLineErrors.ProcessCommandLineError_CustomError;
                    e.errorMessage = "Invalid Command Line Argument: " + e.arguments[e.currentArgument];
                }
            }
  • Modifying a TestStand user interface to print UI error messages to the console and set the return code appropriately
    Spoiler
    		private void axApplicationMgr_ReportError(object sender, NationalInstruments.TestStand.Interop.UI.Ax._ApplicationMgrEvents_ReportErrorEvent e)
    		{
    			Console.WriteLine("Error " + e.errorCode + ": " + e.errorMessage); //this isn't working because of appdomain stuff
    			axApplicationMgr.ExitCode = e.errorCode; 		
    		}
  • Use of the ApplicationMgr.AddCommandLineArgumentsHelp function to add custom command line arguments help.
    Spoiler
    axApplicationMgr.AddCommandLineArgumentsHelp("[/SimulateFailure <failures>]", "<failures>: components to fail separated by spaces.\nvalid parameters are: Keyboard, Video, Power, CPU, ROM, & RAM", "");
  • Creating a custom UI Message handler to pass simulation data to sequence execution.
Spoiler
private void axApplicationMgr_UserMessageEvent(object sender, NationalInstruments.TestStand.Interop.UI.Ax._ApplicationMgrEvents_UIMessageEventEvent e)
        {
			if (e.uiMsg.Event == UIMessageCodes.UIMsg_UserMessageBase + 1)
			{
				SequenceContext SeqContext = e.uiMsg.ActiveXData as SequenceContext;
				if (SeqContext == null)
					return; //Instead of erroring when SeqContext isn't passed, we will ignore this message as to not break other client files that use the same UI message code.
				try
				{
					SeqContext.Locals.SetValBoolean("UiMsgHandled", 0, true);
					SeqContext.Locals.SetValNumber("KeyboardValue", 0, Fails.Keyboard);
					SeqContext.Locals.SetValNumber("VideoValue", 0, Fails.Video);
					SeqContext.Locals.SetValBoolean("PowerFail", 0, Fails.Power);
					SeqContext.Locals.SetValBoolean("CPUFail", 0, Fails.CPU);
					SeqContext.Locals.SetValBoolean("ROMFail", 0, Fails.ROM);
					SeqContext.Locals.SetValBoolean("RAMFail", 0, Fails.RAM);
				}
				catch (Exception theException)
				{
					Console.WriteLine("Error :" + theException.Message);
					Application.Exit();
				}

			}
        }

Note: By default, TestStand shows dialogs under certain conditions. This can cause hangs when automating sequence execution without user input, such as when ran from a CI Server. In order to prevent popups that require user input, configure the following options:

  • Disable User Management or automatically log in the System User
  • Set the 'Station Options » Execution »  On Run-Time Error' setting to something other than 'Show Dialog', such as 'Run Cleanup'.
  • Specify 'Station Options » Time Limits' for Exiting and Normal Execution such that the user will not be prompted.
  • Pass the /quit argument via the command line so TestStand automatically shuts down after all executions are complete.

Hardware and Software Requirements

TestStand 2014 SP1 or later

Visual Studio 2015 or later
LabWindows/CVI Runtime Engine 2015 or later

Steps to Implement or Execute Code

 

  1. Unzip the attached example and build the Basic User Interface solution for the bitness of TestStand you have installed.
  2. Open a command line window in the build output directory (Shift + right click » Open command window here). Alternatively, you can configure command line arguments for the Visual Studio debugger in the Project Properties under the Debug tab.
  3. In order to see the Command Line help, run:

    TestExec /?

  4. Experiment with the /SimulateFailure argument. For example:

    TestExec /runEntryPoint "Single Pass" "<ExampleDir>\TestSequence\Computer Motherboard Test Sequence.seq" /SimulateFailure Keyboard Video RAM /quit

Comments
Member AntoineVretman
Member

Very useful ! I guess i begin to understand how to really use axApplicationManager to run my sequences programmatically ! 

As described in Overview, i need to run my sequences from a CI Server, so this example is absolutely matching my needs. 

Nevertheless, i am not able to report errors: when parameter is not correct and i set: 

                e.errorProcessing = ProcessCommandLineErrors.ProcessCommandLineError_CustomError;
                e.errorMessage = "Invalid Command Line Argument: " + e.arguments[e.currentArgument];

i have no error raised... 

I set options as you recommand about StationOptions, log in and /quit parameter...

Any idea ? thanks.

Active Participant WireWeaver
Active Participant

Hey Antoine,

 

I don't have a chance to dig too much into this right now, but I'll take a look later. Setting that error should trigger the axApplicationMgr_ReportError event that is also customized in this example. I just noticed a comment i left, but don't remember why i never revisited it.

Console.WriteLine("Error " + e.errorCode + ": " + e.errorMessage); //this isn't working because of appdomain stuff

Pretty vague i know... but for starters I would put a breakpoint there and see if your error message is making it to this callback.

 

-Trent

Member AntoineVretman
Member

Hi,

 

Thanks for reply. Actually that's the blocking point: inserting a breakpoint here, or adding methos to display log messages, or message boxes has no effect here. It means the exception is not raised, and execution never goes through axApplicationManager_reportError method. 

 

 private void axApplicationMgr_ReportError(object sender, NationalInstruments.TestStand.Interop.UI.Ax._ApplicationMgrEvents_ReportErrorEvent e)
        {
            TSUIlogger.Error("Error " + e.errorCode + ": " + e.errorMessage);
            MessageBox.Show(this, ErrorMessage.AppendCodeAndDescription(this.axApplicationMgr, e.errorMessage, e.errorCode), "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            Console.WriteLine("Error " + e.errorCode + ": " + e.errorMessage);
            axApplicationMgr.ExitCode = e.errorCode;
   
}

I don't understand why, because documentation expressively specify that set value for 

 e.errorProcessing = ProcessCommandLineErrors.ProcessCommandLineError_CustomError;

should automatically raise event for reportError method, and my axApplicationManager has event property for ReportError attached to corresponding method...image.png

 

Thanks for your time !

Antoine

Active Participant WireWeaver
Active Participant

Hey Antoine,

 

I just played around with this some and it looks like the error is actually getting reported prior to this event being called. it does print the error to the console, but it never calls that callback (because the error is already handled by the appMgr):

DebugSettings.PNG

ErrorMessage.PNG

Member danimon
Member

Hi

Thanks for this helpfully example.

Unfortunately I have still some struggle with the "no login message box" part. I did as described and while I am opening the Teststand Sequence Editor, the login message box doesn't appear, but if I start the example code application, the message box shows up.

Could you help me to find out, what I have forgotten or did wrong?

Thanks for your help,

Dani

Member joshikak
Member

I'm trying to run a sequence using command line argument but my sequence is ending up with the below error.

 

Thank you in advance,

Capture.PNG

Active Participant WireWeaver
Active Participant

Since this isn't specific to custom commands, you should post your question to the normal TestStand forums:

https://forums.ni.com/t5/NI-TestStand/bd-p/330

 

Also, you should include the specific command that you're which throws this error.

-Trent

Contributors