Created
May 8, 2020 00:35
-
-
Save tegaaa/b48a2d0b79808b4cbb05aae850326ffe to your computer and use it in GitHub Desktop.
start chromedriver as another user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using OpenQA.Selenium; | |
using OpenQA.Selenium.Chrome; | |
using OpenQA.Selenium.IE; | |
using OpenQA.Selenium.Remote; | |
using System; | |
using System.Diagnostics; | |
using System.Net; | |
using System.Security; | |
using System.Threading; | |
namespace UITests | |
{ | |
public class DriverHelper : IDisposable | |
{ | |
private static IWebDriver _driver; | |
private static Process _driverProcess = null; | |
string _driverPath = @"IEDriverServer.exe"; | |
string _driverPathChrome = @"ChromeDriver.exe"; | |
public IWebDriver RunIEAsDifferentUser(string User, string Password) | |
{ | |
if (_driver == null) | |
{ | |
RunAs(_driverPath, User, Password); | |
var ops = new InternetExplorerOptions(); | |
//_driver = new InternetExplorerDriver(ops); | |
_driver = new RemoteWebDriver(new Uri("http://localhost:5555/"), ops.ToCapabilities(), TimeSpan.FromSeconds(180)); | |
} | |
return _driver; | |
} | |
public IWebDriver RunChromeAsDifferentUser(string User, string Password) | |
{ | |
if (_driver == null) | |
{ | |
RunAs(_driverPathChrome, User, Password); | |
var opsChrome = new ChromeOptions(); | |
opsChrome.AddArgument("no-sandbox"); | |
_driver = new RemoteWebDriver(new Uri("http://localhost:9515/"), opsChrome.ToCapabilities(), TimeSpan.FromSeconds(180)); | |
//_driver = new ChromeDriver(opsChrome); | |
} | |
return _driver; | |
} | |
public void RunAs(string path, string username, string password) | |
{ | |
ProcessStartInfo myProcess = new ProcessStartInfo(path); | |
myProcess.UserName = username; | |
myProcess.Password = MakeSecureString(password); | |
myProcess.UseShellExecute = false; | |
myProcess.LoadUserProfile = true; | |
myProcess.Verb = "runas"; | |
myProcess.Domain = "Domain"; | |
Thread startThread = new Thread(() => | |
{ | |
_driverProcess = Process.Start(myProcess); | |
_driverProcess.WaitForExit(); | |
}) | |
{ IsBackground = false }; | |
startThread.Start(); | |
} | |
public SecureString MakeSecureString(string text) | |
{ | |
SecureString secure = new SecureString(); | |
foreach (char c in text) | |
{ | |
secure.AppendChar(c); | |
} | |
return secure; | |
} | |
public void Dispose() | |
{ | |
// - Remember to close/exit/terminate the driver process and browser instance when you are done. | |
if (_driverProcess != null) | |
{ | |
_driver.Quit(); | |
// Free managed resources | |
if (!_driverProcess.HasExited) | |
{ | |
_driverProcess.CloseMainWindow(); | |
_driverProcess.WaitForExit(5000); | |
// Kill the process if the process still alive after the wait | |
if (!_driverProcess.HasExited) | |
{ | |
_driverProcess.Kill(); | |
} | |
_driverProcess.Close(); | |
} | |
_driverProcess.Dispose(); | |
_driverProcess = null; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment