This is a small class that will hold a pool of driver objects. We are using a ThreadLocal object to instantiate our WebDriverThread objects in separate threads. We have also created a getDriver() method that uses the getDriver() method
on the WebDriverThread object to pass each test a WebDriver instance it can use.
We are doing this to isolate each instance of WebDriver to make sure that there is no cross contamination between tests. When our tests start running in parallel, we don't want different tests to start ring commands to the same browser window. Each instance of WebDriver is now safely locked away in its own thread.
Since we are using this factory class to start up all our browser instances, we need to make sure that we close them down, as well. To do this we have created a method with an @AfterMethod annotation that will destroy the driver after our test has run. This also has the added advantage of cleaning up if our test fails to reach the line where it would normally call driver.quit()—for example, if there was an error
in the test that caused it to fail and nish early.
All that is left now is to clean up the code in our basicTest class and change
its name to BasicTestWD. You may have noticed that we added an <includes> con guration item to our POM. This is because Maven will use maven-surefire- plugin to run les that have test at the start or end of their name. We don't want maven-surefire-plugin to pick up our tests; we want to use maven-failsafe- plugin instead.
Please sign in to leave a comment.