2017-04-13 67 views
0

泛型方法所以我创造了这个通用寻找元素功能:为硒FindElement功能

public static IWebElement FindElement(IWebDriver driver, Func<IWebDriver, IWebElement> expectedCondtions, int timeoutInSeconds) 
    { 
     WebDriverWait webDriverWait = CreateWebDriverWait(driver, finder,timeoutInSeconds); 
     webDriverWait.IgnoreExceptionTypes(typeof(NoSuchElementException)); 
     return webDriverWait.Until(expectedCondtions); 
    } 

    public static ReadOnlyCollection<IWebElement> FindElements(IWebDriver driver, Func<IWebDriver, ReadOnlyCollection<IWebElement>> expectedCondtions, int timeoutInSeconds) 
    {   
     WebDriverWait webDriverWait = CreateWebDriverWait(driver, finder, timeoutInSeconds); 
     webDriverWait.IgnoreExceptionTypes(typeof(NoSuchElementException));    
     return webDriverWait.Until(expectedCondtions); 
    } 

    private static WebDriverWait CreateWebDriverWait(IWebDriver driver, IWebElement finder, int timeoutInSeconds) 
    { 
     WebDriverWait webDriverWait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds)); 
     webDriverWait.IgnoreExceptionTypes(typeof(NoSuchElementException)); 
     return webDriverWait; 
    } 

用法:

 IWebElement element= 
      WaitAndFindElement(
      driver, 
      ExpectedConditions.ElementIsVisible(By.CssSelector("...")), 
      120); 

现在我万特添加到找到element也没有使用driver的选项。 例如,而不是driver.FindElement我想从另一个element搜索元素:

IWebElemen element = ... 
element.FindElement... 

所以我想从改变我的函数签名:

IWebElement FindElement(IWebDriver driver,Func<IWebDriver, IWebElement> expectedCondtions, int timeoutInSeconds) 

要:

IWebElement FindElement(IWebDriver driver, IWebElement finder, Func<IWebDriver, IWebElement> expectedCondtions, int timeoutInSeconds) 

如果finder为空我想用driver.FindElement进行搜索。 否则:finder.FindElement

所以我的问题是如何做到这一点?

+0

你需要驱动程序的第二个场景?听起来像是这样或那样,这是正确的吗? – Delta

+0

为什么要为简单的Selenium函数创建一个包装?这完成了什么?无论如何,你可以在一两行中完成大部分工作。每次调用函数时,即使它使用相同的超时值,也会创建一个新的'WebDriverWait'实例。 – JeffC

回答

0
Class WebElementFinder 
{ 
    public static IWebElement FindElement(ISearchContext sc, By locator, Func<IWebElement, bool> elementCondition = null, int timeOutInceconds = 20) 
     { 
      DefaultWait<ISearchContext> wait = new DefaultWait<ISearchContext>(sc); 
      wait.Timeout = TimeSpan.FromSeconds(timeOutInceconds); 
      wait.PollingInterval = TimeSpan.FromSeconds(3); 
      wait.IgnoreExceptionTypes(typeof(NoSuchElementException)); 

      return wait.Until(x => GetElement(x, locator, elementCondition)); 
     } 

     private static IWebElement GetElement(ISearchContext sc, By locator, Func<IWebElement, bool> elementCondition = null) 
     { 
      IWebElement webElement = sc.FindElement(locator); 
      if(elementCondition != null) 
      { 
       if (elementCondition(webElement)) 
        return webElement; 
       else 
        return null; 
      } 
      else 
      { 
       return webElement; 
      } 
     } 
} 

用法:

Func<IWebElement, bool> isElementVisible = (webElement) => webElement.Displayed; 
      var element = FindElement(driver, By.Id("name_10"), isElementVisible); 
+0

https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/SearchContext.html –

0

这是一件好事:我在工作中有类似的情况,这就是我在C#中做的:

IWebElement FindElement(IWebDriver driver, Func<IWebDriver, IWebElement> expectedCondtions, int timeoutInSeconds, IWebElement finder = null) 

这意味着,“发现者”的默认值是空的,你可以调用你的功能有没有指定它。 (您可以将此参数与默认值一起添加到您的所有方法中)

然后,您可以在函数中简单地做一个简单的if语句以确定如何找到该元素。

if(finder != null) 
{ 
    //use finder instead of driver and return 
} 
//Otherwise use driver 
+0

是的,但我怎样才能使用这个发现者,而不是我的功能内的驱动程序?我可以有代码示例吗? – user979033

+0

我问它,因为我没有看到任何提及driver.FindElement在我的函数 – user979033

0

我想你不需要使它变得复杂,如果你想使它通用。 这里是一个能解决问题

Class WebElementFinder 
{ 
private static IWebElement FindElement(ISearchContext sc, By locator, int timeOutInceconds = 20) 
     { 
      DefaultWait<ISearchContext> wait = new DefaultWait<ISearchContext>(sc); 
      wait.Timeout = TimeSpan.FromSeconds(timeOutInceconds); 
      wait.IgnoreExceptionTypes(typeof(NoSuchElementException)); 
      return wait.Until(x => GetElement(x, locator)); 
     } 

     private static IWebElement GetElement(ISearchContext sc, By locator) 
     { 
      return sc.FindElement(locator); 
     } 
} 

让我解释一下这段代码,这将帮助你了解你提到的代码问题的某些部分的代码。

  1. 如果您使用webdriver等待,那么它从DefaultWait类驱动,它将IWebDriver转换为泛型类型。因此,直到方法将有使用驱动程序对象的限制。
  2. 如果您在方法中使用Func,则每个调用方法都需要为find元素创建函数。但是,您可以将其用作可选参数。

现在回到我的代码片段。 我已经使用DefaultWait可以用于WebDriver以及IWebElement。两者都来自ISearchContext。 定位器将帮助查找网页元素上的驱动器/ IWebElement对象为您在您的文章问

+0

那么使用expectedCondtions怎么样? – user979033

+0

,因为我在每个web元素上寻找不同的expectedCondtions(可点击,存在......) – user979033

+0

您可以使用一个多个参数编写修改方法.. private static IWebElement FindElement(ISearchContext sc,By locator,Func elementCondition = null,int timeOutInceconds = 20)....你可以通过委托来确定你想要验证的web元素。 –