C# 如何创建一个接受两类型lambda表达式的泛型方法?
我想扩展C#Selenium webdriver操作链,使其在链中有一个“WaitFor”方法 我已经检查了Selenium Webdriver的源代码,在查看它的过程中,我设法找到了以下代码块:C# 如何创建一个接受两类型lambda表达式的泛型方法?,c#,selenium,lambda,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 Sys
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"; } );
}
}
}
使用Microsoft.VisualStudio.TestTools.UnitTesting;
使用OpenQA.Selenium;
使用OpenQA.Selenium.PhantomJS;
使用OpenQA.Selenium.Support.UI;
使用制度;
命名空间My.Selenium.Extensions
{
公共类操作:OpenQA.Selenium.Interactions.Actions
{
公共操作(IWebDriver):基本(驱动程序){}
//注意:此行是目标状态
//我希望能够呼叫WebDriver或传入
//带有匿名代码求值的IWebElement
//通过使用selenium DefaultWait类,这应该允许
//动态链接事件,同时包括等待更复杂的
//动作执行
//等待的公共操作(T元素,Func条件)
//注2:这是唯一一个既可编译又可修改的版本
//通过下面的“测试”成功调用
等待的公共操作(T元素,Func条件)
{
DefaultWait=新的DefaultWait(元素);
等到(条件);
归还这个;
}
}
[测试类]
公共类动作测试
{
[测试方法]
公共无效WaitForTest(){
IWebDriver=new PhantomJSDriver();
IWebElement bar=driver.FindElement(By.Id(“bar”));
Actions a=新的My.Selenium.Extensions.Actions(驱动程序);
//请注意,这将通过编译器测试,但不会
//不一定按预期工作
a、 WaitFor(bar,(foo)=>{return foo.FindElement(By.CssSelector(“table”);});
//我最理想的做法是:
//a.WaitFor(bar,(bar)=>{返回bar.GetCssValue(“不透明度”)==“1.0”});
}
}
}
上面的代码经过编译(尽管我不太确定它是否真的能按预期工作)
我的最终目标是能够使用当前的C#webdriver“标准”ExpectedConditions
动态地编写“waitfor”条件,或者使用IWebElement
和lambda语法计算动态地编写我自己的动态计算
我的问题似乎是上面的WaitFor
类声明为Func
我被告知找不到类型或命名空间T2
项目来源如下:
和一些相关的课程
对于我试图模仿的例子:
预期条件:
和WebDriverWait:
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;
}
public Actions WaitFor<T, U>(T element, Func<T, U> condition)
{
DefaultWait<T> wait = new DefaultWait<T>(element);
wait.Until(condition);
return this;
}