- 1 year ago
How to "run as different user" in Winappdriver to lauch a desktop app?
Comment
1 / 1
To run a desktop app as a different user in WinAppDriver, you can use the WindowsIdentity.RunImpersonated
method in C#.
Here is an example code snippet that demonstrates how to launch a desktop app as a different user:
csharpusing System.Security.Principal;
using OpenQA.Selenium.Appium.Windows;
// Launch the app as a different user
WindowsIdentity.RunImpersonated(new WindowsIdentity("username", "domain", "password"), () =>
{
// Create a new session with the Appium driver
DesiredCapabilities appCapabilities = new DesiredCapabilities();
appCapabilities.SetCapability("app", "C:\\path\\to\\app.exe");
WindowsDriver<WindowsElement> appSession = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appCapabilities);
// Do something with the app
WindowsElement button = appSession.FindElementByName("Button");
button.Click();
// Close the app and quit the session
appSession.CloseApp();
appSession.Quit();
});
In this code, the WindowsIdentity.RunImpersonated
method is used to launch the app as a different user. The WindowsIdentity
constructor is used to create a new WindowsIdentity
object with the username, domain, and password of the user to run the app as. The WindowsIdentity.RunImpersonated
method takes a delegate that contains the code to run as the impersonated user. Inside the delegate, a new session with the WinAppDriver is created and the app is launched. Finally, the app is closed and the session is quit.