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