2
votes

I am creating a new Selenium framework using C# as PageFactory is deprecated and no longer used. I am using dependency injection, my code builds without errors but when I run my test the chrome browser does not open and I get the error:

Message: BoDi.ObjectContainerException : Interface cannot be resolved: 
OpenQA.Selenium.IWebDriver (resolution path: 
UnitTestProject1.Steps.LoginSteps->UnitTestProject1.Page.LoginPage)

I am not sure what is wrong. Something to do with the Driver I think. Some help appreciated, thanks.

Base Class

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 Hooks

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 Class

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 file

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 File

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
1
Hi, do You resolve your problem? I'm already try to do Test Framework with SPecFlow and Pages objects and have issue: BoDi.ObjectContainerException : Interface cannot be resolved: OpenQA.Selenium.IWebDriver (resolution path: AutomationTestSiiFramework.Tests.TheInternet.Tests.SpecFlowFeature1Steps)Saganowsky

1 Answers

2
votes

You are missing the [Binding] attribute above the class name for SpecFlowHooks. The [Binding] attribute is what SpecFlow looks for when recognizing step definitions and "hooks" files. That means the SpecFlowHooks class methods are not being executed, and therefore the IWebDriver object is not being registered in SpecFlow's dependency injection framework.