0

我想扩展 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

4

1 回答 1

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;
}
于 2013-10-16T21:42:54.623 回答