C# – The name ‘PageFactory’ does not exist in the current context

cpage-factoryselenium

I'm creating a test automation framework in c#. At the top of the project I have "using OpenQA.Selenium.Support.PageObjects;", however when I try to reference PageFactory.initElements(driver, PageName) I'm seeing the message "The name 'PageFactory' does not exist in the current context".

I thought the PageFactory class was included as part of the Selenium.Support nu get package. Tutorials online seem to reference the PageFactory in the same way I am with no extra imports, is there anything I'm missing?

NuGet packages:

  • Using Selenium.WebDriver version 3.9.1
  • Using Selenium.Support version 3.9.1
  • Using NUnit 3.9.0
  • Using NUnit3TestAdapter 3.9.0
  • Microsoft.NET.Test.Sdk 15.5.0
  • Code below:

    using NUnit.Framework;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Support.PageObjects;
    using System;
    using System.IO;
    
    namespace Framework.TestCases
    {
        class TestCase
        {
            [Test]
            public void Test()
            {
                String pathToDrivers = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "\\Drivers";
                IWebDriver driver = new ChromeDriver(pathToDrivers);
                String searchTerm = "Search Term";
    
                driver.Url = "https://website.com/";
                driver.Manage().Window.Maximize();
    
                HomePage homePage = new HomePage(driver);
                PageFactory.initElements(driver, homePage);
            }
        }
    }
    

    Best Answer

    In case anyone else comes across this question, reason why you can't find the PageFactory nowadays is pretty simple: It does not exist.

    Namely, with 3.11.0 release of Selenium.Support, PageFactory and ExpectedConditions were marked as obsolete. With Selenium.Support 3.12.0 they have been removed completely. More on that topic here.

    Solution to this is to simply add DotNetSeleniumExtras to your packages as those were moved to separate repository. One can also find useful Dreamescaper's fork (NuGet) that has added .NET Core support until the original repo finds an owner.