如何在 VS 2010 中正确显示 MSTest 方法的对话框?
我最近将 MSTest 项目升级到 .NET 4.0 和 VS 2010。其中一些测试会查询外部供应商服务,从而提示用户提供必要的凭据,以便通过我们的公司 Web 代理进行通信。这过去在 vs2008 中工作得很好,但升级后,只有当用户在开始测试后立即将焦点从 VS 切换到另一个应用程序时,才会显示该对话框。显示对话框时有什么特别需要做的吗?我能想到的最好的情况是,随着 VS 的重新设计,引入了一些 WPF 警告。
有问题的代码
private void PromptUser()
{
if (!credentialsSet)
{
using (CredentialsDialog dialog = new CredentialsDialog(true))
{
Process process = Process.GetCurrentProcess();
IWin32Window window = Control.FromHandle(process.MainWindowHandle);
DialogResult dr = dialog.ShowDialog(window);
if (dr == DialogResult.Cancel)
{
throw new InvalidOperationException("Credentials not entered");
}
credentials = dialog.Credentials;
user = dialog.Username;
password = dialog.Password;
domain = dialog.Domain;
}
credentialsSet = true;
}
}
I recently upgraded an MSTest project to .NET 4.0 and VS 2010. Several of the tests query an outside vendor service and thus prompt the user for necessary credentials to communicate through our corporate web proxy. This used to work fine in vs2008 but after the upgrade the dialog will only display if the user switches focus from VS to another app immediately after kicking off the tests. Is there anything special that needs to be done when displaying the dialog? The best I can figure is that there is some WPF caveat that got introduced with the redesign of VS.
The code in question
private void PromptUser()
{
if (!credentialsSet)
{
using (CredentialsDialog dialog = new CredentialsDialog(true))
{
Process process = Process.GetCurrentProcess();
IWin32Window window = Control.FromHandle(process.MainWindowHandle);
DialogResult dr = dialog.ShowDialog(window);
if (dr == DialogResult.Cancel)
{
throw new InvalidOperationException("Credentials not entered");
}
credentials = dialog.Credentials;
user = dialog.Username;
password = dialog.Password;
domain = dialog.Domain;
}
credentialsSet = true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为要求提供凭据是不好的做法。您是否考虑过将导出的凭据添加到测试程序集的配置中。
如果您尝试将其设置为自动构建,您将遇到很多问题。
I would sugest that requiring the credentials is bad practice. Have you conidered adding your credentials in an excrypted fasion to the config of the test assembly.
You will run into a lot of problems if you try to setup this for automated build.
您可能需要激活该对话框才能使其在您激活的任何程序上弹出。
http://msdn.microsoft.com/ en-us/library/system.windows.forms.form.activate.aspx
You may need to activate the dialog in order to get it to pop up over whatever programs you have active.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.activate.aspx