【问题标题】:C# Selenium WebDriver Switch/CaseC# Selenium WebDriver 开关/案例
【发布时间】:2017-10-30 16:35:31
【问题描述】:
public enum ElementType
{
    Id,
    ClassName,
    Name,
    XPath,
    CssSelector,
    LinkText
}

public static class WebDriverExtensions
{

    public static void AssertElementDisplayed(this IWebDriver driver, ElementType elementType, string element)
    {
        if (elementType == ElementType.Id)
            Assert.IsTrue(driver.FindElement(By.Id(element)).Displayed);
        if (elementType == ElementType.ClassName)
            Assert.IsTrue(driver.FindElement(By.ClassName(element)).Displayed);
        if (elementType == ElementType.Name)
            Assert.IsTrue(driver.FindElement(By.Name(element)).Displayed);
        if (elementType == ElementType.XPath)
            Assert.IsTrue(driver.FindElement(By.XPath(element)).Displayed);
        if (elementType == ElementType.CssSelector)
            Assert.IsTrue(driver.FindElement(By.CssSelector(element)).Displayed);
        if (elementType == ElementType.LinkText)
            Assert.IsTrue(driver.FindElement(By.LinkText(element)).Displayed);
    }

    public static void WaitForElementPresent(this IWebDriver driver, ElementType elementType, string element)
    {
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

        if (elementType == ElementType.Id)
            wait.Until(ExpectedConditions.ElementIsVisible(By.Id(element)));
        if (elementType == ElementType.ClassName)
            wait.Until(ExpectedConditions.ElementIsVisible(By.ClassName(element)));
        if (elementType == ElementType.Name)
            wait.Until(ExpectedConditions.ElementIsVisible(By.Name(element)));
        if (elementType == ElementType.XPath)
            wait.Until(ExpectedConditions.ElementIsVisible(By.XPath(element)));
        if (elementType == ElementType.CssSelector)
            wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector(element)));
        if (elementType == ElementType.LinkText)
            wait.Until(ExpectedConditions.ElementIsVisible(By.LinkText(element)));
    }

我正在寻找将其转换为 switch case 语句而不是使用 if 语句的最佳方法,但不确定实现此目的的最佳方法。我需要一种简单的方法来在 By.Id、By Class、By.Name 等之间切换...

【问题讨论】:

  • 只写一个普通的switch语句?这到底是什么问题?

标签: c# selenium switch-statement case


【解决方案1】:

无需传递所有这些额外信息。只需传递一个 By 定位器,它会处理其余的工作并使代码更简单。

public static void AssertElementDisplayed(this IWebDriver driver, By locator)
{
    Assert.IsTrue(driver.FindElement(locator).Displayed);
}

public static void WaitForElementPresent(this IWebDriver driver, By locator, int timespan)
{
    new WebDriverWait(driver, TimeSpan.FromSeconds(timespan)).Until(ExpectedConditions.ElementIsVisible(locator));
}

【讨论】:

  • 发布了所有这些,我认为这不是解决这个问题的正确方法(每个方法都有一个单独的方法)。此代码应该进入您的脚本本身...Assert.IsTrue(driver.FindElement(...).Displayed); 等等。没有太多理由编写包装单行代码的方法。
  • 我把它作为操作员的唯一原因是因为多个人将在同一个项目上工作,有些人的经验比其他人少,可以简单地使用该方法。
  • 这真的不是一个好理由。向他们展示做事的正确方法与将他们指向做同样事情的函数不再需要任何时间。为他们创建备忘单或编写最佳实践、常见问题等的文档。他们将有它参考并最终编写更好的代码。为一些复杂的东西保存辅助方法。
【解决方案2】:

我对 Selenium 不是很熟悉,但是这样的东西可以吗?

private By GetElement(ElementType type, string element)
{
    switch(ElementType)
    {
        case ElementType.Id:
            return By.Id(element);
        // add your other cases here!
    }
}

然后你可以在你的方法中使用它,如下所示:

public static void WaitForElementPresent(this IWebDriver driver, ElementType elementType, string element)
{
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

    wait.Until(ExpectedConditions.ElementIsVisible(GetElement(elementType, element)));
}

【讨论】:

  • 我敢打赌这是因为 OP 正在执行断言,而您只是返回“by”。 耸耸肩 但我同意,downvote 需要对它进行必要的评论,因为如果没有解释为什么它被否决,downvote 是没有帮助的。
  • @IamBatman 我提出了一个新方法,它只返回一个 By,这似乎正是使用它的地方所需要的......如果 Selenium 不接受通用 By 而不是By 的子类,例如 By.Id 我会感到惊讶,但随后可以将开关移至原始方法。
  • 是的,我知道,只是假设这就是它被否决的原因。任何真正的程序员都可以拿走你写的东西并弄清楚你在做什么。
猜你喜欢
  • 1970-01-01
  • 2013-09-13
  • 1970-01-01
  • 1970-01-01
  • 2022-10-07
  • 1970-01-01
  • 2016-12-03
  • 2017-11-30
  • 2022-11-02
相关资源
最近更新 更多