5

I am creating automated tests with selenium and specflow using C#. Today I had to update Selenium.WebDriver to version 4.0.0-beta4 because of logging functionality, which has a bug in previous versions with AvailableLogTypes property, which was always throwing null reference exceptions. After updating to selenium 4, another problem raised up. Build works fine without errors or warnings but when I run tests, following exception is thrown:

Message: 
    System.TypeLoadException : Could not load type 'OpenQA.Selenium.Internal.IWrapsElement' from assembly 'WebDriver, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null'.

Stack Trace: 
    DefaultPageObjectMemberDecorator.CreateProxyObject(Type memberType, IElementLocator locator, IEnumerable`1 bys, Boolean cache)
    DefaultPageObjectMemberDecorator.Decorate(MemberInfo member, IElementLocator locator)
    PageFactory.InitElements(Object page, IElementLocator locator, IPageObjectMemberDecorator decorator)
    PageFactory.InitElements(Object page, IElementLocator locator)
    PageFactory.InitElements(ISearchContext driver, Object page)
    LoginPageElements.ctor(IWebDriver driver) line 12
    PortalSharedPageSteps.CheckUIElementsOfLoginScreen() line 32
    LoginPageTestSteps.CheckUIElementsOfLoginScreenStep() line 21
    BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration)
    TestExecutionEngine.ExecuteStepMatch(BindingMatch match, Object[] arguments, TimeSpan& duration)
    TestExecutionEngine.ExecuteStep(IContextManager contextManager, StepInstance stepInstance)
    TestExecutionEngine.OnAfterLastStep()
    TestRunner.CollectScenarioErrors()
    LoginPageTestFeature.ScenarioCleanup()
    LoginPageTestFeature.LoginPageTest() line 9

From official release notes of selenium:

Per v4.0.0a1 in https://github.com/SeleniumHQ/selenium/blob/master/dotnet/CHANGELOG "Moved IWrapsDriver and IWrapsElement from the OpenQA.Selenium.Internal namespace to the OpenQA.Selenium namespace. This should be a no-op for the vast majority of users, requiring only a recompile of code, given that both of these interfaces feature return types in the base namespace, meaning that users likely already have "using" statements for the root namespace in their source. If errors are encountered, changing the namespace in the code and rebuilding should resolve the error."

I double checked for all occurrences of OpenQA.Selenium.Internal namespace and found out, that this namespace is not even used in my code. So I supposed that everything will work fine, but no ...

Later on I found out that everything works if I remove DotNetSeleniumExtras.PageObjects nuget package. That's nice, but I need this package to be able to use PageFactory.InitElements() for page objects initialization and FindsBy attribute to bind UI elements. I inspected DotNetSeleniumExtras.PageObjects package and it also uses selenium 4.

Do you have any ideas how to solve this problem ? Let me know if you need to see source code (it is quite extensive so, I did not want to bloat this post)

SamJ26
  • 154
  • 2
  • 10
  • 1
    The dotnetextras https://github.com/DotNetSeleniumTools/DotNetSeleniumExtras hasn't been updated for 4 years - the readme says there is no owner so it's not looking likely it will be available for selenium 4. In c# you don't need to use the page factory at all - when they removed it they said it was because it was the wrong way to make it work in .net. You just use a lambda operator to specify your objects in the pom: `private By yourByIdendifier => By.XPath("//button[text()='Hello World']");` – RichEdwards Aug 10 '21 at 12:42

4 Answers4

3

So here is how I fixed this:

  1. I removed DotNetSeleniumExtras packages from code.
    After that, I was finally able to gather performance logs from browser because I got rid of errors shown in my question.
  2. After removal of DotNetSeleniumExtras package I lost PageFactory for initialization of page objects.
    I fixed this problem by converting all web elements (properties of page object) into computed properties:
public class PageObject
{
    private readonly IWebDriver driver;         

    public IWebElement PageLink => driver.FindElement(By.XPath(@"my custom XPath"));
    public IWebElement TitleMain => driver.FindElement(By.Id("some Id"));

    // etc

    public PageObject(IWebDriver webDriver)
    {
         driver = webDrvier;
    }
}

Code below shows an example how class above would be used:

var pageObj = new PageObject(driver);
Console.Writeline(pageObj.PageLink.Enabled);

PS: thanks to RichEdwards for giving me a hint how to solve this problem

SamJ26
  • 154
  • 2
  • 10
3

Starting with Selenium.WebDriver 4.0, one needs to install both Selenium.WebDriver and Selenium.Support nuget packages (same version) to avoid this issue.

Cosmin Sontu
  • 1,034
  • 11
  • 16
2

I had the same issue when upgrade Selenium WebDriver 3 to WebDriver 4 for my C# project test. and the issue was gone by simply removed DotNetSeleniumExtras packages from code. then Install DotNetSeleniumExtras.core (4.1) packages

Jason Dai
  • 21
  • 1
  • Do you mean DotNetSeleniumExtras.PageObjects.Core package by what you installed after removing DotNetSeleniumExtras packages? How about DotNetSeleniumExtras.PageObjects.Core.Linq? Did you install this, too? – Brian Hong Mar 22 '22 at 21:13
  • Same as here. The error I was getting was different, but the solution was the same. The problem had occurred after I downloaded Selenium.Support NuGet package, which in turn upgraded Selenium.WebDriver NuGet from v3.141 to 4.8. After I got my error, I uninstalled DotNetSeleniumExtras.WaitHelpers, check-in the changes and downloaded DotNetSeleniumExtras.WaitHelpers again. – Cagin Uludamar Feb 15 '23 at 09:02
2

I had the same issues that lead to this thread.

Manage to solve it removing the DotNetSeleniumExtras Nuget, and replacing it by the DotNetSeleniumExtras.PageObjects.Core.

With this solution I didn't lose the access to PageFactory.InitElements()

The web test scripts started to run smoothly.

Nugets used in my project:

  • DotNetSeleniumExtras.PageObjects.Core (4.3.0)
  • Selenium.Support (4.3.0)
  • Selenium.WebDriver (4.3.0)
tripleee
  • 175,061
  • 34
  • 275
  • 318
Ricardo
  • 21
  • 3