How to show user prompts from within the plugin code?

OpenTAP provides the ability to provide user prompts from within the plugin code.
This can be asking a simple yes or no question or it can be asking for part details like a serial number etc.

For simple prompts


Use the built-in DialogStep which is part of the OpenTAP SDK.
You can provide a title, message, and a pre-defined choice of responses(Ok/Cancel and Yes/No)

For prompts showing custom inputs


Use the UserInput class provided by the OpenTAP SDK. When using the TAP editor, it shows up as a dialog, and when using the CLI, it automatically comes up as a user prompt.

The following code contains 2 examples.

  1. Prompt that asks the user to reset a DUT and confirm
  2. Prompt which asks the user to enter a DUT serial number.
public enum WaitForInputResult
    {
        // The number assigned determines the order in which the buttons are shown in the dialog.
        Cancel = 2, Ok = 1
    }

    // This describes a dialog that asks the user to reset the DUT.
    class ResetDutDialog
    {
        // Name is handled specially to create the title of the dialog window.
        public string Name { get { return "DUT Reset"; } }

        [Layout(LayoutMode.FullRow)] // Set the layout of the property to fill the entire row.
        [Browsable(true)] // Show it event though it is read-only.
        public string Message { get { return "Please reset the DUT. Click OK to confirm."; } }

        [Layout(LayoutMode.FloatBottom | LayoutMode.FullRow)] // Show the button selection at the bottom of the window.
        [Submit] // When the button is clicked the result is 'submitted', so the dialog is closed.
        public WaitForInputResult Response { get; set; }
    }

    // This describes a dialog that asks the user to enter the serial number.
    class EnterSNDialog
    {
        // Name is handled specially to create the title of the dialog window.
        public string Name { get { return "Please enter serialnumber"; } }
        
        // Serial number to be entered by the user.
        [Display("Serial Number")]
        public string SerialNumber { get; set; }

    }

    // This example shows how to use UserInput to get user input. 
    // In TAP GUI it appears as a dialog box. In TAP CLI the user is prompted.
    [Display("User Input Example", Groups: new[] { "Examples", "Plugin Development", "GUI" },
        Description: "This example shows how to use UserInput.")]
    public class UserInputExample : TestStep
    {
        [Display("Use Timeout", "Enabling this will make the dialog close after an amount of time.", Group: "Timeout", Order: 1)]
        public bool UseTimeout { get; set; }

        [EnabledIf("UseTimeout", true)]
        [Unit("s")]
        [Display("Timeout", "The dialog closes after this time.", Group: "Timeout", Order: 2)]
        public double Timeout { get; set; }

        public UserInputExample()
        {
            UseTimeout = true;
            Timeout = 5;
            Rules.Add(() => Timeout > 0, "Timeout must > 0s.", "Timeout");
        }

        public override void Run()
        {
            try
            {
                var timeout = UseTimeout ? TimeSpan.FromSeconds(Timeout) : TimeSpan.Zero;

                // Dialog/prompt where user has option to select "OK" or "Cancel".
                // The message/query is set by the Message property. Selection options are defined by WaitForInputResult.
                var dialog = new ResetDutDialog();
                UserInput.Request(dialog, timeout);
                
                // Response from the user.
                if (dialog.Response == WaitForInputResult.Cancel)
                {
                    Log.Info("User clicked Cancel.");
                    return;
                }
                Log.Info("User clicked OK. Now we prompt for DUT S/N.");

                var snDialog = new EnterSNDialog();
                // Dialog/prmpt where user has option to enter a string as DUT S/N
                UserInput.Request(snDialog, timeout);
                
                Log.Info("DUT S/N: {0}", snDialog.SerialNumber);
            }
            catch (TimeoutException)
            {
                Log.Info("User did not click. Is he sleeping?");
            }
        }
    }
3 Likes