Watir-headless-testing
提供:Dev Guides
Watir-ヘッドレステスト
この章では、Watir Webdriverのヘッドレスオプションを使用してページURLをテストする方法を学習します。
構文
Browser = Watir::Browser.new :chrome, headless: true
私たちがテストしようとしているテストページはここに示されています-
<html>
<head>
<title>Testing UI using Watir</title>
</head>
<body>
<script type = "text/javascript">
function wsentered() {
console.log("inside wsentered");
var firstname = document.getElementById("firstname");
if (firstname.value != "") {
document.getElementById("displayfirstname").innerHTML =
"The name entered is : " + firstname.value;
document.getElementById("displayfirstname").style.display = "";
}
}
</script>
<div id = "divfirstname">
Enter First Name : <input type = "text" id = "firstname" name = "firstname" onchange = "wsentered()"/>
</div>
<br/>
<br/>
<div style = "display:none;" id = "displayfirstname"></div>
</body>
</html>
出力
Watirコード
require 'watir'
b = Watir::Browser.new :chrome, headless: true
b.goto('http://localhost/uitesting/textboxl')
t = b.text_field(name: 'firstname')
t.exists?
t.set 'Riya Kapoor'
t.value
t.fire_event('onchange')
b.screenshot.save 'headless.png'
Watir chromeブラウザにオプション_headless_:trueを追加しました。 あなたがRubyプログラムを実行するとき、それはブラウザを開かず、すべてがコマンドラインで実行されます-
DevTools listening on ws://127.0.0.1:53973/devtools/browser/b4127866-afb8-4c74-b967-5bacb3354b19
[0505/144843.905:INFO:CONSOLE(8)] "inside wsentered", source: http://localhost/uitesting/textboxl (8)
console.logメッセージを追加し、コマンドラインに同じメッセージを出力しました。
headless.pngのスクリーンショットを以下に示します-
Firefoxでは
Firefoxのwatirコードはここに示されています-
require 'watir'
b = Watir::Browser.new :firefox, headless: true
b.goto('http://localhost/uitesting/textboxl')
t = b.text_field(name: 'firstname')
t.exists?
t.set 'Riya Kapoor'
t.value
t.fire_event('onchange')
b.screenshot.save 'headlessfirefox.png'
headlessfirefox.pngのスクリーンショットを次に示します-