2009-12-07 73 views
1

有沒有人能夠獲得jQuery UI對話框按鈕來響應click_button或selenium.click?我似乎無法得到這個工作。jQuery UI對話框按鈕不響應Selenium/Webrat中的click_button或selenium點擊

基本上,我試圖在使用Cucumber/Webrat/Selenium的Rails應用程序中的jQuery UI對話框中測試表單。

我有一個包含許多表格行的頁面,每一行點擊都會觸發一個帶有窗體的對話框。每個表單元素都有一個唯一的ID,所以標記是有效的。

由於按鈕可以通過對話框插件動態創建,我初始化對話框添加一個'保存'和'取消'按鈕。有趣的是,插件插入一個按鈕標籤,而不是輸入標籤。我也在open上添加了id,如下所示,所以按鈕可以被測試框架作爲目標。

$('.inventory_dialog').dialog({ 
    autoOpen: false, 
    modal: true, 
    buttons: { 
    'Save': function() { 
     // ajax submit stuff 
    }, 
    Cancel: function() { 
     // cancel stuff 
    } 
    }, 
    open: function() { 
    // add ids to buttons for selenium 
    var inventory_id = $(this).attr('id').split('_').pop(); 
    $('.ui-dialog-buttonpane') 
     .find('button:contains("Save")').attr('id', 'inventory_'+inventory_id+'_save_button') 
     .end() 
     .find('button:contains("Cancel")').attr('id', 'inventory_'+inventory_id+'_cancel_button'); 
    } 
}); 

的標記看起來像:

<div id="inventory_dialog_392827" class="inventory_dialog"> 
    <form action="/suppliers/22/listings/27738/inventory/392827" class="edit_inventory" id="edit_inventory_392827" method="post"><div style="margin:0;padding:0"><input name="_method" type="hidden" value="put" /></div> 
    <div class="input_block"> 
     <label for="inventory_392827_new_quantity">Quantity</label> 
     <input id="inventory_392827_new_quantity" name="inventory[new_quantity]" type="text" value="10" /> 
    </div> 
    <div class="input_block"> 
     <label for="inventory_392827_claim_quantities">Groups of</label> 
     <input id="inventory_392827_claim_quantities" name="inventory[claim_quantities]" type="text" value="6-8" /> 
    </div> 
    </form> 
</div> 

我的黃瓜步驟(目前)是這樣的:

When /^I click the "(.+)" button in the inventory dialog$/ do |button_name| 
    debugger 
    selenium.click "//button[@id='inventory_#{@current_offer.id}_#{button_name.downcase}_button']" 
    selenium_wait 
end 

當我運行黃瓜和它打 '調試',我能在輸入字段中手動'selenium.click'。

selenium.click "//input[@id='inventory_392827_new_quantity']" 

這成功地將光標置於該字段中。但是,單擊按鈕不起作用:

selenium.click "//button[@id='inventory_392827_save_button']" 

當我鍵入的命令行調試,它返回零(我認爲是成功的,因爲沒有例外),但Firefox不會做任何事情。該對話框在瀏覽器中保持打開狀態。當我輸出response.body時,該按鈕存在。

我也試過

click_button "inventory_392827_save_button" 

但「selenium_wait」命令超時,這意味着它不會看到該元素。

我卡住了...

+0

您的標記不顯示的按鈕。按鈕是否已正確創建? – AutomatedTester 2009-12-07 09:39:42

+0

這些按鈕是由jQuery對話框插件動態創建的,所以我沒有將它包含在原始標記中。 – cotopaxi 2009-12-08 02:18:04

回答

0

問題是否消失,如果你把它改爲「fireEvent ...點擊」或clickAt?

+0

我還沒有嘗試過(對不起),但看起來我的問題與response.body吐出多個按鈕具有相同的id,雖然在瀏覽器中的id是唯一的。我很困惑。 – cotopaxi 2009-12-08 02:19:00

0

通過另一種位置策略(如XPath或CSS)來定位按鈕可能會更好。如果你添加了按鈕的HTML,那麼我會試着給出一些建議。

如果你的目標按鈕具有一類的「提交」例如,你可以使用:

css=input.submit 

如果目標按鈕具有的「點擊我」的值,你可以使用:

//input[@value='Click Me'] 

另一個建議是使用沒有唯一編號的id屬性,因此以下內容可能適用於您:

//input[substring(@id, string-length(@id) -11, 12) = '_save_button'] 
0

我有一個類似的問題,以下是我如何完成(使用水豚/硒/ Factory_Girl)。

jQuery的用戶界面對話框按鈕生成此HTML,在我的情況有兩個按鈕添加和取消:

<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix"> 
<button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false"> 
    <span class="ui-button-text">Add</span> 
</button> 

<button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false"> 
    <span class="ui-button-text">Cancel</span> 
</button> 
</div> 

但click_button方法不起作用,因爲實際的按鈕名稱是嵌套在一個跨度 按鈕標籤,如上所示。

所以我寫在.feature步:

When I press the jquery dialog "Add" button 

而且我將此添加到我的 'base.rb':

# jQuery Dialog buttons 
When /^I press the jquery dialog "([^\"]*)" button$/ do |button_text| 
    page.find("div.ui-dialog-buttonpane").find('span', :text => button_text).click 
end