WebDriver API c# .NET 中的 PageFactory.InitElements 问题

发布于 2024-11-16 23:12:33 字数 2128 浏览 2 评论 0原文

我正在尝试使用页面对象模式创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

°如果伤别离去 2024-11-23 23:12:33

问题是 PageFactory.InitElements() 返回 void。相反,它会修改您传入的页面。您的代码应如下所示:

public ResultsPage Search(string searchStatement)
{
    q.SendKeys(searchStatement);
    go.Click();
    ResultsPage page = new ResultsPage(_driver);
    PageFactory.InitElements(_driver, page);
    return page;
}

The problem is that PageFactory.InitElements() returns void. Rather, it modifies the page you've passed in. Your code should look something like this:

public ResultsPage Search(string searchStatement)
{
    q.SendKeys(searchStatement);
    go.Click();
    ResultsPage page = new ResultsPage(_driver);
    PageFactory.InitElements(_driver, page);
    return page;
}
风吹雨成花 2024-11-23 23:12:33

看起来 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

只是偏爱你 2024-11-23 23:12:33

使用 PageFactory.InitElements(_driver, this);在基页类的构造函数上。

public class Page
{
    public IWebDriver _driver;

    public Page(IWebDriver driver)
    {
        this._driver = driver;
        PageFactory.InitElements(_driver, this);
    }
}

Using PageFactory.InitElements(_driver, this); on the constructor of your base page class .

public class Page
{
    public IWebDriver _driver;

    public Page(IWebDriver driver)
    {
        this._driver = driver;
        PageFactory.InitElements(_driver, this);
    }
}
倥絔 2024-11-23 23:12:33

我同意吉姆埃文斯的观点。但不要将 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)
{

}
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文