jQuery UI 对话框按钮不响应 Selenium/Webrat 中的 click_button 或 selenium.click

发布于 2024-08-13 22:32:29 字数 2580 浏览 4 评论 0原文

有谁能够让 jQuery UI 对话框按钮响应 click_button 或 selenium.click 吗?我似乎无法让它发挥作用。

基本上,我正在尝试使用 Cucumber/Webrat/Selenium 在 Rails 应用程序的 jQuery UI 对话框中测试表单。

我有一个包含许多表格行的页面,单击每一行都会弹出一个带有表单的对话框。每个表单元素都有一个唯一的 id,因此标记是有效的。

由于按钮可以通过对话框插件动态创建,因此我初始化对话框以添加“保存”和“取消”按钮。有趣的是,该插件插入了按钮标签,而不是输入标签。我还在打开时添加了 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>

我的 Cucumber 步骤(目前)如下所示:

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

当我运行 Cucumber 并点击“调试器”时,我可以在输入字段中手动“selenium.click”。

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

这成功地将光标置于该字段中。但是,单击该按钮不起作用:

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

当我在命令行调试器中键入该按钮时,它返回 nil (我相信这是成功的,因为没有例外),但 Firefox 不执行任何操作。该对话框在浏览器中保持打开状态。当我输出response.body时,该按钮就出现了。

我也尝试过,

click_button "inventory_392827_save_button"

但“selenium_wait”命令超时,这意味着它看不到该元素。

我被困住了...

Has anyone been able to get the jQuery UI Dialog buttons to respond to click_button or selenium.click? I can't seem to be able to get this to work.

Basically, I'm trying to test a form in a jQuery UI Dialog in a Rails app using Cucumber/Webrat/Selenium.

I have a page with many table rows, and each row on click fires off a dialog box with a form. Each form element has a unique id so the markup is valid.

Since the buttons can be created dynamically by the Dialog plugin, I initialize the dialog to add a 'Save' and 'Cancel' button. Interestingly, the plugin inserts a button tag, not an input tag. I also add ids on open as shown below, so the buttons can be targeted by the testing framework.

$('.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');
  }
});

The markup looks like:

<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>

My Cucumber step (presently) looks like:

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

When I run Cucumber and it hits 'debugger', I am able to manually 'selenium.click' in the input fields.

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

This successfully puts a cursor in that field. However, clicking the button does not work:

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

When I type that in the command line debugger, it returns nil (which I believe is success, since there is no exception), but Firefox doesn't do anything. The dialog box stays open in the browser. When I output response.body, that button is present.

I also tried

click_button "inventory_392827_save_button"

but the 'selenium_wait' command times out which means it doesn't see that element.

I'm stuck...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

日裸衫吸 2024-08-20 22:32:29

如果将其更改为“fireEvent ... click”或 clickAt,问题会消失吗?

Does the problem go away if you change it to "fireEvent ... click" or to clickAt?

苏大泽ㄣ 2024-08-20 22:32:29

通过其他位置策略(例如 XPath 或 CSS)来定位按钮可能会更好。如果您添加按钮的 HTML,那么我会尝试提供一些建议。

例如,如果您的目标按钮具有“提交”类别,您可以使用:

css=input.submit

如果目标按钮的值为“Click Me”,您可以使用:

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

另一个建议是使用 id 属性,而不使用唯一的编号,因此以下内容可能适合您:

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

It might be better to locate your button by another location strategy such as XPath or CSS. If you add the HTML of the button(s) then I'll try to give some suggestions.

If your target button has a class of 'submit' for example you could use:

css=input.submit

If the target button has a value of 'Click Me' you could use:

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

Another suggestion would be to use the id attribute without the unique number, so the following may work for you:

//input[substring(@id, string-length(@id) -11, 12) = '_save_button']
习ぎ惯性依靠 2024-08-20 22:32:29

我有一个类似的问题,这是我如何完成它的(使用 Capybara/Selenium/Factory_Girl)。

jQuery UI 对话框按钮生成此 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

I had a similar problem, and here's how i got it done (using Capybara/Selenium/Factory_Girl).

The jQuery UI Dialog buttons generates this HTML, in my case with two buttons Add and Cancel:

<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>

But the click_button method doesn't work because the actual button name is a span
nested inside the button tag, as you can see above.

So i wrote in the .feature step:

When I press the jquery dialog "Add" button

And i added this to my '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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文