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

广告
© . All rights reserved.