我的应用程序中有两种不同的形式,表单输入数据加载可以使用UI中可用的预定义模板完成(即输入文本字段值将根据模板选择预先填充)。当数据加载发生时,不会发生元素渲染,只会填充输入字段值。为了确认数据加载,我使用等待如下
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(60));
wait.Until(x => x.FindElement(By.XPath(Element1_FieldXPath)).GetAttribute("value").Length > 1);
在任何时候,只有Form1或Form2可见。因此,我需要确认Form1或Form2中的数据加载。所以,我需要编写一个通用方法来处理这种情况,我遇到了以下解决方案,
public bool IsDataLoadingCompleted()
{
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(60));
try
{
wait.Until(x => x.FindElement(By.XPath(Form1_FieldXPath)).GetAttribute("value").Length > 1);
return true;
}
catch (TimeoutException)
{
try
{
wait.Until(x => x.FindElement(By.XPath(Form2_FieldXPath)).GetAttribute("value").Length > 1);
return true;
}
catch (TimeoutException)
{
return false;
}
}
}
在我的解决方案中,最糟糕的情况是等待时间是120s
,当form2在UI中可见时。我需要用单次等待而不是双等待来解决上述问题。
有没有其他方法可以用最少的等待时间来解决问题?
示例HTML:
Form1 - Field1 HTML:
<div class="form-group field field-string">
<label class="control-label" for="root_employeeName">Employee name</label>
<input class="form-control" id="root_employeeName" label="Employee name" placeholder="" type="text" value="">
</div>
Form2 - Field1 HTML:
<div class="form-group field field-string">
<label class="control-label" for="root_employeeAddress">Employee Address</label>
<input class="form-control" id="root_employeeAddress" label="Employee Address" placeholder="" type="text" value="">
</div>
您可以用逗号分隔CssSelectors来构造OR
逻辑,您可以使用以下任一解决方案:
CSS_SELECTOR A
:
new WebDriverWait(_driver, TimeSpan.FromSeconds(120)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector('#root_employeeName, #root_employeeAddress')));
CSS_SELECTOR B
:
new WebDriverWait(_driver, TimeSpan.FromSeconds(120)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("label[for='root_employeeName'], label[for='root_employeeAddress']")));
工作守则:
public bool IsDataLoadingCompleted(){
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(60));
try
{
wait.Until(x => x.FindElement(By.CssSelector("[label='Form1_Field'],[label='Form1_Field']")).GetAttribute("value").Length > 1);
return true;
}
catch (TimeoutException)
{
return false;
}
}
我假设一个表单总是显示,定位器有一个静态id
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
wait.Until(ExpectedConditions.ElementExists(By.CssSelector("input[class='form-control']")))
string elementid = FindElement(By.CssSelector("input[class='form-control']")).GetAttribute("id") // or GetAttribute("label")
if(elementid=="root_employeeName"){
// do your action here if name form is displayed
}
else if(elementid=="root_employeeAddress"){
// do you action here if Address form is displayed
}
您可以通过使用并行运行的任务来解决此问题。所需的代码看起来如下所示,Task.WaitAny
就是这样做的。
public static bool WaitUntilOneFormLoaded(IWebDriver driver)
{
var task1 = IsFormLoadedAsync(driver, "//some/xpath1");
var task2 = IsFormLoadedAsync(driver, "//some/xpath2");
Task.WaitAny(task1, task2);
var completedTask = Task.WaitAny(task1, task2);
switch (completedTask)
{
case 0: return task1.Result;
case 1: return task2.Result;
default: return false; // Timeout
}
}
public static Task<bool> IsFormLoadedAsync(IWebDriver driver, string xpath)
{
return Task.Run(() =>
{
try
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
wait.Until(x => driver.FindElement(By.XPath(xpath)).GetAttribute("value").Length > 1);
return true;
}
catch (TimeoutException)
{
return false;
}
});
}