本文介绍了错误CS7036没有给出与"SpecflowBaseTest.SpecflowBaseTest(IWebDriver)"的所需形式参数"driver"相对应的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating a new framework as PageFactory has been deprecated. I am getting the error

Error CS7036 There is no argument given that corresponds to the required 
formal parameter 'driver' of 
'SpecflowBaseTest.SpecflowBaseTest(IWebDriver)' UnitTestProject1 
D:\Selenium 
Projects\Olympus\UnitTestProject1\UnitTestProject1\Steps\LoginSteps.cs 14 
Active

下面是我的框架的完整代码段.我正在尝试进行依赖项注入,因为PageFactory已弃用,不再使用.

My full code snippet of my framework is below. I am trying the dependency injection because PageFactory is deprecated, not used anymore.

我不确定如何解决此错误.一些帮助表示赞赏.谢谢

I am not sure how to resolve this error. Some help appreciated. Thanks

BaseClass

BaseClass

using System;
using System.Collections.Generic;
using System.Linq;    
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
{
    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);
    }
}
} 

SpecflowHooksClass

SpecflowHooksClass

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

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类

LoginPage Class

using System;
using System.Collections.Generic;
using System.Text;
using OpenQA.Selenium;
using SeleniumExtras.PageObjects;
using NUnit.Compatibility;
using Shouldly;
using OpenQA.Selenium.Remote;

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

}
}

步骤文件

using NUnit.Framework;
using System;
using TechTalk.SpecFlow;
using UnitTestProject1.Page;
using OpenQA.Selenium;

namespace UnitTestProject1.Steps
{
[Binding, Parallelizable]
public class LoginSteps : SpecflowBaseTest
{
    protected LoginPage LoginPage;

    public LoginSteps(LoginPage loginpage, IWebDriver driver)
    {
        //Driver = driver;
        this.LoginPage = loginpage;
    }

    [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.
    }
}
}

推荐答案

您实际上应该在这里做三件事.

There are three things you really should do here.

  1. 从SpecflowBaseTest中删除LoginPage属性
  2. 从LoginSteps调用基类构造函数
  3. LoginPage属性和初始化代码移至LoginSteps:
  1. Remove LoginPage property from SpecflowBaseTest
  2. Call the base class constructor from LoginSteps
  3. Move the LoginPage property and initialization code into LoginSteps:
[Binding]
public class LoginSteps : SpecflowBaseTest
{
    private LoginPage LoginPage { get; }

    public LoginSteps(IWebDriver driver) : base(driver)
    {
        LoginPage = new LoginPage(driver);
    }
}

这篇关于错误CS7036没有给出与"SpecflowBaseTest.SpecflowBaseTest(IWebDriver)"的所需形式参数"driver"相对应的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 20:16