如何在 UWP 中使用 WinAppDriver 等待元素? [英] How do I wait for an element using the WinAppDriver in UWP?
问题描述
我目前正在使用 WinAppDriver 将 UWP 应用的编码 UI 测试迁移到 Appium 和我遇到了这个问题,我等不及某个元素出现了.没有办法像 Microsoft 的编码 UI 测试那样等待元素准备好".
I'm currently migrating my Coded UI Tests for a UWP app to Appium using the WinAppDriver and I have come across the issue, that I can't wait for an element to show up. There is no way to wait for a element to be "ready" as the Coded UI Test from Microsoft did.
在 ClassInitialize
方法中,一切正常(数据在登录视图中输入)并单击登录按钮.触发点击事件后,应用程序会显示一个进度条,直到用户登录.我的问题是登录过程后我无法等待组件.
In the ClassInitialize
method everything works fine (data is entered on the login view) and the login button is clicked. After the click event is triggered the app shows a progress bar until the user is logged in. My problem is that I cannot wait for the components after the login process.
我发现了一些代码片段,但是,它们似乎对我不起作用.这是我目前使用的扩展方法:
I have found some code snippets, however, they don't seem to work for me. Here is the extension method I'm currently using:
public static IWebElement WaitForElement(this IWebDriver driver, By by, int timeoutInSeconds)
{
if (timeoutInSeconds > 0){
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(timeoutInSeconds);
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(ExpectedConditions.ElementIsVisible(by));
}
return driver.FindElement(by);
}
我还了解到必须设置 Windows 驱动程序的隐式超时:
I have also read that the implicit timeout for the Windows driver has to be set:
session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);
session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(60);
并在 WaitForElement
方法中被覆盖,这对我也不起作用.
and overridden in the WaitForElement
method, which also didn't work for me.
[TestMethod]
public void UploadDocuments()
{
var UploadButton = session.WaitForElement(By.XPath("//Button[@AutomationId='AddDocument']"), 60);
UploadButton.Click();
session.FindElementByXPath("//ToolbarWindow32[@AutomationId='1001']").SendKeys(Keys.Control + "a");
session.FindElementByXPath("//ToolbarWindow32[@AutomationId='1001']").SendKeys(testFilesFolder);
//session.FindElementByName("Open").Click();
}
在使用 ClassInitialize
完成后,测试通常会在第一行崩溃.所以我想在测试继续之前等待'AddDocument'按钮弹出.
The test usually crashes in the first line after it's finished with the ClassInitialize
. So I would like to wait for the 'AddDocument' button to pop up before the test carries on.
如果有人有解决方案,我将不胜感激.谢谢!
If anyone has a solution I'd appreciate the help. Thanks!
推荐答案
您可以像这样实现等待功能:
You can implement wait funcionalities like this:
public WindowsElement GetElementByAutomationID(string automationId, int timeOut = 10000)
{
WindowsElement element = null;
var wait = new DefaultWait<WindowsDriver<WindowsElement>>(Driver)
{
Timeout = TimeSpan.FromMilliseconds(timeOut),
Message = $"Element with automationId \"{automationId}\" not found."
};
wait.IgnoreExceptionTypes(typeof(WebDriverException));
try
{
wait.Until(Driver =>
{
element = Driver.FindElementByAccessibilityId(automationId);
return element != null;
});
}
catch (WebDriverTimeoutException ex)
{
LogSearchError(ex, automationId);
Assert.Fail(ex.Message);
}
return element;
}
您的问题似乎是 appium-dotnet-driver 问题.在 github 上查看这些问题:https://github.com/Microsoft/WinAppDriver/issues/329
Your problem seems to be a appium-dotnet-driver issue. See these issues on github about this: https://github.com/Microsoft/WinAppDriver/issues/329
https://github.com/appium/appium-dotnet-driver/issues/225
这篇关于如何在 UWP 中使用 WinAppDriver 等待元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!