
- Watir 教程
- Watir - 首頁
- Watir - 概述
- Watir - 簡介
- Watir - 環境設定
- Watir - 安裝瀏覽器驅動程式
- Watir - 使用瀏覽器
- Watir - Web 元素
- Watir - 定位 Web 元素
- Watir - 使用 Iframes
- Watir - 自動等待
- Watir - 無頭測試
- Watir - 移動測試
- Watir - 捕獲螢幕截圖
- Watir - 頁面物件
- Watir - 頁面效能
- Watir - Cookie
- Watir - 代理
- Watir - 警報
- Watir - 下載
- Watir - 瀏覽器視窗
- Watir 有用資源
- Watir - 快速指南
- Watir - 有用資源
- Watir - 討論
Watir - Web 元素
本章將討論如何在 Watir 中使用以下內容:
- 使用文字框
- 使用組合框
- 使用單選按鈕
- 使用複選框
- 使用按鈕
- 使用連結
- 使用 Div
使用文字框
語法
browser.text_field id: 'firstname' // will get the reference of the textbox
這裡我們將嘗試瞭解如何在 UI 上使用文字框。
考慮如下所示的頁面 Textbox.html:
<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>
相應的輸出如下所示:

我們有一個文字框,當您輸入名稱時,將觸發 onchange 事件,並在下方顯示名稱。
現在讓我們編寫程式碼,在其中我們將定位文字框,輸入名稱並觸發 onchange 事件。
Watir 程式碼
require 'watir' b = Watir::Browser.new :chrome b.goto('https:///uitesting/textbox.html') t = b.text_field id: 'firstname' t.exists? t.set 'Riya Kapoor' t.value t.fire_event('onchange')
我們使用 Chrome 瀏覽器,並將頁面 URL 指定為https:///uitesting/textbox.html。
使用goto api,瀏覽器將開啟頁面 URL,我們將查詢 id 為 firstname 的 text_field。如果存在,我們將值設定為 Riya Kapoor,並將使用fire_event api 觸發 onchange 事件。
現在,讓我們執行程式碼以顯示如下所示的輸出:


使用組合框
語法
browser.select_list id: 'months' // will get the reference of the dropdown
我們將要測試的測試頁面顯示在此處:
<html> <head> <title>Dropdown</title> </head> <body> <script type = "text/javascript"> function wsselected() { var months = document.getElementById("months"); if (months.value != "") { document.getElementById("displayselectedmonth").innerHTML = "The month selected is : " + months.value; document.getElementById("displayselectedmonth").style.display = ""; } } </script> <form name = "myform" method = "POST"> <div> Month is : <select name = "months" id = "months" onchange = "wsselected()"> <option value = "">Select Month</option> <option value = "Jan">January</option> <option value = "Feb">February</option> <option value = "Mar">March</option> <option value = "Apr">April</option> <option value = "May">May</option> <option value = "Jun">June</option> <option value = "Jul">July</option> <option value = "Aug">August</option> <option value = "Sept">September</option> <option value = "Oct">October</option> <option value = "Nov">November</option> <option value = "Dec">December</option> </select> </div> <br/> <br/> <div style = "display:none;" id = "displayselectedmonth"> </div> </body> </html>
輸出

從下拉列表中選擇月份時,將在下方顯示相同的月份。
現在讓我們使用 Watir 進行測試。
組合框選擇的 Watir 程式碼
require 'watir' b = Watir::Browser.new :chrome b.goto('https:///uitesting/combos.html') t = b.select_list id: 'months' t.exists? t.select 'September' t.selected_options t.fire_event('onchange')
要使用組合框,您需要使用 b.select_list api 和下拉列表的 id 來定位 select 元素。要從下拉列表中選擇值,您需要使用 t.select 和所需的值。
執行後的輸出如下:

