我在用于Selenium测试的方法中有以下代码的几种变体(在返回之前等待某些事件)并且我想重构它并使其可重用,所以我有控制延迟和逻辑的逻辑。 try / catch作为通用方法,但能够根据情况交换进出条件。
有没有简单的方法来实现这一目标?
代码:
for (int second = 0; second <= 10; second++)
{
try
{
// bit that needs to vary
matchedAddresses = driver.FindElements(By.ClassName("addresslookup"));
if (matchedAddresses.Count > 0)
{
break;
}
}
catch (Exception)
{
}
Thread.Sleep(1000);
}
return matchedAddresses.Count;
答案 0 :(得分:1)
您希望函数接受类似Func<int>
之类的参数 - 返回元素数量的方法(或枚举Func<IEnumerable<sometype>>
)
public int GetCountOfElementsWithWait(Func<int> test)
{
.....
var count = test();
....
}
答案 1 :(得分:0)
似乎有点太明显了,但这会有用吗?
public int GetCountOfElementsByClassName(string className)
{
for (int second = 0; second <= 10; second++)
{
try
{
// bit that needs to vary
matchedElements = driver.FindElements(By.ClassName(className));
if (matchedElements.Count > 0)
{
break;
}
}
catch (Exception)
{
}
Thread.Sleep(1000);
}
return matchedElements.Count;
}