2017-03-08 27 views
0

我正在使用HTML select(即下拉菜單)在最初加載時沒有任何價值的網頁。它僅在其上執行一個onfocus JS事件時加載值。請注意,這是一個GWT控制。關注並點擊使用Selenium的GWT下拉菜單

這是下拉菜單的DOM渲染。注意onfocus事件,實際上在下拉列表中加載的值:

<select id="Field1" class="select not_disabled" onchange="clearOperators(Operator1); addOperators(Field1,Operator1); adjustInput(Field1, 1);" onfocus="loadValues(Field1);" style="width:20em;" name="Field1"> 
</select> 

的問題是,在元素上的點擊是沒有得到註冊,除非該窗口是焦點。因此這些值不會被加載。

我有這個在我的代碼,這與執行不影響:

// bring the focus on the dropdown 
((JavascriptExecutor) webDriver).executeScript("return document.getElementById('Field1').focus;", dropdownElement); 

// click on it to load the values 
dropdownElement.click(); 

偶試過的元素射擊事件的老辦法。沒有工作:

JavascriptLibrary javascript = new JavascriptLibrary(); 

javascript.callEmbeddedSelenium(webDriver, "triggerEvent", dropdownElement, "blur"); 

我也嘗試了一些建議,但沒有幫助:

Correct way to focus an element in Selenium WebDriver using Java

https://groups.google.com/forum/#!msg/selenium-users/jk59XG2TQk8/xbaskp9uFnQJ

可能有人建議我能做些什麼來加載值下拉?我在Firefox 47上使用Selenium 2上的Java。

回答

0

我找到了一種方法來實現這個功能。雖然它不是用於UI測試的非常理想的解決方案,但由於它不模仿「真實用戶」行爲,所以它確實工作正常。

我在我的測試類中調用了onfocus函數,它負責加載值。

(String) ((JavascriptExecutor) webDriver).executeScript("return loadValues(Field1);", dropdownElement); 

此外,我添加了這個優先於Firefox的配置:

profile.setPreference("focusmanager.testmode",true); 

希望它可以幫助別人。