如何解决WebDriver API c#.NET中的PageFactory.InitElements问题
|| 我试图使用以下URL作为参考,使用Page Object模式创建WebDriver UI测试框架:http://www.peternewhook.com/2010/09/automated-testing-pageobjects-webdriver/ 作为示例,我创建了3个类(请参见下文)。问题出在SearchPage类的Search方法中的行“ 0”。 当我尝试构建时,出现以下错误: 在未引用的程序集中定义类型'OpenQA.Selenium.ISearchContext \'。您必须添加对程序集\'WebDriver的引用 相当公平,因为我正在引用WebDriver.Common.dll,所以我尝试将其删除并将WebDriver.dll添加到“引用”中,并且在构建时突然得到以下信息: 无法将类型\'void \'隐式转换为\'ConsoleApplication1.ResultsPage \' 它在同一行上失败;当我将鼠标悬停在上面时,它说: 无法将表达式类型\'void \'转换为\'ConsoleApplication1.ResultsPage \'。 我也尝试同时引用这两个程序集,并认为我可以使用不同的用法,但这是不行的,没有用。 使用WebDriver.dll时为什么不能返回PageFactory.InitElements? 有没有解决的办法,还是可以通过略微更改体系结构来达到相同的结果? 非常感谢您的帮助。谢谢。using OpenQA.Selenium;
namespace ConsoleApplication1
{
public class Page
{
public IWebDriver _driver;
public Page(IWebDriver driver)
{
this._driver = driver;
}
}
}
using OpenQA.Selenium;
namespace ConsoleApplication1
{
public class ResultsPage : Page
{
public ResultsPage(IWebDriver driver)
: base(driver)
{
}
private IWebElement count;
public string GetPagesReturned()
{
return count.Text;
}
}
}
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
namespace ConsoleApplication1
{
public class SearchPage : Page
{
public SearchPage(IWebDriver driver) : base(driver)
{
}
private IWebElement q;
private IWebElement go;
public ResultsPage Search(string searchStatement)
{
q.SendKeys(searchStatement);
go.Click();
ResultsPage page = new ResultsPage(_driver);
return PageFactory.InitElements(_driver,page);
}
}
}
解决方法
问题在于
PageFactory.InitElements()
返回void。而是,它修改了您传入的页面。您的代码应如下所示:
public ResultsPage Search(string searchStatement)
{
q.SendKeys(searchStatement);
go.Click();
ResultsPage page = new ResultsPage(_driver);
PageFactory.InitElements(_driver,page);
return page;
}
, 看起来C#PageFactory不会按以下方式初始化私有超类元素(通过PageFactory.InitElements):
http://code.google.com/p/selenium/issues/detail?id=1189#makechanges
, 使用PageFactory.InitElements(_driver,this);在您的基页类的构造函数上。
public class Page
{
public IWebDriver _driver;
public Page(IWebDriver driver)
{
this._driver = driver;
PageFactory.InitElements(_driver,this);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。