2010-12-18 56 views
1

我一直在嘗試大部分時間來啓用Selenium RC上的JQuery定位器使用我在interwebs上發現的各種建議,但沒有多少運氣。我已經按照包含在該線程使JQuery的定位器的建議:獲取硒RC空null sessionId異常試圖啓用JQuery AddLocationStrategy

How do I add a JQuery locators to Selenium Remote Control

我打補丁的TestRunner文件的建議,我應用了相同的修復到RemoteRunner文件。我還修補了相應的* .hta文件。我還將縮小的jquery.min.js文件添加到JAR文件的lib目錄中。

我也嘗試保持服務器JAR完好無損,並使用user-extensions.js文件(其中包含jquery.min.js)。但是這也沒有奏效。

在任何情況下,我得到以下運行時錯誤:

19:10:50.174 ERROR - Exception running 'addLocationStrategy 'command on session null java.lang.NullPointerException: sessionId should not be null; has this session been started yet?

我的配置是:

的Win7 64位
IIS
硒服務器1.0.3
Firefox
C#

我發現了兩種風格的JavaScript用於調用.Ad dLocationStrategy()。下面是我實現的:

[SetUp] 
public void SetupTest() 
{ 
    selenium = SeleniumUtils.GetSeleniumWithJQueryStrategy("localhost", 4444, "*firefox", "http://localhost:55023"); 
    selenium.Start(); 
    sbVerificationErrors = new StringBuilder(); 
} 

這是我的實用工具類

public static class SeleniumUtils 
    { 
    public static ISelenium GetSeleniumWithJQueryStrategy(string serverHost, int serverPort, string browserString, string browserURL) 
    { 
     ISelenium selenium = new DefaultSelenium(serverHost, serverPort, browserString, browserURL); 
     selenium.AddLocationStrategy("jquery", GetJQueryLocationStrategy2()); 
     return selenium; 
    } 

    public static string GetJQueryLocationStrategy2() 
    { 
     string r = @" 
    var loc = locator; 
    var attr = null; 
    var isattr = false; 
    var inx = locator.lastIndexOf('@'); 

    if (inx != -1) 
    { 
     loc = locator.substring(0, inx); 
     attr = locator.substring(inx + 1); 
     isattr = true; 
    } 

    var selectors = loc.split('<'); 
    var found = $(inDocument); 

    for (var i = 0; i < selectors.length; i++) 
    { 
     if (i > 0) 
     { 
      found = $(found.parents()[0]); 
     } 

     if (jQuery.trim(selectors[i]) != '') 
     { 
      found = found.find(selectors[i]); 
     } 
    } 

    if (found.length > 0) 
    { 
     if (isattr) 
     { 
      return found[0].getAttributeNode(attr); 
     } 
     else 
     { 
      return found[0]; 
     } 
    } 
    else 
    { 
     return null; 
    }"; 
     return r; 

    } 

    public static string GetJQueryLocationStrategy() 
    { 
     string r = @" 
    var loc = locator; 
    var attr = null; 
    var isattr = false; 
    var inx = locator.lastIndexOf('@'); 

    if (inx != -1) 
    { 
     loc = locator.substring(0, inx); 
     attr = locator.substring(inx +1); 
     isattr = true; 
    } 

    var found = jQuery(inDocument).find(loc); 

    if (found.length >= 1) 
    { 
     if (isattr) 
     { 
      return found[0].getAttribute(attr); 
     } 
     else 
     { 
      return found[0]; 
     } 
    } 
    else 
    { 
     return null; 
    }"; 
     return r; 
    } 
    } 

的來電來訪失敗:

19:10:13.297 INFO - Started [email protected]
19:10:50.139 INFO - Checking Resource aliases
19:10:50.151 INFO - Command request: addLocationStrategy[jquery,
var loc = locator;
...(echoes rest of Javascript)...
}] on session null
19:14:09.796 ERROR - Exception running 'addLocationStrategy 'command on session null java.lang.NullPointerException: sessionId should not be null; has this session been started yet?
at org.openqa.selenium.server.FrameGroupCommandQueueSet.getQueueSet(FrameGroupCommandQueueSet.java:216)
at org.openqa.selenium.server.commands.SeleniumCoreCommand.execute(SeleniumCoreCommand.java:34)

+0

所以,它玩了之後這我意識到我需要調用「selenium.Start()」之前調用「selenium.AddLocationStrategy(...)」 – 2010-12-18 15:57:46

回答

0

的SessionID無效通常意味着硒對象未獲通過。嘗試傳遞該對象,它會起作用。

+0

你可以更具體嗎?你指的是哪種方法? .AddLocationStrategy()沒有重載。我看到的唯一的另一種可能性是使用.DefaultSelenium()的重載。 – 2010-12-18 15:43:06

0

事實證明,我需要調用「selenium.Start()」調用「selenium.AddLocationStrategy(...)」之前,這裏是修改後的代碼:

[SetUp] 
    public void SetupTest() 
    { 
    selenium = SeleniumUtils.GetSeleniumWithJQueryStrategy("localhost", 4444, "*firefox", "http://localhost:55023"); 
    sbVerificationErrors = new StringBuilder(); 
    } 

public static class SeleniumUtils 
{ 
    public static ISelenium GetSeleniumWithJQueryStrategy(string serverHost, int serverPort, string browserString, string browserURL) 
    { 
     ISelenium selenium = new DefaultSelenium(serverHost, serverPort, browserString, browserURL); 
     // Need to call .Start() before calling .AddLocationStrategy() 
     selenium.Start(); 
     selenium.AddLocationStrategy("jquery", GetJQueryLocationStrategy()); 

     return selenium; 
    } 
}