2011-04-02 52 views

回答

0

這是我的錯。

意外的行爲,由此引起的代碼和發生,因爲我停止硒兩次(硒對象永遠不會成爲空):

public class SeleniumController { 
    private static Selenium selenium; 

    public static Selenium startNewSelenium(){ 
     // if already exists stop it and replace with new one 
     if(selenium != null){ 
      selenium.stop(); 
     } 

     selenium = createNewSelenium(getCurContext()); 
     return selenium; 
    } 

    public static void stopSelenium() { 
     if(selenium != null){ 
      selenium.stop(); 
     } 
    } 

    private static Selenium createNewSelenium(TestContext testContext){ 
     TestProperties testProps = new TestProperties(testContext); 
     ExtendedSelenium selenium = new ExtendedSelenium("localhost", RemoteControlConfiguration.DEFAULT_PORT, 
       testProps.getBrowser(), testProps.getServerUrl()); 
     selenium.start(); 
     selenium.useXpathLibrary("javascript-xpath"); 
     selenium.allowNativeXpath("false");  
     return selenium; 
    } 
} 

正確的類代碼:

public class SeleniumController { 
    private static Selenium selenium; 

    public static Selenium startNewSelenium(){ 
     // if already exists stop it and replace with new one 
     stopSelenium();   
     selenium = createNewSelenium(getCurContext()); 
     return selenium; 
    } 

    public static void stopSelenium() { 
     if(selenium != null){ 
      selenium.stop(); 
      selenium = null; 
     } 
    }  

    private static Selenium createNewSelenium(TestContext testContext){ 
     TestProperties testProps = new TestProperties(testContext); 
     ExtendedSelenium selenium = new ExtendedSelenium("localhost", RemoteControlConfiguration.DEFAULT_PORT, 
       testProps.getBrowser(), testProps.getServerUrl()); 
     selenium.start(); 
     selenium.useXpathLibrary("javascript-xpath"); 
     selenium.allowNativeXpath("false");  
     return selenium; 
    } 
} 
0

爲什麼你認爲它不應該是安全的?除非它可以正常工作,否則它沒有問題,如果它不能再次重新創建DefaultSelenium對象,它不會減慢代碼的速度

+0

問題是硒開始意外工作 – nahab 2011-04-02 14:20:21

0

您應該在設置和拆卸時通常保持start()和stop()方法。在使用TestNG時,您可以使用@BeforeClass和@AfterClass annonations進行註釋。因此瀏覽器只會在課程中的測試方法之前和之後啓動和關閉。

您是否支持Selenium方案51 - http://area51.stackexchange.com/proposals/4693/selenium 此提案得到了SeleniumHQ的支持,我們需要更多的用戶致力於讓它看到光明的一天。

2

沒有什麼錯打開多個瀏覽器(您稱之爲「硒」)。實際上,這是您測試某些應用程序的唯一方法。想象一下,具有管理UI和最終用戶UI的應用程序,您可以在管理端進行更改並在用戶端驗證其影響。您可以編寫測試在同一個瀏覽器會話中在兩者之間來回切換,也可以打開兩個瀏覽器,其中一個用於應用程序的各個方面。前者是通常的技術,但後者更清潔。