WebDriver API c# .NET 中的 PageFactory.InitElements 问题
我正在尝试使用页面对象模式创建 WebDriver UI 测试框架,并使用以下 URL 作为参考: http://www.peternewhook.com/2010/09/automated-testing-pageobjects-webdriver/
按照示例,我创建了 3 个类(请参阅 以下)。问题出在 SearchPage 类的 Search 方法中的行 return PageFactory.InitElements(_driver, page);
中。
当我尝试构建时出现以下错误:
类型“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);
}
}
}
I am trying to create WebDriver UI tests framework using Page Object pattern, using the following URL as a reference: http://www.peternewhook.com/2010/09/automated-testing-pageobjects-webdriver/
As per example I have created 3 classes (see below). The problem is with the line return PageFactory.InitElements(_driver, page);
in the Search method of the SearchPage class.
When I try to build I get the following error:
The type 'OpenQA.Selenium.ISearchContext' is defined in an assembly that is not referenced. You must add a reference to assembly 'WebDriver
Fair enough, as I am referencing WebDriver.Common.dll, so I tried removing it and added WebDriver.dll to my References and all of a sudden I get the following when I build:
Cannot implicitly convert type 'void' to 'ConsoleApplication1.ResultsPage'
and it fails on the same line; when I hover over it, it says:
Cannot convert expression type 'void' to 'ConsoleApplication1.ResultsPage'.
I also tried referencing both assemblies and thought I could use different usings but it is a no-go, didn't work.
Why can't PageFactory.InitElements be returned when using WebDriver.dll?
Is there a way around it, or can I achieve the same result by changing the architecture slightly?
Your help is much appreciated. Thanks.
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是
PageFactory.InitElements()
返回 void。相反,它会修改您传入的页面。您的代码应如下所示:The problem is that
PageFactory.InitElements()
returns void. Rather, it modifies the page you've passed in. Your code should look something like this:看起来 C# PageFactory 不会按照以下方式初始化私有超类元素(通过 PageFactory.InitElements):
http://code.google.com/p/selenium/issues/detail?id=1189#makechanges
Looks like C# PageFactory does not init private superclass elements (via PageFactory.InitElements) per this:
http://code.google.com/p/selenium/issues/detail?id=1189#makechanges
使用 PageFactory.InitElements(_driver, this);在基页类的构造函数上。
Using PageFactory.InitElements(_driver, this); on the constructor of your base page class .
我同意吉姆埃文斯的观点。但不要将 PAgeFactory 代码放在单独的位置,而是将其放在结果页面的构造函数中。
您的结果页面类应该是这样的。
公开课成绩
{
公共结果(驱动程序,这个)
{
}
}
I agree with JimEvans. But instead of having PAgeFactory code in seperate place, have that in constructor of Results page.
Your results page class should be something like this.
public class result
{
public result(driver,this)
{
}
}