2015-11-04 96 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