使用 ruby​​ 从 excel 中选择单选按钮 [英] Select Radio buttons from excel with ruby

查看:42
本文介绍了使用 ruby​​ 从 excel 中选择单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 Excel 电子表格中选择单选按钮进行数据驱动测试.

I would like to select radio button from an excel spreadsheet for a data driven test.

我尝试了以下方法:

radio=worksheet.cells(rows,"A").value
browser.radio(:name => 'name').set radio

我也尝试过选择字符串,但出现错误:

I also tried selecting as a string but got errors:

radio=worksheet.cells(rows,"A").value
string_from_excel = "set"
browser.send string_from_excel, radio

单选按钮的代码是:

<input id="radio" type="radio" onclick="setradio(0)" value="0" name="radio">

感谢您的帮助

推荐答案

首先让我们尝试使您的代码更易于阅读.注意到评论和其他答案中的所有问题了吗?不清楚您从电子表格中检索什么,并且保存该数据radio"的变量的命名只是在已经散布着该词的一段代码(和相关的 html)中呈现了该术语的另一个实例.将其称为 radio_idradio_valueradio_name 之类的名称,以使任何人都清楚应该从电子表格中检索什么,以及该变量究竟包含什么.从现在起 4 个月后,这对你自己也会更有意义.

First lets try to make your code a bit easier to read. Notice all the questions in the comments and other answers? It's not clear what you are retrieving from the spreadsheet, and the naming of the variable that holds that data 'radio' just presents yet another instance of that term in a section of code (and associated html) that is littered with that word already. Call it something like radio_id, radio_value or radio_name to make it clear to anyone what should getting retrieved from the spreadsheet, and what exactly that variable contains. It will also make more sense to yourself 4 months from now.

其次,仔细评估如何正确识别您需要的物品.您的示例 HTML 仅显示一个单选控件,但通常这些控件是成套出现的,而且大多数情况下,NAME 用于标识公共集中的单选控件,请参阅此页面上的示例.因此,使用 name 属性通常是一种识别单选按钮的糟糕方式.值,或嵌入的文本通常是从集合中获得正确"无线电控制的更好方法.

Second, carefully evaluate how to properly identify the item you need. Your sample HTML only shows a single radio control, but usually these controls come in sets, and most often the NAME is used to identify radio controls that are in a common set, see the examples on this page. Using the name attribute is therefore quite often a terrible way to identify a radio button. Value, or the embedded text is usually a far better way of getting the 'right' radio control out of the set.

第三,watir 单选按钮对象的 .set 方法不接受参数,它只是确保指定的特定单选控件处于设置"状态..set 的补充是 .clear(参见 Watir RDOC 了解更多信息)

Thirdly, the .set method for a watir radio button object does not accept a parameter, it merely ensures the particular radio control specified is in the 'set' state. The complement to .set is .clear (see the Watir RDOC for more info)

此外,您的控件在 HTML 中定义了一个事件处理程序.这是在控件收到 onclick 事件时执行脚本.因此,在设置(或者可能不是设置)之后,为了让页面正常工作,您可能需要触发 onclick 事件,以便调用正确的客户端代码.

Also Your control has a event handler defined in it in the HTML. This is executing a script when the control receives an onclick event. So after setting it (or perhaps instead of setting it) in order for the page to work correctly, you may need to fire the onclick event so that the proper client side code is called.

考虑到这一点,我会尝试类似这样的代码

With that in mind I'd try code that looks something like this

radio_value = worksheet.cells(rows,"A").value
browser.radio(:value => radio_value).set 
browser.radio(:value => radio_value).fire_event('onclick')

肥皂盒:
收音机成套出现的另一个原因是您不能用鼠标取消设置"单选按钮,除非选择同一组中的另一个按钮.您几乎从未在网页中看到单独的单选按钮.如果需要可以选择或取消选择的单个事物,通常使用复选框"元素.如果开发人员通过实现 javascript 代码使其表现得像一个复选框,将单个收音机用作复选框",则他们需要(恕我直言)被取出并射击解雇并替换为一名开发人员知道如何将 CSS 用于此类事情,而不是滥用糟糕的无线电控制并创建一个页面,如果有人禁用了 javascript,则该页面将无法正常工作.

Soapbox:
Another reason radio's come in sets is that you cannot 'un-set' a radio button with your mouse except by picking another button in the same set. You almost never see a single radio button standing alone in a web-page. If a single thing that can be selected or unselected is needed, a 'checkbox' element is usually used. If a developer is using a single radio as a 'checkbox' by implementing javascript code to make it behave like a checkbox, they need (IMHO) to be taken out and shot sacked and replaced with a developer who knows how to use CSS for such things instead of abusing the poor radio control and creating a page that will not work properly if someone has javascript disabled.

这篇关于使用 ruby​​ 从 excel 中选择单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