问题描述
由于不推荐使用PageFactory,因此我正在创建一个新框架.我遇到了错误
I am creating a new framework as PageFactory has been deprecated.I am getting the error
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驱动程序移到一个类上并在构造函数中调用它,但不确定在步骤文件中应在何处调用它.
My code snippet of my framework is below. I am not sure how I can resolve this. I am aware I could use Context Injection but am not sure what attributes from my framework I should move and to where.I was thinking should I move the IWedriver Driver to a class and call this in a constructor but not sure where I should call it in the steps file.
感谢一些帮助解决此问题的帮助.
Some help to resolve this issue appreciated, thanks.
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.
}
}
}
推荐答案
您需要初始化一个新的 IWebDriver
对象,并将其在 [BeforeScenario] 中的SpecFlow依赖项注入框架中注册.代码>.
You need to initialize a new IWebDriver
object and register it with SpecFlow's dependency injection framework in a [BeforeScenario]
.
[Binding]
public class SeleniumSpecFlowHooks
{
private readonly IObjectContainer container;
public SeleniumSpecFlowHooks(IObjectContainer container)
{
this.container = container;
}
[BeforeScenario]
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);
}
[AfterScenario]
public void DestroyWebDriver()
{
IWebDriver driver = container.Resolve<IWebDriver>();
driver.Close();
driver.Dispose();
}
}
您的步骤定义类不应初始化Web驱动程序.只需在其构造函数中声明 IWebDriver
参数即可.
Your step definition classes should not be initializing the web driver. Just declare an IWebDriver
argument in their constructors.
基类:
[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.
}
}
这篇关于BoDi.ObjectContainerException接口无法解析:OpenQA.Selenium.IWebDriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!