Is there any alternative for PageFactory using net core

10,478

Solution 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.

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

Solution 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;

Solution 4

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

Thanks, Karthik KK

Solution 5

DotNetSeleniumExtras.PageObjects.Core is supporting .NETStandard 2.0.

The library contains PageFactory and supporting classes. It's a fork of DotNetSeleniumExtras.

It's Tested in netcore3.1 and Net5 and the package support PageFactory.InitElements(driver, this);

To use:

Install-Package DotNetSeleniumExtras.PageObjects.Core -Version 3.12.0

using OpenQA.Selenium;
using SeleniumExtras.PageObjects;
Share:
10,478

Related videos on Youtube

Marcel
Author by

Marcel

Updated on June 04, 2022

Comments

  • Marcel
    Marcel almost 2 years

    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?