Is there any alternative for PageFactory using net core

Marcel picture Marcel · Oct 31, 2017 · Viewed 9k times · Source

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?

Answer

Michele Delle Donne picture Michele Delle Donne · Mar 14, 2019

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