2015-11-04 97 views
0

作为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进程。

回答

1

把双引号的文件路径(因为它包含空格)可能会有所帮助:

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

此外,如果你的目的是从一个服务进程或DLL中,如“服务运行启动此SharePoint“,那么这段代码可能不会在桌面上启动所需的进程。您需要在启动信息中将桌面设置为"winsta0\\default"

+0

谢谢你的建议Gread和Sudhakar。我试图实现的是以另一个用户的身份打开Internet Explorer,以便我可以运行一些CodedUI来批准工作流程。 – Nicola1234

1

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

希望这可以帮助,并请提供场景,为什么你要启动IE浏览器,并可以给你一个更好的答案在论坛上。

+0

你提出了一些很好的观点! – GreatAndPowerfulOz