2010-03-29 26 views
7

我使用IE 6和XPath的Selenium RC非常慢,速度很慢。 所以我想看看如果JavaScript-xpath實際上加快了速度。如何在JavaScript中使用Selenium RC中的xpath?

但是找不到足夠/清晰的關於如何使用本機x- 路徑庫的文檔。

我做了以下內容:

protected void startSelenium (String testServer, String appName, String testInBrowser){ 
    selenium = new DefaultSelenium("localhost", 4444, "*" +testInBrowser, testServer+ "/"+ appName + "/"); 
    echo("selenium instance created:"+selenium.getClass()); 
    selenium.start(); 
    echo("selenium instance started..." + testServer + "/" + appName +"/"); 

    selenium.runScript("lib/javascript-xpath-latest-cmp.js"); 
    selenium.useXpathLibrary("javascript-xpath"); 
    selenium.allowNativeXpath("true"); 
} 

這導致的XPath定位器的速度提高,但 改善並不一致。在某些運行中, 定位器所用的時間減半;而有時它的隨機性很高。

我在這裏是否缺少配置步驟?如果有人在這方面取得了成功,他們可以分享他們的觀點和方法,那將是非常好的。

感謝, 尼爾默爾

解決方案:

protected void startSelenium (String testServer, String appName, String testInBrowser){ 
    selenium = new DefaultSelenium("localhost", 4444, "*" +testInBrowser, testServer+ "/"+ appName + "/"); 
    echo("selenium instance created:"+selenium.getClass()); 
    selenium.start(); 
    echo("selenium instance started..." + testServer + "/" + appName +"/"); 

    selenium.useXpathLibrary("javascript-xpath"); 
} 

回答

4

我自己實現了這一點,我只需要做selenium.useXpathLibrary(「javascript-xpath」)。在我的測試中,IE瀏覽器的JavaScript xpath速度大約快了7倍。還沒有真正測試過其他任何東西,但我們只在IE中使用它。

+0

喜丹, 如果我們不明確添加腳本;發動機從哪裏來?硒是否已經包含這個js?我無法找到任何這樣的證據。 – 2010-03-30 05:55:40

+0

如果你看一下Selenium的源代碼,你會發現javascript-xpath-0.1.11.js被包含在普通的\ src \ js \ core \ xpath中,所以我假設它正在從中拉出。 SOMETHING導致我的測試速度提高了7倍。 =) – 2010-03-30 22:02:55

0

我從來沒有做過這一點,但認爲你可能需要做一些像

//Add the library to the page since runScript just does an eval on the JS 
selenium.runScript("document.body.append(document.createElement('script')).src = 'path/to/lib');"); 
selenium.useXpathLibrary("javascript-xpath"); 
selenium.allowNativeXpath("true"); 

您需要添加該庫到頁面,然後加載它。

但是,我建議使用CSS Selectors而不是XPath Selectors,因爲它們在Selenium中快得多。你可以看到如何使用不同的定位策略here。我看到測試的速度至少比原來的XPath快兩倍。

+0

我目前正在使用CSS選擇器......但它們有點複雜,並且在IE中使用它們時存在更多問題。 我正在探索javascript-xpath提供的速度改進,以查看它們與CSS選擇器的比較。 – 2010-03-30 05:40:56

相關問題