我需要有关如何使用WinAppDriver在WPF应用程序中打开新窗口时如何更改焦点的帮助
使用WinAppDriver,我可以打开WPF应用程序并使用以下代码进行测试, 我需要有关在WPF应用程序中打开新窗口时如何更改焦点的帮助。 WPF应用程序的问题在于您不能使用标准方法,因为驱动程序将无法识别该应用程序。应该有一种方法可以帮助切换焦点或至少打开一个我现在无法执行的现有WPS应用程序窗口。如果有人可以帮忙,我将不胜感激。
// to open the standard app
AppiumOptions options2 = new AppiumOptions();
options2.AddAdditionalCapability("app", "Application new window");
options2.AddAdditionalCapability("Window", "WindowsPC");
//to open a WPS application
AppiumOptions options = new AppiumOptions();
options.AddAdditionalCapability("app", @"C:\Path\Debug\Application.exe");
options.AddAdditionalCapability("deviceName", "WindowsPC");
_driver = new WindowsDriver(new Uri("http://127.0.0.1:4723"), options);
{"desiredCapabilities":{"app":"[@Name =\"Loan Folder 14847\"]","Window":"WindowsPC","platformName":"Windows"},"capabilities":{"firstMatch":[{"platformName":"Windows"}]}}
HTTP/1.1 500 Internal Error
Content-Length: 101
Content-Type: application/json
{"status":13,"value":{"error":"unknown error","message":"The system cannot find the file specified"}}
答案 0 :(得分:1)
我能够弄清楚如何专注于WPF应用程序中正在打开的新窗口,并希望发布我的解决方案来帮助以下人员:
//Setup
private WindowsDriver _driver;
AppiumOptions options = new AppiumOptions();
options.AddAdditionalCapability("app", @"C:\Desktop\Debug\application.exe");
options.AddAdditionalCapability("deviceName", "WindowsPC");
_driver = new WindowsDriver(new Uri("http://127.0.0.1:4723"), options);
_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
//Test
_driver.FindElementByAccessibilityId("Login").Click();
Thread.Sleep(5000);
_driver.FindElementByName("button").Click();
//Opens a new window
_driver.FindElementByName("Open").Click();
Thread.Sleep(2000);
//Solution - switches to the latest window
_driver.SwitchTo().Window(_driver.WindowHandles.First());
//can click on buttons in the new Window
_driver.FindElementByName("buttons").Click();
//Opens another new Window
_driver.FindElementByName("Open another window").Click();
Thread.Sleep(2000);
//Solution - switches to the latest window
_driver.SwitchTo().Window(_driver.WindowHandles.First());
//can click on buttons in the new Window
_driver.FindElementByName("button3").Click();
答案 1 :(得分:0)
使用WindowHandles的另一种方法是通过WindowsElement API查找window元素。然后,您可以使用元素API查找窗口的子级。
下面的示例打开一个新窗口,然后通过在新窗口上找到“关闭”控件来关闭它。
[Test]
public void TestAboutWindow()
{
// Open About window through menu
_session.FindElementByName("Help").Click();
_session.FindElementByName("About").Click();
// Close child window through WindowElement API
var aboutWindow = _session.FindElementByName("AboutWindow");
aboutWindow.FindElementByName("Close").Click();
}
答案 2 :(得分:0)
您可以使用WinAPI。
[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(IntPtr hWnd);
hWnd
是Window的NativeWindowHandle
属性。
string windowHandle = windowElement.GetAttribute("NativeWindowHandle");
return new IntPtr(long.Parse(windowHandle));