how run new test in the same window there another test was completed?


(khris) #1

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.


(Дмитрий Жарий) #2

 

Hi khris,

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();

 

    }

}


Как запустить тесты в одном браузере?
(Mykhailo Poliarush) #3

and here you have code on java  protected WebDriver getWebDriver() {

  if (driver == null) {
   driver = new InternetExplorerDriver();
  }
  return driver;
}


(khris) #4

This is right code for me:

public class Browser{
    
        
        private static InternetExplorerDriver driver;
 
        private void InternetExplorerDriver(){
        }
 
        public static InternetExplorerDriver getInstance(){
                if (driver == null){
                    driver = new InternetExplorerDriver();                }
                return driver;
        }
        
}

In each place where I need driver I put Browser.getInstance()

 

Thank all for advice.


Как указать webdriver'у, что бы он подхватывал уже открытый браузер?