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

I am creating a test automation environment 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 see 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. Online tutorials seem to link to PageFactory just like I don't have excess import, is there something I am 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); } } } 
+7
source share
5 answers

In the end, I just created a new project and ported everything with one difference, the type of project I created was Visual C #> Test> Unit Test Project, before the project I created was Visual C #> .Net Core> Class library project (I followed the tutorial).

I'm not sure if this changed anything as such or simply meant that I had a clear-cut project when I downloaded and installed NuGet packages, however now I can access the PageFactory class and its associated methods. Thanks to all who responded.

+2
source

In case someone else comes across this question, the reason why you cannot find PageFactory these days is quite simple: it does not exist .

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

The solution to this problem is to simply add DotNetSeleniumExtras to your packages, as they have been moved to a separate repository . You can also find the useful fork Dreamescaper ( NuGet ), which adds support for .NET Core, until the original repository finds the owner.

+3
source

There was the same problem, tried to uninstall and reinstall, but it did not work. Then I started looking for NuGet packages named Selenium and found that the Selenium.Support package was separate from the main Selenium.Webdriver package. So if you have this problem, go back and make sure that you have installed both the Selenium.Webdriver and Selenium.Support packages through Nuget.

+1
source

As you already mentioned, when you try to link to PageFactory.initElements(driver, PageName) , you see the error message as follows:

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

If you look at the documentation for the PageFactory Class API, it clearly states the following:

  • System.Object: OpenQA.Selenium.Support.PageObjects.PageFactory
  • Namespace: OpenQA.Selenium.Support.PageObjects

Thus, the error basically means that there are some problems installing the Selenium.Support.PageObjects package.

Decision

The correct solution would be to completely remove the Selenium.WebDriver and Selenium.Support.PageObjects packages, and then reinstall them back through NuGet Manager.

Note. Here you can find a similar discussion.


Update

Since you still see the same error, you can make a small change to your code as follows:

Replace:

 HomePage homePage = new HomePage(driver); PageFactory.initElements(driver, homePage); 

Via:

 HomePage homePage = PageFactory.initElements(driver, HomePage); 
0
source

If someone is facing the same problem, please install the following from the NuGet package manager: DotNetSeleniumExtras.PageObjects.Core(3.12.0)

0
source

Source: https://habr.com/ru/post/1275297/


All Articles