在测试用例需要时使用助手 class 中的显式等待

Explicit Wait in helper class to be used when needed by test cases

我正在尝试创建一个帮助程序 class 来保存对 Selenium WebDriver 的不同等待。这是为了在创建测试用例时为用户提供更大的灵活性。我在使用显式等待的助手 class 时遇到了一些问题。

测试用例

[Test]
[TestCase(Browser.Chrome)]
public void ValidateExpicitWait(Browser browser)
{
    Driver = StaticWebDriverFactory.GetLocalWebDriver(browser);
    Driver.Url = "https://example.com/";
    WaitsHelper.SetExplicitWait(Driver, ElementIdentifierType.LinkText, "More information...", 10);

    var title = Driver.Title;
    Assert.AreEqual(true, title.Contains("Example Domain"), $"Expected title does not match actual: {title}", title);
}

显式等待 Class

public static void SetExplicitWait(IWebDriver driver, ElementIdentifierType identifierType, string identifer, int timeout = 10)
{
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));
    IWebElement element = wait.Until<IWebElement>((d) =>
    {
        return d.FindElement(By.identifierType(identifer));
    });

}

来自 d.FindElement(By.identifierType(identifier));

identifierType 有问题

错误

'By' does not contain a definition for 'identifierType

By class 无法将 Enum 转换为静态方法,它没有 identifierType 方法。您可以发送 By 对象而不是

public static void SetExplicitWait(IWebDriver driver, By by, int timeout = 10)
{
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));
    IWebElement element = wait.Until<IWebElement>((d) =>
    {
        return d.FindElement(by);
    });
}

WaitsHelper.SetExplicitWait(Driver, By.LinkText("More information..."), 10);