【问题标题】:Starting a process启动一个进程
【发布时间】:2015-11-04 10:10:30
【问题描述】:

作为 SharePoint 自动化测试的一部分,我尝试使用 System.Diagnostics.Process 以其他用户身份打开 Internet Explorer。下面是 C# 代码

System.Diagnostics.Process p = new System.Diagnostics.Process();

// Domain and User Name:
p.StartInfo.Domain = "MYDOMAIN";
p.StartInfo.UserName = "myusername";

// Command to execute and arguments:
p.StartInfo.FileName = "C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe";
p.StartInfo.Arguments = "http://url/AllItems.aspx";

// Build the SecureString password...
System.String rawPassword = "thepassword";
System.Security.SecureString encPassword = new System.Security.SecureString();
foreach (System.Char c in rawPassword)
{
    encPassword.AppendChar(c);
}

p.StartInfo.Password = encPassword;

// The UseShellExecute flag must be turned off in order to supply a password:
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = false;

p.Start();

当我运行这个自动化测试时,Visual Studio 返回通知我测试成功,但是 Internet Explorer 没有打开。

为了让窗口出现,我的代码中是否缺少某些内容?在运行测试之前没有运行 iexplore 进程。

【问题讨论】:

    标签: c# automation


    【解决方案1】:

    在文件路径周围加上双引号(因为它包含空格)可能会有所帮助:

    p.StartInfo.FileName = "\"C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe\""
                            ^^                                                        ^^
    

    此外,如果您打算从服务进程或在“SharePoint”等服务中运行的 dll 启动此代码,则此代码可能不会在所需的桌面上启动该进程。您需要在启动信息中将桌面设置为"winsta0\\default"

    【讨论】:

    • 感谢您的建议 Gread 和 Sudhakar。我想要实现的是以另一个用户身份打开 Internet Explorer,这样我就可以运行一些 CodedUI 来批准工作流。
    【解决方案2】:

    要运行一个进程,工作进程应该具有高权限,这在任何 Web 应用程序中都不是理想的情况。如果您的目的是使用 IE 进行单元测试,那么我会考虑使用 WaitIN 之类的东西。如果您的目的是让应用程序逻辑访问 URL 并执行某些操作,请考虑使用 HttpWebRequest。如果您仍需要启动一个进程,则创建一个 Windows 服务,然后公开一个网络调用,以便在 Share Point 中您只需拨打一个电话,您的 Windows 服务就可以在本地帐户或其他一些高权限帐户上运行。

    希望这对您有所帮助,请提供您要启动 IE 的场景,并且可以在论坛中为您提供更好的答案。

    【讨论】:

      猜你喜欢
      • 2015-03-22
      • 1970-01-01
      • 2018-05-12
      • 2011-04-16
      • 2012-12-04
      • 2012-10-27
      • 1970-01-01
      • 2014-07-15
      相关资源
      最近更新 更多