添加新的Winform后无法调试自动化测试

时间:2018-12-19 21:06:08

标签: c# winforms automated-tests visual-studio-debugging

我使用Selenium和NUnit对Web UI页面和Web服务进行了一些自动测试。我最近添加了一个动态创建的Windows窗体,该窗体显示为模式对话框,并且需要一些用户输入,例如密码等。输入输入后,将使用DPAPI保存并加密设置,并且在随后的运行中不需要用户输入了。

我的问题是我可以调试到显示对话框的位置,但是一旦代码到达模式对话框,Visual Studio就会挂起。我可以运行代码,并且可以正常运行。我只是无法调试。有人看过这个问题吗?

用户提示代码:

public static class TestSettingsConfigExtensions
{

    ....    

    private static string PromptUserForSetting(string settingName)
    {
        string promptedValue = null;

        DialogResult result = UserInput.Show("User Setting", $"Please enter a value for {settingName}", ref promptedValue);

        if (result == DialogResult.OK)
            return promptedValue;

        return null;
    }


    private class UserInput
    {
        public static DialogResult Show(string title, string promptText, ref string value)
        {
            Form form = new Form();
            Label lblPrompt = new Label();
            TextBox txtSettingValue = new TextBox();
            txtSettingValue.UseSystemPasswordChar = true;
            Label lblUnmask = new Label();
            CheckBox chkShouldUnmask = new CheckBox();
            chkShouldUnmask.CheckedChanged += (object sender, EventArgs e) => { ChkShouldUnmask_CheckedChanged(chkShouldUnmask.Checked, ref txtSettingValue); };
            Button btnOk = new Button();
            Button btnCancel = new Button();

            form.Text = title;
            lblPrompt.Text = promptText;
            txtSettingValue.Text = value;

            lblUnmask.Text = "Unmask";
            chkShouldUnmask.Checked = false;

            btnOk.Text = "OK";
            btnCancel.Text = "Cancel";
            btnOk.DialogResult = DialogResult.OK;
            btnCancel.DialogResult = DialogResult.Cancel;

            lblPrompt.SetBounds(9, 20, 372, 13);
            txtSettingValue.SetBounds(12, 36, 372, 20);
            lblUnmask.SetBounds(9, 65, 50, 13);
            chkShouldUnmask.SetBounds(59, 65, 372, 13);
            btnOk.SetBounds(228, 102, 75, 23);
            btnCancel.SetBounds(309, 102, 75, 23);

            lblPrompt.AutoSize = true;
            txtSettingValue.Anchor = txtSettingValue.Anchor | AnchorStyles.Right;
            lblUnmask.AutoSize = true;
            chkShouldUnmask.Anchor = chkShouldUnmask.Anchor | AnchorStyles.Right;
            btnOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            btnCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

            form.ClientSize = new Size(396, 137);
            form.Controls.AddRange(new Control[] { lblPrompt, txtSettingValue, lblUnmask, chkShouldUnmask, btnOk, btnCancel });
            form.ClientSize = new Size(Math.Max(300, lblPrompt.Right + 10), form.ClientSize.Height);
            form.FormBorderStyle = FormBorderStyle.FixedDialog;
            form.StartPosition = FormStartPosition.CenterScreen;
            form.MinimizeBox = false;
            form.MaximizeBox = false;
            form.AcceptButton = btnOk;
            form.CancelButton = btnCancel;

            DialogResult dialogResult = form.ShowDialog();
            value = txtSettingValue.Text;
            return dialogResult;
        }

        private static void ChkShouldUnmask_CheckedChanged(bool shouldUnmask, ref TextBox txtSettingValue)
        {
            if (shouldUnmask)
                txtSettingValue.UseSystemPasswordChar = false;
            else
                txtSettingValue.UseSystemPasswordChar = true;
        }
    }
}

0 个答案:

没有答案