使用單選按鈕
語法
browser.radio value: 'female' // will get the reference of the radio button with value “female”
這是一個我們將用於處理單選按鈕的測試頁面:
<html> <head> <title>Testing UI using Watir</title> </head> <body> <form name = "myform" method = "POST"> <b>Select Gender?</b> <div> <br/> <input type = "radio" name = "gender" value = "male" checked> Male <br/> <input type = "radio" name = "gender" value = "female"> Female <br/> </div> </form> </body> </html>

我們將選擇值為 Female 的單選按鈕,如 Watir 程式碼所示:
require 'watir' b = Watir::Browser.new b.goto('https:///uitesting/radiobutton.html') t = b.radio value: 'female' t.exists? t.set b.screenshot.save 'radiobutton.png'
要使用單選按鈕,我們需要告訴瀏覽器我們選擇的 value,即b.radio value:”female”
我們還截取了螢幕截圖並將其儲存為 radiobutton.png,如下所示:

使用複選框
語法
browser. checkbox value: 'Train' // will get the reference of the checkbox with value “Train”
這是複選框的測試頁面:
<html> <head> <title>Testing UI using Watir</title> </head> <body> <form name = "myform" method = "POST"> <b>How would you like to travel?</b> <div> <br> <input type = "checkbox" name = "option1" value = "Car"> Car<br> <input type = "checkbox" name = "option2" value = "Bus"> Bus<br> <input type = "checkbox" name = "option3" value = "Train"> Train<br> <input type = "checkbox" name = "option4" value = "Air"> Airways<br> <br> </div> </form> </body> </html>

現在,讓我們使用 Watir 定位瀏覽器中的複選框,如下所示:
require 'watir' b = Watir::Browser.new b.goto('https:///uitesting/checkbox.html') t = b.checkbox value: 'Train' t.exists? t.set b.screenshot.save 'checkbox.png'
要定位瀏覽器中的複選框,請使用b.checkbox 和要選擇的值。

使用按鈕
語法
browser.button(:name => "btnsubmit").click // will get the reference to the button element with has name “btnsubmit”
這是按鈕的測試頁面:
<html> <head> <title>Testing UI using Watir</title> </head> <body> <script type = "text/javascript"> function wsclick() { document.getElementById("buttondisplay").innerHTML = "Button is clicked"; document.getElementById("buttondisplay").style.display = ""; } </script> <form name = "myform" method = "POST"> <div> <br> <input type = "button" id = "btnsubmit" name = "btnsubmit" value = "submit" onclick = "wsclick()"/> <br> </div> </form> <br/> <div style = "display:none;" id = "buttondisplay"></div> </body> </html>

這是在給定頁面上定位按鈕的 watir 程式碼:
require 'watir' b = Watir::Browser.new b.goto('https:///uitesting/button.html') b.button(:name => "btnsubmit").click b.screenshot.save 'button.png'
這是螢幕截圖 button.png

使用連結
語法
browser.link text: 'Click Here' // will get the reference to the a tag with text ‘Click Here’
我們將使用以下測試頁面來測試連結:
<html> <head> <title>Testing UI using Watir</title> </head> <body> <br/> <br/> <a href = "https://www.google.com">Click Here</a> <br/> </body> </html>

測試連結所需的 Watir 詳細資訊如下:
require 'watir' b = Watir::Browser.new b.goto('https:///uitesting/links.html') l = b.link text: 'Click Here' l.click b.screenshot.save 'links.png'
輸出


使用 Div
語法
browser.div class: 'divtag' // will get the reference to div with class “divtag”
我們可以測試 div 的測試頁面。
<html> <head> <title>Testing UI using Watir</title> <style> .divtag { color: blue; font-size: 25px; } </style> </head> <body> <br/> <br/> <div class = "divtag"> UI Testing using Watir </div> <br/> </body> </html>
輸出

此處顯示了測試 div 的 Watir 程式碼:
require 'watir' b = Watir::Browser.new b.goto('https:///uitesting/div.html') l = b.div class: 'divtag' l.exists? l.text b.screenshot.save 'divtag.png'
輸出
