picocontainer for singleton DI

自古美人都是妖i 提交于 2019-12-06 22:25:11

The cache of the pico container is reset between scenarios, when the world is disposed of at the end of the scenario.

This is very much per design to avoid state leaking between scenarios and make them isolated so the order of the tests doesn't influence the results.

If you do wish to maintain state between scenario A and scenario B, you either need to handle SharedObject yourself, outside of the pico container, or you can make the dependency between these two scenarios explicit – for example by using a Background.

I hope it's not too late to answer your question.

Actually, you can have what you want. I saw that you also want to have a single instance of your browser. You have to use static objects in order to do that. I created a little class to avoid the Open/close of the driver between each tests. It is managed by picocontainer. In your stepDefinition classes, you have to implement the "same" constructor.

Here is a sample :

public class myStepDefinition{
    private Drivers context;
    public myStepDefinition(Drivers context){
        this.context = context;
        // whatever you want to do
    }
}

public class Drivers {
    private static boolean initialized = false;
    private static WebDriver driver;

    @Before
    public void initialize(){
        if (!initialized){
            initialized = true;
            driver = new FirefoxDriver();
            driver.manage().window().maximize();
            driver.get("http://www.myurl.url");
        }
    }

    public static WebDriver getDriver(){
        return driver;
    }
}

Keep in mind that you have to go back to you login page or your starting page after each test (passed or failed)

Regards,

Nicolas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!