【发布时间】: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