javascript - 使用 Watir 单击具有变量值的单选按钮

标签 javascript ruby-on-rails ruby watir watir-webdriver

我在尝试单击单选按钮时遇到了麻烦。

HTML 代码是:

function radioButtonFormatter(el, oRecord, oColumn, oData) {
var checkFalse='checkFalse';
var check='check'     ;


el.innerHTML="<input type='radio' name='table-radiobutton' value='" + oData + "' onclick='disableButtons(this.checked, this.type)' />";        
}

我想使用类似的东西:

browser.radio(:value => "oData").click

但它不起作用...我也尝试使用一段文字作为引用,但效果不佳。

你们对如何执行它有什么建议吗? 非常感谢!

最佳答案

But it's not working

是的。当页面定义这样的js函数时:

function radioButtonFormatter(el, oRecord, oColumn, oData) {
var checkFalse='checkFalse';
var check='check'     ;


el.innerHTML="<input type='radio' name='table-radiobutton' value='" + oData + "' onclick='disableButtons(this.checked, this.type)' />";        
}

...它不会导致函数执行。如果您执行了这个 ruby​​ 程序:

def greet
  puts 'hello'
end

...您想知道为什么没有输出吗?同样,单选按钮在执行 js 函数之前并不存在。

接下来,这个:

browser.radio(:value => "oData").click

查找具有以下属性的单选按钮:

<radio value="oData" ...>

你想要的是:

browser.radio(:value => oData).click

但是,这意味着您必须创建一个名为 oData 的变量,并为其分配一个值:

oData = "something here"
browser.radio(:value => oData).click

在 ruby​​ 程序中写入变量名 oData 并不会神奇地使其与 javascript 程序中名为 oData 的变量具有相同的值。

以下是您可以执行的一些操作:

3.htm:

<!DOCTYPE html>
<html>
<head>
<title>index.html</title>

<script type="text/javascript">

function disableButtons(x, y) {
  document.getElementById("disable-info").innerHTML = x + " " + y;
}

function radioButtonFormatter(el, oData) {
  var checkFalse='checkFalse';
  var check='check'     ;


  el.innerHTML="<input type='radio' name='table-radiobutton' value='" + oData + "' onclick='disableButtons(this.checked, this.type)' />";        
}

</script>

</head>
<body>
  <div>Hello world</div> 
  <div id="div1"></div>
  <div id="disable-info"</div>
</body>
</html>

require 'watir-webdriver'

b = Watir::Browser.new :firefox

b.goto "http://localhost:8080/3.htm"

b.execute_script(
  "radioButtonFormatter(
    document.getElementById('div1'), 42
  )"
)

b.radio(:value => "42").click

或者,如果 radioButtonFormatter() 函数由某个事件触发,您可以使用 watir-webdriver 触发该事件:

<!DOCTYPE html>
<html>
<head>
<title>index.html</title>

<script type="text/javascript">

function disableButtons(x, y) {
  document.getElementById("disable-info").innerHTML = x + " " + y;
}

function radioButtonFormatter(el, oData) {
  var checkFalse='checkFalse';
  var check='check'     ;


  el.innerHTML="<input type='radio' name='table-radiobutton' value='" + oData + "' onclick='disableButtons(this.checked, this.type)' />";        
}

window.onload = function() {
  document.getElementById('greeting').onclick = function() {
    radioButtonFormatter(document.getElementById('div1'), 42);
  }
}

</script>

</head>
<body>
  <div id="greeting">Hello world</div> 
  <div id="div1"></div>
  <div id="disable-info"</div>

</body>
</html>

require 'watir-webdriver'

b = Watir::Browser.new :firefox

b.goto "http://localhost:8080/3.htm"

b.div(id: "greeting").when_present.click  #This causes radioButtonFormatter() to execute
b.radio(value: "42").when_present.click

但这并不能让您控制 oData 的值。您必须查看 js 来确定设置了 oData 的值,然后在 ruby​​ 脚本中使用该值。

关于javascript - 使用 Watir 单击具有变量值的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18797890/

相关文章:

javascript - 在 for 循环中处理多个异步函数调用

javascript - html() 中的 jQuery 编码

javascript - 如何在node js中读取音频流?

ruby-on-rails - RSpec 测试 `rand` 内部 `rand`

ruby-on-rails - 组合具有相同符号值的哈希

ruby - 如何知道超时是由于未出现的元素还是 400..500 错误页面引起的

javascript - 是否可以从 AWS JavaScript v3 SDK 调用 AWS Step Functions?

ruby-on-rails - Rails3 - 如何在应用程序中获取 aws-s3 的 yml 配置数据?

ruby - 新手 Ruby on Rails - 从 View 将字符串传递给方法?

php - 在我的本地 Windows 机器上,我如何编写一个脚本来每天下载漫画并将其通过电子邮件发送给自己?