Gbru Gbru -3 years ago 215
Java Question

Should WebDriver setup be Static for Concurrent Test Execution?

Should WebDriver setup be Static for Concurrent Test Execution?

  1. Online im seeing many examples of BasePage setups etc where developer are using a static instance of driver(s) using lists etc.

  2. In my case the following work and I can execute more than one test case at the same time:

    public class BrowserFactory implements ISuiteListener {
    protected WebDriver webdriver;


Your thoughts?

Answer Source

It depends entirely on the scope of your testing. If you are using a Gherkin-based system like Cucumber or JBehave, your tests are generally steps withing scenarios within stories. When you have multiple scenarios and/or multiple stories, it is often easier to setup the driver once (static) and pass that instance along to other scenarios/stories. If, on the other hand, you want a fresh, new instance of the driver each time, you should not define your driver as static, but this means that each scenario would require the steps necessary to instantiate the driver, as well as typically log the user in, etc. and close the driver at the end. This does allow for a more obviously self-contained scenario, but the same thing can be accomplished with static drivers if you check to see if the driver has been instantiated (or the user logged-in) before continuing and react accordingly instead of always assuming it's a fresh start.

If you have a test that is completely self-contained, then stick with a dynamic (non-static) declaration of the driver, since it will need to be initialized and closed if it's all that will be run with that driver. If your suite contains other tests, though, and they could benefit from using the same driver, then design it that way. In JBehave I initialize my driver in the test runner, so it will work whether I run one test, or one story, or multiple stories, in the quickest and most efficient ways.

I hope I've explained this clearly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download