如何创建一个采用两种类型的lambda表达式的泛型方法?

时间:2013-10-16 19:57:23

标签: c# selenium lambda

我想扩展C#Selenium webdriver动作链,使它们在链中有一个“WaitFor”方法。

我已经查看了Selenium Webdriver的源代码,在查看它之后,我已经成功了解了以下代码块:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.PhantomJS;
using OpenQA.Selenium.Support.UI;
using System;

namespace My.Selenium.Extensions
{
    public class Actions : OpenQA.Selenium.Interactions.Actions
    {
        public Actions(IWebDriver driver) : base(driver) { }
        //  NOTE: this line is the GOAL state 
        //     I would like to be able to call to the WebDriver OR to pass in  
        //     an IWebElement along with an anonymous code evaluation
        //     by using the selenium DefaultWait<T> class this should allow
        //     dynamic chaining of events while including waits for more complex
        //     action execution
        //  public Actions WaitFor<T>(T element, Func<T, TResult> condition)

        // NOTE2: this is the only version that will both compile and can be
        //        successfully called via the "test" below
        public Actions WaitFor<T>(T element, Func<T, T> condition)
        {
            DefaultWait<T> wait = new DefaultWait<T>(element);
            wait.Until(condition);
            return this;
        }
    }

    [TestClass]
    public class ActionTests
    {
        [TestMethod]
        public void WaitForTest() {
            IWebDriver driver = new PhantomJSDriver();
            IWebElement bar = driver.FindElement(By.Id("bar"));
            Actions a = new My.Selenium.Extensions.Actions(driver);
            // Note that this will pass the compiler test, but does 
            // not necessarily work as intended
            a.WaitFor(bar, (foo) => { return foo.FindElement(By.CssSelector("table")); } );
            // what I would ideally like to do is more like:
            // a.WaitFor(bar, (bar) => { return bar.GetCssValue("opacity") == "1.0"; } );
        }
    }
}

上面的代码编译(虽然我不太确定它实际上是按预期工作的)

我的最终目标是能够使用当前的C#webdriver“标准”ExpectedConditions或我自己的动态评估动态地使用IWebElement和lambda语法动态制作“等待”条件评价。

我的问题似乎是在上面的WaitFor<T>类声明为Func<T,T2>我被告知无法找到类型或命名空间T2。

项目来源位于:https://code.google.com/p/selenium/source/browse/

有一些相关的课程

https://code.google.com/p/selenium/source/browse/dotnet/src/webdriver/Interactions/Actions.cs

https://code.google.com/p/selenium/source/browse/dotnet/src/support/UI/DefaultWait.cs

我正在尝试建模的例子:

预期条件: https://code.google.com/p/selenium/source/browse/dotnet/src/support/UI/ExpectedConditions.cs

和WebDriverWait: https://code.google.com/p/selenium/source/browse/dotnet/src/support/UI/WebDriverWait.cs

1 个答案:

答案 0 :(得分:2)

WaitFor方法需要一个返回相同类型对象的函数作为参数:

public Actions WaitFor<T>(T element, Func<T, T> condition)

如果您希望提供的函数是适当的条件,请尝试此版本的方法:

public Actions WaitFor<T>(T element, Func<T, bool> condition)
{
    DefaultWait<T> wait = new DefaultWait<T>(element);
    wait.Until(condition);
    return this;
}

或者对于更通用的表单(如果您原谅双关语),您可以使用第二个泛型类型占位符替换显式bool,如下所示:

public Actions WaitFor<T, U>(T element, Func<T, U> condition)
{
    DefaultWait<T> wait = new DefaultWait<T>(element);
    wait.Until(condition);
    return this;
}