如何创建采用双类型lambda表达式的泛型方法

本文关键字:类型 lambda 表达式 泛型方法 何创建 创建 | 更新日期: 2023-09-27 18:35:36

我想扩展C# Selenium网络驱动程序操作链,以便它们在链中具有"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# Web驱动程序"标准"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

如何创建采用双类型lambda表达式的泛型方法

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;
}