BoDi.ObjectContainerException接口无法解析:OpenQA.Selenium.IWebDriver

时间:2019-05-27 16:47:09

标签: c# selenium selenium-webdriver bdd specflow

由于不推荐使用PageFactory,因此我正在创建一个新框架。 我遇到了错误

BoDi.ObjectContainerException : Interface cannot be resolved: OpenQA.Selenium.IWebDriver (resolution path: UnitTestProject1.Base)
TearDown : BoDi.ObjectContainerException : Interface cannot be resolved: OpenQA.Selenium.IWebDriver (resolution path: UnitTestProject1.Base)

下面是我的框架代码片段。我不确定该如何解决。我知道我可以使用上下文注入,但是不确定应该将框架中的哪些属性移至何处。 我在想应该将IWedriver驱动程序移到一个类并在构造函数中调用它,但不确定在步骤文件中应在何处调用它。

感谢一些帮助解决此问题的帮助。

using System;
using System.Collections.Generic;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using SeleniumExtras.PageObjects;


namespace UnitTestProject1
{
    public class Base : SpecflowBaseTest
    {
        protected IWebDriver driver { get; set; }

    public Base(IWebDriver Driver)
    {
        driver = Driver;
        //PageFactory.InitElements(Driver, this);
    }
   }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TechTalk.SpecFlow;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using UnitTestProject1.Page;
using OpenQA.Selenium.Remote;
using BoDi;

namespace UnitTestProject1
{
[Binding]
public class SpecflowBaseTest : TechTalk.SpecFlow.Steps
{
    // For additional details on SpecFlow hooks see 
http://go.specflow.org/doc-hooks
    protected IWebDriver Driver { get; set; }
    private readonly IObjectContainer objectContainer;

    [BeforeScenario]
    public void BeforeScenario()
    {
        Driver = new ChromeDriver();
        //this.objectContainer = objectContainer;
        //ObjectContainer.RegisterInstanceAs<IWebDriver>(Driver);
        Driver.Manage().Window.Maximize();
    }

    [AfterScenario]
    public void AfterScenario()
    {
        Driver.Close();
        Driver.Quit();
    }

    public void NavigateToURL(string URL)
    {
        Driver.Navigate().GoToUrl(URL);
    }

    protected LoginPage LoginPage => new LoginPage(Driver);
  }
}

using NUnit.Framework;
using System;
using TechTalk.SpecFlow;


namespace UnitTestProject1.Steps
{
[Binding, Parallelizable]

public class LoginSteps : SpecflowBaseTest
{
    [Given(@"I navigate to (.*)")]
    public void GivenINavigateToHttpsCompany_Com(string URL)
    {
        NavigateToURL(URL);
    }

    [Given(@"I enter bw_(.*) and (.*)")]
    public void GivenIEnterBw_Valid_UserAnd(string Username, string 
Password)
    {
        LoginPage.Login(Username, Password);
    }

    [Then(@"I am logged in as bw_valid_user")]
    public void ThenIAmLoggedInAsBw_Valid_User()
    {
        //LoginPage.
    }
  }
}

3 个答案:

答案 0 :(得分:0)

您需要初始化一个新的IWebDriver对象,并在[BeforeStep]中向SpecFlow的依赖项注入框架注册它。

[Binding]
public class SeleniumSpecFlowHooks
{
    private readonly IObjectContainer container;

    public SeleniumSpecFlowHooks(IObjectContainer container)
    {
        this.container = container;
    }

    [BeforeStep]
    public void CreateWebDriver()
    {
        // Create and configure a concrete instance of IWebDriver
        IWebDriver driver = new FirefoxDriver(...)
        {
            ...
        };

        // Make this instance available to all other step definitions
        container.RegisterInstance(driver);
    }

    [AfterStep]
    public void DestroyWebDriver()
    {
        IWebDriver driver = container.Resolve<IWebDriver>();

        driver.Close();
        driver.Dispose();
    }
}

您的步骤定义类不应初始化Web驱动程序。只需在其构造函数中声明一个IWebDriver参数即可。

基类:

[Binding]
public class SpecflowBaseTest : TechTalk.SpecFlow.Steps
{
    protected IWebDriver Driver { get; }
    protected LoginPage LoginPage { get; }

    public SpecflowBaseTest(IWebDriver driver)
    {
        Driver = driver;
        LoginPage = new LoginPage(driver);
    }

    public void NavigateToURL(string URL)
    {
        Driver.Navigate().GoToUrl(URL);
    }
}

儿童班:

[Binding, Parallelizable]
public class LoginSteps : SpecflowBaseTest
{
    [Given(@"I navigate to (.*)")]
    public void GivenINavigateToHttpsCompany_Com(string URL)
    {
        NavigateToURL(URL);
    }

    [Given(@"I enter bw_(.*) and (.*)")]
    public void GivenIEnterBw_Valid_UserAnd(string Username, string Password)
    {
        LoginPage.Login(Username, Password);
    }

    [Then(@"I am logged in as bw_valid_user")]
    public void ThenIAmLoggedInAsBw_Valid_User()
    {
        //LoginPage.
    }
}

答案 1 :(得分:0)

通过创建一个新的类来解决

cd word-count-beam/

mvn compile exec:java \
      -Dexec.mainClass=org.apache.beam.examples.WordCount \
      -Dexec.args="--output=./output/"

答案 2 :(得分:0)

对我来说,此问题已通过添加类级别字段public IWebDriver Driver而非本地方法级别driver得以解决。

[Binding]
public class DriverSetup
{
    private IObjectContainer _objectContainer;
    public IWebDriver Driver;

    public DriverSetup(IObjectContainer objectContainer)
    {
        _objectContainer = objectContainer;

    }

    [BeforeScenario]
    public void BeforeScenario()
    {
        //TODO please supply your Sauce Labs user name in an environment variable
        var sauceUserName = Environment.GetEnvironmentVariable("SAUCE_USERNAME", EnvironmentVariableTarget.User);
        //TODO please supply your own Sauce Labs access Key in an environment variable
        var sauceAccessKey = Environment.GetEnvironmentVariable("SAUCE_ACCESS_KEY", EnvironmentVariableTarget.User);
        var sauceOptions = new Dictionary<string, object>
        {
            ["username"] = sauceUserName,
            ["accessKey"] = sauceAccessKey
        };
        var chromeOptions = new ChromeOptions
        {
            BrowserVersion = "latest",
            PlatformName = "Windows 10"
        };
        chromeOptions.AddAdditionalOption("sauce:options", sauceOptions);

        Driver = new RemoteWebDriver(new Uri("https://ondemand.saucelabs.com/wd/hub"),
            chromeOptions.ToCapabilities(), TimeSpan.FromSeconds(30));
        _objectContainer.RegisterInstanceAs(Driver);
    }
}