Reputation: 13
I declare one Singleton with a driver instance in @BeforeTest annotation and I use it to execute all the tests in @Test annotation with 2 browsers parallely.
My question is when I try to close or quit the driver in @AfterTest, it is thrown an error saying "The FirefoxDriver cannot be used after quit() was called"? Is it possible to resolve it?
Upvotes: 0
Views: 52
Reputation: 1088
A code example is needed, but it sounds like you have a threading problem.
If what you are trying to do is abstract WebDriver configuration from your test classes into one central place, I'm not sure a singleton is the best solution.
If you take a look at how TestNG threads it's test execution, you can very easily end up with unexpected WebDriver behavior if the WebDriver instance lives on a class separate from the test. In your case, this is a singleton. In my case, when I first encountered this issue, it was a parent class.
The solution I put into place was to use a parent class for all configuration and setup/teardown of the WebDriver, then extend this class with all my test classes. In order to make this work properly, I had to put my WebDriver on a ThreadLocal<>
. By doing this, I was able to ensure each thread (test) had it's own WebDriver instance to use.
ThreadLocal
s aren't terribly difficult to work with, but more info can be found here.
There are many other ways to solve this problem, and many of us have found our own way... this is simply my approach.
Upvotes: 1