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

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

我正在创建一个框架来测试使用 Specflow 和 Nunit 的 Web 应用程序,但我 运行 遇到了以前没有的错误。

我得到的错误是:BoDi.ObjectContainerException:'Interface cannot be resolved: OpenQA.Selenium.IWebDriver'

当我尝试将 _container 解析为 IWebDriver 时,这起源于基础 class。

我没有得到编译错误,只有在测试 运行 时才会出现错误。

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

您遇到了订单问题。 SpecFlow 创建 LoginSteps class 的实例来调用 BeforeScenarioHook 方法 Startup。但是当它这样做时,它会在您要创建 LoginPage 实例的地方执行构造函数,该实例要解析 WebDriver。

但是此时webdriver还没有在容器中注册,无法解析。

我会把行 loginPage = new LoginPage(container); 移到钩子中来解决这个问题。