Watir - 使用 iFrame



Watir 提供易於使用的語法來處理 iframe。

語法

browser.iframe(id: 'myiframe') 
// will get the reference of the iframe where we want to input details.

為了瞭解如何處理 iframe 並定位 iframe 中的元素,我們將在本章中使用示例來講解。

示例

main.html

<html>
   <head>
      <title>Testing using Watir</title>
   </head>
   <body>
      <iframe src = "test1.html" id = "myiframe" width = "500" height = "100"></iframe>
   </body>
</html>

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

輸出

Iframe

在以上示例中,輸入表單在 iframe 中進行定義。以下給出的 Watir 程式碼將幫助我們定位此表單並測試該表單:

Watir 程式碼

require 'watir'
b = Watir::Browser.new :chrome
b.goto('https:///uitesting/main.html')
t = b.iframe(id: 'myiframe').text_field
t.set 'Riya Kapoor'
b.screenshot.save 'iframetestbefore.png'
t.fire_event('onchange')
b.screenshot.save 'iframetestafter.png'

以下是此 URL 中定位 iframe 的 Watir 程式碼:

t = b.iframe(id: 'myiframe').text_field

我們使用了 iframe 標籤名稱以及 iframe 的 id,如上所示。

以下為以上程式碼的螢幕截圖:

iframetestbefore.png

Using ID

iframetestafter.png

Using ID Element

廣告