Is there any alternative for PageFactory using net core

微笑、不失礼 提交于 2020-06-16 04:17:27

问题


I'm using Selenium v3.6.0 and .NET Core 2.0, the following code gives me an error on PageFactory.InitElements saying it doesn't exist in the currenct context.

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
        }
    }
}

I'm a bit confused about this since the attributes FindsBy, FindsByAll and FindsBySequence are all available in the OpenQa.Selenium.Support.PageObjects namespace, but PageFactory is not. To my knowledge, these attributes only work with PageFactory.

Is there a different approach to this using .NET Core or is it just not (yet) implemented?


回答1:


There is no PageFactory class in WebDriver.Support.dll for version 3.6.0 (In visual studio you can open this dll in object explorer and see that there is no such class). So you've got just usual compilation error.

I've looked source code on github https://github.com/SeleniumHQ/selenium/blob/master/dotnet/src/support/PageObjects/PageFactory.cs and see preprocessor directive #if !NETSTANDARD2_0 ... #endif in PageFactory class. I don't know why NETSTANDARD2_0 affected NETCORE2_0, and not sure that it's real reason, but for us as library users PageFactory is unaccessible for now.




回答2:


I'm using WebDriver V3.141.0.0 and .NET v4.5 and you can replace your code as follows:

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
        }
    }
}



回答3:


Not specific to .Net Core but for .Net Standard 2.0 and .Net Framework(s) 3.5, 4.0, & 4.5 you should be able to add DotNetSeleniumExtras.PageObjects to your project to get PageFactory functionality once OpenQA.Selenium officially drops it. You would reference this by: using SeleniumExtras.PageObjects;




回答4:


Yes its not supported as mentioned in this video https://www.youtube.com/watch?v=xd5TCWmaxGI

Thanks, Karthik KK



来源:https://stackoverflow.com/questions/47035004/is-there-any-alternative-for-pagefactory-using-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!