栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

NUnit Specflow如何为所有测试共享一个类实例

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

NUnit Specflow如何为所有测试共享一个类实例

有几种方法可以做到这一点。此页面涵盖了大多数

我个人可能要做的是定义一个

SeleniumContext
类,并在所有Step类的构造函数中都需要该类,然后告诉SpecFlow的IOC在每种情况下都使用相同的实例:

首先创建用于保存硒驱动程序实例的类

public class SeleniumContext{     public SeleniumContext()     {          //create the selenium context          WebDriver = new ...create the flavour of web driver you want     }     public IWebDriver WebDriver{get; private set;} }

然后将IOC设置为每次返回相同的实例

[Binding]public class BeforeAllTests{    private readonly IObjectContainer objectContainer;    private static SeleniumContext seleniumContext ;    public BeforeAllTests(IObjectContainer container)    {        this.objectContainer = container;    }    [BeforeTestRun]    public static void RunBeforeAllTests()    {        seleniumContext = new SeleniumContext();     }    [BeforeScenario]    public void RunBeforeScenario()    {         objectContainer.RegisterInstanceAs<SeleniumContext>(seleniumContext );    }}

然后确保您的步骤类始终在其构造函数中要求上下文(您需要在每个步骤类中都执行此操作)

[Bindings]public class MySteps{    private SeleniumContext seleniumContext;    public MyClass(SeleniumContext seleniumContext)    {         //save the context so you can use it in your tests         this.seleniumContext = seleniumContext;    }    //then just use the seleniumContext.WebDriver in your tests}

或者,如果您已经将实例存储在功能上下文中,则可以使用

BeforeFeature
挂钩保存相同的实例:

[Binding]public class BeforeAllTests{    private static WebDriver webDriver;    [BeforeTestRun]    public static void RunBeforeAllTests()    {        webDriver = new WebDriver();     }    [BeforeFeature]    public static void RunBeforeFeature()    {        FeatureContext["WebDriver"] = webDriver;     }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/470961.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号