C# .NET Core PageFactory

1,147 views
Skip to first unread message

Amanda Perkins

unread,
Mar 28, 2018, 2:40:53 PM3/28/18
to Selenium Users
So, I read that PageFactory was going away (bummer).  How do I go about making PageFactory.InitElements(driver, this) work?  (PS I'm new to C# and .NET core - typically I've written selenium in Java, so please be kind)

using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Support.Extensions;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Support.PageObjects;




namespace APSeleniumTest.Pageclasses
{
    public sealed class PageFactory {
        class LoginPageFactory
        {
            IWebDriver driver;

            [FindsBy(How = How.Id, Using = "register")]
            IWebElement SignInButton;

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

    }
}


Jim Evans

unread,
Mar 28, 2018, 5:46:13 PM3/28/18
to Selenium Users
The .NET PageFactory implementation does not work with .NET Core. The mechanism that allows PageFactory to work is unavailable in .NET Core, and unlikely ever to be available. Indeed, that’s one motivation for removing the implementation from the Selenium project’s repository. If someone wants to take over maintenance of the .NET PageFactory implementation, they are welcome to contact me to discuss assuming ownership of the new repository the code has been migrated to.

Unlike in Java, there is virtually zero benefit to using the PageFactory to construct Page Objects. There is no savings in code verbosity, and the mechanism of execution is far more complex than it ought to be simply for the dubious benefit of marking element identifiers by attribute instead of actual executable code.

Finally, the Java PageFactory, flawed though it might be, isn’t going anywhere. It will continue to be available as part of the Selenium package going forward.

shiv

unread,
Mar 30, 2018, 12:41:20 AM3/30/18
to Selenium Users

Hi Amanda Perkins,
You can import DotNetSeleniumExtra nuget package it has DLL files for PageObject and ExpectedConditions.  From  Selenium Webdriver 3.11.0 These two classes are going to be obsolete hence these two classes are part of different DLL's now i.e. DotNetSeleniumExtra . You just have to import these DLLs and give the reference of these DLL in existing test scripts , it should work fine .

All the best

Thanks !

Sebastian Felice

unread,
Mar 31, 2018, 7:48:22 AM3/31/18
to Selenium Users


I use this in C#, it's less code too!

IWebElement GoogleLogoMain => driver.FindElement(By.Id("hplogo"));

Sebastian Felice

unread,
Mar 31, 2018, 8:28:37 AM3/31/18
to Selenium Users
I just found this. 
I'm going to try it out.
Not sure what the driver1 part is? 

IWebDriver driver = new FirefoxDriver();
            
IWait<IWebDriver> wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
IWebElement element = wait.Until(driver1 => driver.FindElement(By.Name("whatever"))); 
Reply all
Reply to author
Forward
0 new messages