2016-02-04 233 views

回答

0

您可以通過將類構造函數定義爲Private來定義單例類。請對下面的代碼一看,

public class InstanPage { 

    private static InstanPage instance=null; 
    private WebDriver driver; 


    private InstanPage(){ 

    } 

    public WebDriver openBrowser(){ 

     driver=new FirefoxDriver(); 

     } 
     driver.manage().window().maximize(); 
     return driver; 
    } 

    public static InstanPage getInstance(){ 
     if(instance==null){ 
      instance = new InstanPage(); 
     } 
     return instance; 
    } 

} 

您的測試類,

public class YourTestClass { 

     private InstanPage automation=InstanPage.getInstance(); 
     private WebDriver driver; 
     //this will give the comman instance of Browser. 
     driver=automation.openBrowser(); 
      driver.get("WWW.XYZ.COM"); 
      @test 
      public void testone() 
      { 
      // your test code 
      } 
      } 



    Hope this will solve your Problem.. :) 
+0

謝謝你爲你的感受而努力。 –

1

的單例類:

public class WebDriverSingleton { 

    public static WebDriver driver; 

    public static WebDriver getInstance() { 
    if (driver == null) { 
     driver = new FirefoxWebDriver(); 
    } 
    return driver; 
    } 

} 

,並在您的測試類:

WebDriver driver = WebDriverSingleton.getInstance(); 
相關問題