English 中文(简体)
Watir - Web Elements
  • 时间:2024-03-18 23:09:01

Watir - Web Elements


Previous Page Next Page  

在这一章中,我们将讨论如何在瓦伊开展工作。

    Working with Textboxes

    Working with Combos

    Working with Radio Buttons

    Working with Checkboxes

    Working with Buttons

    Working with Links

    Working with Div’s

Working with Textboxes

Syntax

browser.text_field id:  firstname  // will get the reference of the textbox

这里将设法了解如何与《国际不动产合同》文本箱合作。

考虑页文本框。

<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>
      
      <span id = "spanfirstname">
         Enter First Name : 
         <input type = "text" id = "firstname" name = "firstname" onchange = "wsentered()" />
      </span>
      <br/>
      <br/>
      <span style = "display:none;" id = "displayfirstname"></span>
   </body>
</html>

相应产出如下:

Working with TextBoxes

我们有一个文字箱,当你进入换面活动时,名字就会落到下面。

现在请我们写这部法典,我们将在其中找到文本箱,并输入名称,并焚烧换面活动。

Watir Code

require  watir 
b = Watir::Browser.new :chrome
b.goto( http://localhost/uitesting/textbox.html )
t = b.text_field id:  firstname 
t.exists?
t.set  Riya Kapoor 
t.value
t.fire_event( onchange )

我们正在使用黄色浏览器,并将页数作为http:// localhost/uitesting/text Box.html

使用goto api browser将打开网页,我们将找到文本——有点:第一点。 如果存在,我们将确定Riya Kapoor的价值,并将使用fire_event。 api to fire the onchange activity.

现在,让我们执行守则,显示以下产出:

Working with TextBoxes Run

Working with TextBoxes Run Output

Working with Combos

Syntax

browser.select_pst 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">
         <span>
            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>
         </span>
         <br/>
         <br/>
         
         <span style = "display:none;" id = "displayselectedmonth">
         </span>
   </body>
</html>

Output

Working with Combos

当你从下降中选择一个月时,情况如下。

我们现在用Watir来测试同样的情况。

Watir Code for combo selection

require  watir 
b = Watir::Browser.new :chrome
b.goto( http://localhost/uitesting/combos.html )
t = b.select_pst id:  months 
t.exists?
t.select  September 
t.selected_options
t.fire_event( onchange )

为了与 com公司合作,你需要找到使用b.select_pst api的选定元素,然后是 drop倒。 为了从下降中挑选出价值,你需要使用电灯和你想要的价值。

执行产出如下:

Working with Combos Output

Working with Radio Buttons

Syntax

browser.radio value:  female  
// will get the reference of the radio button with value “female”

在这里,我们将使用一个试验页面,与无线电信 but合作。

<html>
   <head>
      <title>Testing UI using Watir</title>
   </head>
   
   <body>
      <form name = "myform" method = "POST">
         <b>Select Gender?</b>
         <span>
            <br/>
            <input type = "radio" name = "gender" value = "male" checked> Male
            <br/>
            <input type = "radio" name = "gender" value = "female"> Female
            <br/>
         </span>
      </form>
   </body>
   
</html>
Working with Radio Buttons

我们将选择具有价值的女性的无线电台,如Watir法典所示。

require  watir 
b = Watir::Browser.new
b.goto( http://localhost/uitesting/radiobutton.html )
t = b.radio value:  female 
t.exists?
t.set
b.screenshot.save  radiobutton.png 

为了与无线电台合作,我们需要告诉浏览器我们选择的价值,即:b.radio Value:”female

我们还以广播方式照亮并挽救了这些地雷。 页: 1

Working with Radio Buttons Output

Working with Checkboxes

Syntax

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 pke to travel?</b>
         <span>
            <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>
         </span>
      </form>
   </body>
   
</html>
Working with Checkboxes

现在,让我们利用Watir在浏览器中找到检查箱如下:

require  watir 
b = Watir::Browser.new
b.goto( http://localhost/uitesting/checkbox.html )
t = b.checkbox value:  Train 
t.exists?
t.set
b.screenshot.save  checkbox.png 

在浏览器中找到检查箱,使用b.check Box,贵方希望选择。

Working with Checkboxes Output

Working with Buttons

Syntax

browser.button(:name => "btnsubmit").cpck
// 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 wscpck() {
            document.getElementById("buttondisplay").innerHTML = "Button is cpcked";
            document.getElementById("buttondisplay").style.display = "";
         }
      </script>
      
      <form name = "myform" method = "POST">
         <span>
            <br>
            <input type = "button" id = "btnsubmit" name = "btnsubmit"
               value = "submit" oncpck = "wscpck()"/>
            <br>
         </span>
      </form>
      <br/>
      
      <span style = "display:none;" id = "buttondisplay"></span>
   </body>
   
</html>
Working with Buttons

这里是《瓦伊夫法典》,以查找特定网页上的 but子。

require  watir 
b = Watir::Browser.new
b.goto( http://localhost/uitesting/button.html )
b.button(:name => "btnsubmit").cpck
b.screenshot.save  button.png 

这里是shot。 页: 1

Button

Working with Links

Syntax

browser.pnk text:  Cpck Here  
// will get the reference to the a tag with text ‘Cpck Here’

我们将使用以下试验页面测试链接——

<html>
   <head>
      <title>Testing UI using Watir</title>
   </head>
   <body>
      <br/>
      <br/>
      <a href = "https://www.google.com">Cpck Here</a>
      <br/>
   </body>
</html>
Working with Links

用于测试链接的Watir详细情况如下:

require  watir 
b = Watir::Browser.new
b.goto( http://localhost/uitesting/pnks.html )
l = b.pnk text:  Cpck Here 
l.cpck
b.screenshot.save  pnks.png 

Output

Working with Links Output

Working with Links Outputs

Working with Div’s

Syntax

browser.span class:  spantag  
// will get the reference to span with class “spantag”

我们可以测试一页。

<html>
   <head>
      <title>Testing UI using Watir</title>
      <style>
         .spantag {
            color: blue;
            font-size: 25px;
         }
      </style>
   </head>
   
   <body>
      <br/>
      <br/>
      <span class = "spantag"> UI Testing using Watir </span>
      <br/>
   </body>
</html>

Output

Working with Div

The Watir Code to test the span is psted here -

require  watir 
b = Watir::Browser.new
b.goto( http://localhost/uitesting/span.html )
l = b.span class:  spantag 
l.exists?
l.text
b.screenshot.save  spantag.png 

Output

Working with Div Output Advertisements