0

我在用于 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;
4

2 回答 2

1

You want function that takes argument of something like Func<int> - method that returns number of elements (or enumerable Func<IEnumerable<sometype>>)

public int GetCountOfElementsWithWait(Func<int> test)
{
    .....
    var count = test();
    ....
}
于 2013-03-18T16:03:19.387 回答
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;
}
于 2013-03-18T15:57:01.810 回答