しかし、それは機能していません
はい。ページが次のような 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 スクリプトで使用する必要があります。