Search code examples
c#seleniumpage-factory

The name 'PageFactory' does not exist in the current context


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

    Solution

  • I eventually just created a new project, and ported everything over with one difference, the type of project I created was a Visual C# > Test > Unit Test Project, before the project I had created was Visual C# > .Net Core > Class Library project (I was following a tutorial).

    I'm not too sure if this changed anything as such or just meant I had a clear project when I re downloaded and installed the NuGet packages, however I can now access the PageFactory class and associated methods. Thanks to everyone who replied.