NC17
NC17

Reputation: 37

BoDi.ObjectContainerException: 'Interface cannot be resolved: OpenQA.Selenium.IWebDriver'

I am creating a framework to test a web app using Specflow and Nunit and I have run into an error that I previously did not have.

The error I get is: BoDi.ObjectContainerException: 'Interface cannot be resolved: OpenQA.Selenium.IWebDriver'

This originates in the base class when I try to resolve the _container into a IWebDriver.

I get no compile errors with this and the bug only shows up when the test is run.

BasePage.cs

using System;
using BoDi;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using ExpectedConditions = SeleniumExtras.WaitHelpers.ExpectedConditions;

namespace TriageManagerAutomation.BaseClasses
{
    public class BasePage
    {
        public readonly IObjectContainer _container;
        public static IWebDriver Driver { get; set; }
        private readonly int timeOut = 20;
        public BasePage(IObjectContainer container)
        {
            _container = container;
            Driver = _container.Resolve<IWebDriver>();
        }
    }
}

LoginSteps.cs

using TechTalk.SpecFlow;
using NUnit.Framework;
using BoDi;
using TriageManagerAutomation.BaseClasses;
using TriageManagerAutomation.Login.PageObjects;

namespace TriageManagerAutomation.Login.TestSteps
{
    [Binding]
    public class LoginSteps : StepBase
    {
        private readonly IObjectContainer _container;
        private readonly LoginPage loginPage;
        public LoginSteps(IObjectContainer container) : base(container)
        {
            _container = container;
            loginPage = new LoginPage(container);
        }

        [Given(@"I have logged in to Triage Manager using (.*) and (.*) as the login      credentials")]
        public void GivenIHaveLoggedInToTriageManager(string username, string password)
        {
            //Open the login page using chrome driver and submit login details
            loginPage.Open();
            Assert.IsTrue(loginPage.IsVisible, "Page Not Loaded");
            loginPage.EnterDetailsAndSubmit(username, password);
        }

        [Then(@"I am successfully logged in")]
        public void ThenIAmSuccessfullyLoggedIn()
        {
            loginPage.CheckLoggedIn();
        }

        [Given(@"I Logout")]
        public void GivenILogout()
        {
            loginPage.Logout();
        }

        [BeforeScenario]
        public void Startup()
        {
            RegisterChromeDriver();

            _container.RegisterInstanceAs(_driver);
        }

        [AfterScenario]
        public void Destroy()
        {
            Destory();
        }
    }
}

LoginPage.cs

using NUnit.Framework;
using BoDi;
using System.Threading;
using TriageManagerAutomation.BaseClasses;
using TriageManagerAutomation.Login.Locators;
using TriageManagerAutomation.Main.Locators;

namespace TriageManagerAutomation.Login.PageObjects
{
    public class LoginPage : BasePage
    {
        private readonly LoginLocators loginLocators;
        private readonly NavLocators navLocators;

        public LoginPage(IObjectContainer container) : base(container) 
        {
            loginLocators = new LoginLocators(container);
            navLocators = new NavLocators(container);
        }

        public bool IsVisible
        {
            get
            {
                return Driver.Url.Contains("/icp-tm/autotest/login");
            }
            internal set { }
        }  
    
        public void CheckLoggedIn()
        {
            Assert.IsTrue(IsElementVisible(navLocators.mainMenu), "Page failed to load");
        }
    
        internal void Open()
        {
            Driver.Manage().Window.Maximize();
            Driver.Navigate().GoToUrl("https://tm26.kanayo.works/icp-tm/autotest/login");
        }
    
        internal void EnterDetailsAndSubmit(string username, string password)
        {
            loginLocators.Username.SendKeys(username);
            loginLocators.Password.SendKeys(password);
            loginLocators.LoginButton.Click();
        }
    
        public void Logout()
        {
            Driver.SwitchTo().DefaultContent();
            loginLocators.LogoutButton.Click();
            Thread.Sleep(1000);
            Driver.SwitchTo().Alert().Accept();
        }
    }
}

Upvotes: 1

Views: 1600

Answers (1)

Andreas Willich
Andreas Willich

Reputation: 5835

You have a order problem. SpecFlow creates an instance of the LoginSteps class to call the BeforeScenarioHook method Startup. But when it does that, it executed the constructor where you want to create an instance of the LoginPage, which want to resolve the WebDriver.

But at this point in time, the webdriver is not yet registered in the container and can't be resolved.

I would move the line loginPage = new LoginPage(container); into the hook to solve this problem.

Upvotes: 2

Related Questions