是否有使用net core的PageFactory的替代方案

问题描述 投票:2回答:4

我使用的是Selenium v​​3.6.0和.NET Core 2.0,下面的代码在PageFactory.InitElements上给出了一个错误,说它在currentnct上下文中不存在。

using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;

namespace Example
{
    public class Form
    {
        [FindsBy(How = How.Name, Using = "Filter")] // This does exist in current context using .NET Core
        public IWebElement Filter { get; set; }

        [FindsBy(How = How.TagName, Using = "Button")]
        public IWebElement Button;

        public Form(IWebDriver driver)
        {
            PageFactory.InitElements(driver, this); // This doesn't exist in current context using .NET Core
        }
    }
}

我对此有点困惑,因为属性FindsBy,FindsByAll和FindsBySequence都可以在OpenQa.Selenium.Support.PageObjects命名空间中使用,但是PageFactory不是。据我所知,这些属性仅适用于PageFactory。

使用.NET Core是否有不同的方法,或者它尚未实现?

c# selenium selenium-webdriver .net-core pageobjects
4个回答
2
投票

WebDriver.Support.dll中没有适用于版本3.6.0的PageFactory类(在Visual Studio中,您可以在对象资源管理器中打开此dll并查看没有此类)。所以你只有通常的编译错误。

我查看了github https://github.com/SeleniumHQ/selenium/blob/master/dotnet/src/support/PageObjects/PageFactory.cs上的源代码,并在PageFactory类中查看了预处理器指令#if!NETSTANDARD2_0 ... #endif。我不知道为什么NETSTANDARD2_0会影响NETCORE2_0,并且不确定这是真正的原因,但对于我们来说,作为库用户,PageFactory现在是无法访问的。


2
投票

我正在使用WebDriver V3.141.0.0和.NET v4.5,您可以按如下方式替换代码:

using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;

namespace Example
{
    public class Form
    {
        public IWebElement Filter => Driver.FindElement(By.Name("Filter"));   
        /*
        [FindsBy(How = How.Name, Using = "Filter")] // This does exist in current context using .NET Core
        public IWebElement Filter { get; set; }
        */

        public IWebElement Button => Driver.FindElement(By.TagName("Button"));   
        /*
        [FindsBy(How = How.TagName, Using = "Button")]
        public IWebElement Button;
        */
        public Form(IWebDriver driver)
        {
            //PageFactory.InitElements(driver, this); // This doesn't exist in current context using .NET Core
        }
    }
}

1
投票

是的,不支持此视频https://www.youtube.com/watch?v=xd5TCWmaxGI中提到的

谢谢,Karthik Kaka


0
投票

并非特定于.Net Core,但对于.Net Standard 2.0和.Net Framework 3.5,4.0和4.5,您应该能够将DotNetSeleniumExtras.PageObjects添加到您的项目中,以便在OpenQA.Selenium正式删除它时获取PageFactory功能。您可以通过以下方式引用它:使用SeleniumExtras.PageObjects;

© www.soinside.com 2019 - 2024. All rights reserved.