由于不赞成使用PageFactory,并且不再使用它,因此我正在使用C#创建一个新的Selenium框架。 我正在使用依赖项注入,我的代码生成没有错误,但是当我运行测试时,chrome浏览器无法打开,并且出现错误:
Message: BoDi.ObjectContainerException : Interface cannot be resolved:
OpenQA.Selenium.IWebDriver (resolution path:
UnitTestProject1.Steps.LoginSteps->UnitTestProject1.Page.LoginPage)
我不确定是什么问题。我认为与驱动程序有关。感谢一些帮助,谢谢。
基类
namespace UnitTestProject1
{
[Binding]
public class SpecflowBaseTest : TechTalk.SpecFlow.Steps
{
protected IWebDriver Driver { get; }
//protected LoginPage LoginPage { get; }
/*public SpecflowBaseTest()
{
}*/
public SpecflowBaseTest(IWebDriver driver)
{
Driver = driver;
}
public void NavigateToURL(string URL)
{
Driver.Navigate().GoToUrl(URL);
}
}
}
Specflow挂钩
namespace UnitTestProject1
{
public class SpecFlowHooks
{
private readonly IObjectContainer container;
public SpecFlowHooks(IObjectContainer container)
{
this.container = container;
}
[BeforeStep]
public void CreateWebDriver()
{
// Create and configure a concrete instance of IWebDriver
IWebDriver driver = new ChromeDriver();
{
};
// Make this instance available to all other step definitions
container.RegisterInstanceAs(driver);
}
[AfterStep]
public void DestroyWebDriver()
{
IWebDriver driver = container.Resolve<IWebDriver>();
driver.Close();
driver.Dispose();
}
}
}
LoginPage类
namespace UnitTestProject1.Page
{
public class LoginPage : SpecflowBaseTest
{
public LoginPage(IWebDriver browser) : base(browser)
{
//Driver = browser;
}
public void Login(string Username, string Password)
{
UsernameField.SendKeys(Username);
PasswordField.SendKeys(Password);
LoginButton.Click();
}
public void CheckUserIsLoggedIn(string Username)
{
AccountUsername.Displayed.ShouldBe(true, Username);
}
[FindsBy(How = How.CssSelector, Using = "#LoginUsername")]
private IWebElement UsernameField { get; set; }
[FindsBy(How = How.CssSelector, Using = "#LoginPassword")]
private IWebElement PasswordField { get; set; }
[FindsBy(How = How.CssSelector, Using = "#login-submit-button")]
private IWebElement LoginButton { get; set; }
[FindsBy(How = How.CssSelector, Using = "#div.accountUsrename")]
private IWebElement AccountUsername { get; set; }
}
}
LoginSteps文件
namespace UnitTestProject1.Steps
{
[Binding, Parallelizable]
public class LoginSteps : SpecflowBaseTest
{
//protected LoginPage LoginPage;
//protected IWebDriver Driver;
private LoginPage LoginPage { get; }
public LoginSteps(LoginPage loginpage, IWebDriver driver) : base(driver)
{
//Driver = driver;
this.LoginPage = new LoginPage(driver);
}
[Given(@"I navigate to (.*)")]
public void GivenINavigateToHttpstesta_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 valid_user")]
public void ThenIAmLoggedInAsBw_Valid_User()
{
//LoginPage.
}
}
}
功能文件
Feature: Login
In order to log into my account
As a User
I want to Log in and view my account
@Login
Scenario: Check user can log in account
Given I navigate to https://testa.com
And I enter valid_user and abc123
Then I am logged in as valid_user
答案 0 :(得分:1)
您缺少[Binding]
类名上方的SpecFlowHooks
属性。 [Binding]
属性是SpecFlow在识别步骤定义和“挂钩”文件时要查找的内容。这意味着未执行SpecFlowHooks类方法,因此未在SpecFlow的依赖项注入框架中注册IWebDriver对象。