First of all, we need to create our WebDriverThread class: package com.masteringselenium;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.remote.DesiredCapabilities;
public class WebDriverThread { private WebDriver webdriver;
private final String operatingSystem = System.getProperty("os.name").toUpperCase(); private final String systemArchitecture = System.getProperty("os.arch");
public WebDriver getDriver() throws Exception { if (null == webdriver) {
System.out.println(" "); System.out.println("Current Operating System: " + operatingSystem); System.out.println("Current Architecture: " + systemArchitecture); System.out.println("Current Browser Selection: Firefox"); System.out.println(" "); webdriver = new FirefoxDriver(DesiredCapabilities.firefox());
} return webdriver;
[ 12 ]
}
public void quitDriver() { if (null != webdriver) {
webdriver.quit();
webdriver = null; }
} }
This class holds a reference to a WebDriver object, and ensures that every time you call getDriver() you get a valid instance of WebDriver back. If one has been started up, you will get the existing one. If one hasn't been started up, it will start one for you.
It also provides a quitDriver() method that will perform a quit() on your WebDriver object. It also nulli es the WebDriver object held in the class. This prevents errors that would be caused by attempting to interact with a WebDriver object that has been closed.
Comments
0 comments
Please sign in to leave a comment.