I use selenium webdriver, run test with junit in internet explorer. I create webdriver for ie:
System.setProperty("webdriver.ie.driver", "IEDriverServer.exe");
InternetExplorerDriver driver = new InternetExplorerDriver();
I need continue my test, but using another class. All actions are executed in the same windows as in first test, so I do not need create new ie driver. How can I continue test execution? I try create just new webdriver in second test like
InternetExplorerDriver excep1;
But after running first class test failes with java.lang.NullPointerException.
You can use Singleton and Factory pattern to solve the problem.
1. Create a separate class Browser
2. Create static property getInstance()
3. Call Browser. getInstance() where you need to get the driver instance
getInstance() :
During first call at the first test getInstance() will create a new WebDriver instance and save it into static member “inst”. Static members can be shared across the tests.
Otherwise getInstance() will return old “inst” value. That makes the WebDriver persistent across the tests.
This is C# code, but I have tried to change it to look like Java code:
public class Browser
{
private static IWebDriver inst;
public static IWebDriver getInstance()
{
return (inst != null ) ? inst : new InternetExplorerDriver();