Watir - 定位網頁元素



在 Watir 中進行測試時,您需要定位元素,這可以透過多種方式完成 - 使用元素的 id、class 或文字。

在本章中,我們將看到一些示例,這些示例展示了定位元素的不同方法。

使用元素的 ID

測試頁面

<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>

示例

require 'watir'
b = Watir::Browser.new :chrome
b.goto('https:///uitesting/textbox.html')
t = b.text_field(id: 'firstname') // using the id of the textbox to locate the textbox
t.exists?
t.set 'Riya Kapoor'
b.screenshot.save 'textboxbefore.png'
t.value
t.fire_event('onchange')
b.screenshot.save 'textboxafter.png'

在此示例中,我們使用文字框元素的 id 來定位它並設定值。

t = b.text_field(id: 'firstname')

輸出

Using ID

Using ID Element

如果您需要定位 div、span 或任何其他 html 標籤,您可以使用 id 以如下方式執行相同操作:

對於 div

browser.div(id: "divid")
browser.div(id: /divid/)

對於 span

browser.span(id: "spanid")
browser.span(id: /spanid/)

使用元素的 NAME

測試頁面

<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>

示例

require 'watir'
b = Watir::Browser.new :chrome
b.goto('https:///uitesting/textbox.html')
t = b.text_field(name: 'firstname') // name is used to locate the textbox element
t.exists?
t.set 'Riya Kapoor'
b.screenshot.save 'textboxbefore.png'
t.value
t.fire_event('onchange')
b.screenshot.save 'textboxafter.png'

輸出

Using ID

Using ID Element

使用標籤名稱

您可以透過直接使用 html 標籤來定位所需的任何 html 元素,如下所示。

對於 div

browser.div(id: "divid")
browser.div(id: /divid/)

對於 span

browser.span(id: "spanid")
browser.span(id: /spanid/)

對於 p 標籤

browser.p(id: "ptag")
browser.p(id: /ptag/)

對於按鈕

browser.button(id: "btnid")
browser.button(id: /btnid/)

使用類名

您可以使用元素的類名來定位它。這可以透過以下方式完成:

對於 div

browser.div(class: "divclassname")
browser.div(class: /divclassname/)

對於 span

browser.span(class: "spanclassname”)
browser.span(class: /spanclassname/)

對於 p 標籤

browser.p(class: "pclassname")
browser.p(class: /pclassname/)

對於按鈕

browser.button(class: "btnclassname")
browser.button(class: /btnclassname/)

對於文字框

browser.text_field(class: 'txtclassname')
browser.text_field(class: /txtclassname/)

您還可以傳遞多個類,如下所示:

對於 div

browser.div(class: ["class1", "class2"])

使用文字

這是另一種透過使用帶有文字的元素來定位元素的方法。例如:

browser.button(text: "button text")
browser.button(text: /button text/)

使用標籤

您可以使用元素的標籤來定位它,如下所示:

browser.text_field(label: "text here"))
browser.text_field(label: /text here/))

使用資料屬性

如果您在 html 標籤中具有資料屬性,則可以使用它來定位元素,如下所示:

例如,您可以如下所示定位標籤:

<div data-type = "test1"></div>

您可以如下所示定位 div:

browser.div(data-type: 'test1'))
browser.div(data-type: /test1/))

使用自定義屬性

您還可以使用自定義屬性來定位元素,如下所示:

html 元素示例

<div itemprop = ”content”>
   ….
</div>

您可以如下所示定位 div:

browser.div(itemprop: ‘content'))
browser.div(itemprop: /content/))

使用 Visible 屬性

可以使用 Visible 屬性定位元素,如下所示:

browser.div(visible: true)
browser.div(visible: false)
廣告