Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

selenium - NUnit Specflow how to share a class instance for all tests

I am using Specflow with NUnit and Selenium and want to share instance of driver across all tests. I can do do this up to feature level with FeatureContext but can't see anything for all tests. I am aware that this is probably not the right way to go but I want to know if there is a way.

Please help with example(s).

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are a few ways to do this. Most are covered on this page

What I personally would probably do is define a SeleniumContext class and require this class in all my Step class constructors, then tell SpecFlow's IOC to use the same instance in every scenario:

First create the class to hold the selenium driver instance

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

then setup the IOC to return the same instance every time

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

Then ensure your step classes always ask for the context in their constructors (you need to do this in every step class you have)

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

alternatively if you are already storing the instance in the feature context then you can just use the BeforeFeature hook to save the same instance:

[Binding]
public class BeforeAllTests
{
    private static WebDriver webDriver;

    [BeforeTestRun]
    public static void RunBeforeAllTests()
    {
        webDriver = new WebDriver();
     }

    [BeforeFeature]
    public static void RunBeforeFeature()
    {
        FeatureContext["WebDriver"] = webDriver;
     }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...