gpt4 book ai didi

C# Selenium WebDriver 开关/案例

转载 作者:行者123 更新时间:2023-12-02 17:39:01 26 4
gpt4 key购买 nike

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 等之间切换...

最佳答案

无需传递所有这些额外信息。只需传递一个 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));
}

关于C# Selenium WebDriver 开关/案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47020159/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com