1

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?

3
  • It is not required to be static only, it is based upon how you can make available driver instance to all of your selenium code Apr 18, 2017 at 17:17
  • Your implementation clearly works. What do you imagine is the problem with it? There doesn't seem to be any point changing your approach. You could have one driver switch between windows, but that seems like trouble... Apr 18, 2017 at 20:51
  • @Mark Lapierre this approach works when using Webdriver and the PageObjectModel approach but im now required to develop a framework which also uses Cucumber and therefore looking into all possibilities and problems which may arise, for example within my DriverFactory class i call upon BeforeClass etc using TestNG and no this may cause issues when using Cucmber and the JUnit library,
    – Gbru
    Apr 19, 2017 at 8:22

2 Answers 2

2

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.

2
  • thanks again really appreciate you putting the time to go into detail (Im sure you are very busy), your answer is dead on point for what im looking for, I have built a page object model approach / framework (Dosnt use Cucumber) which i have created a driver class and in turn initialised the driver within this class (Non static), in turn all my tests inherit the driverfactory class and this enabled me to run mutiple tests at the same time, my only problem is that this approach uses TestNg and i know Cucumber is more suited to JUnit, thanks again for your help
    – Gbru
    Apr 19, 2017 at 8:28
  • You can use cucumber with testng pretty easily. stackoverflow.com/questions/31285778/… Apr 19, 2017 at 9:21
1

You may already know that static variable is a class variable. If you make WebDriver static, its instance will be shared between parallel threads, and you'll definitely face with race condition issue.

That's where thread safety is important. There're couple of ways how you could achieve it:

  • using ThreadLocal container;
  • using one of specialized structures, which designed for concurrent environment, e.g. ConcurrentHashMap.

Several points on why people make WebDriver static:

  • blind copy/pasting from "authoritative" sources;
  • excluding driver from domain layer;
  • architecture design.

From my experience it's impossible to operate non-static WebDriver using pure inheritance without explicitly exposing it outside framework.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.