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)));
}
これは、if文を使用するのではなく、これを実現するための最良の方法がわからないスイッチケース文に変換する最良の方法を探しています。 By.Id、Byクラス、By.Nameなどを切り替える簡単な方法が必要です。C#Selenium WebDriverスイッチ/ケース
は単なるswitch文を書くのか?何が問題なのですか? – j4nw