C# 如何在C中的UWP上使用WinAppDriver应用等待元素#
我正在使用WinAppDriver和C#实现UWP应用程序的自动化,但在某些情况下,我的测试失败了,因为有些元素需要时间才能显示,我无法应用waitDriverWait(我猜WAD没有这个功能),所以我找到了下面的代码,这些代码正在工作,但我想知道是否有更好的方法C# 如何在C中的UWP上使用WinAppDriver应用等待元素#,c#,uwp,automation,appium,winappdriver,C#,Uwp,Automation,Appium,Winappdriver,我正在使用WinAppDriver和C#实现UWP应用程序的自动化,但在某些情况下,我的测试失败了,因为有些元素需要时间才能显示,我无法应用waitDriverWait(我猜WAD没有这个功能),所以我找到了下面的代码,这些代码正在工作,但我想知道是否有更好的方法 public static void WaitForElement(this WindowsDriver<WindowsElement> driver, string IDType, string elementName
public static void WaitForElement(this WindowsDriver<WindowsElement> driver, string IDType, string elementName, int time = 10000)
{
var wait = new DefaultWait<WindowsDriver<WindowsElement>>(BasePage.WindowsDriver)
{
Timeout = TimeSpan.FromSeconds(time),
PollingInterval = TimeSpan.FromSeconds(0.5)
};
wait.IgnoreExceptionTypes(typeof(InvalidOperationException));
wait.Until(driver1 =>
{
int elementCount = 0;
switch (IDType)
{
case "id":
elementCount = driver1.FindElementsByAccessibilityId(elementName).Count;
break;
case "xpath":
elementCount = driver1.FindElementsByXPath(elementName).Count;
break;
case "name":
elementCount = driver1.FindElementsByName(elementName).Count;
break;
}
return elementCount > 0;
});
}
public static void WaitForElement(此WindowsDriver驱动程序,string IDType,string elementName,int time=10000)
{
var wait=new DefaultWait(BasePage.WindowsDriver)
{
超时=TimeSpan.FromSeconds(时间),
PollingInterval=从秒开始的时间跨度(0.5)
};
wait.IgnoreExceptionTypes(typeof(invalidooperationexception));
等待。直到(driver1=>
{
int elementCount=0;
开关(IDType)
{
案例“id”:
elementCount=driver1.FindElementsByAccessibilityId(elementName).Count;
打破
案例“xpath”:
elementCount=driver1.FindElementsByXPath(elementName.Count);
打破
案例“名称”:
elementCount=driver1.FindElementsByName(elementName.Count);
打破
}
返回元素计数>0;
});
}
过去,我在WinAppDriver automation中成功地使用了WebDriverWait
——您必须安装DotNetSeleniumExtras.WaitHelpers
才能访问ExpectedConditions
类,因为它现在在标准Selenium命名空间中已过时,但是我没有看到任何东西阻止你使用WebDriverWait
正在等待用值填充元素文本:
var Driver = new WindowsDriver<WindowsElement>();
var myElement = Driver.FindElementByAccessibilityId("someId");
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(30));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.TextToBePresentInElement(myElement, "text"));
如前所述,我在过去的WinAppDriver
中使用了这些精确的等待方法,并取得了成功。请让我知道您在使用这些方法时遇到了哪些问题
var Driver = new WindowsDriver<WindowsElement>();
var myElement = Driver.FindElementByAccessibilityId("someId");
new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until(d => myElement.Displayed);
var Driver = new WindowsDriver<WindowsElement>();
new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until(d => d.FindElements(By.Name("someName")).Count > 0);