I would do it on this way:
Create a class with methods that you use to create new WebDriver
instances as easy as possible. E.g.:
WebDriver userOne = DriverBuilder.getUserOne();
WebDriver userTwo = DriverBuilder.getUserTwo();
With that you have two WebDriver
instances and you can control each on them separate. You do not have to use different browsers. Both instances can be the Firefox browser.
If you need to work with WebDriver
instances across several unit tests, create a class that act as a pool/collection. You put initialized WebDriver
s into it and get them, when you need. E.g.:
// Unit Test 1
DriverPool.put(initializedDriver, "uniqueName");
// Unit Test 2
WebDriver chattingUser = DriverPool.get("uniqueName");
But you have to be careful. If you put a lot of instances into the pool, there are a lot of browsers open and maybe your grid system is not able to handle them all. So you have to be sure, that you delete instances that you do not need anymore. Maybe this is the most challenging part.
In addition: Try to keep everything that have to do with the initialization and handling of the WebDriver
s outside your unit tests. Put this into @Before
methods or somewhere else. It is important to not mess unit tests up. They have to be easy to read and concentrate on testing itself.
This is just a theoretical solution. I do not know if this will solve your problem, but my mind says it should. The same apply the code examples.