Watir

上一篇 / 下一篇  2012-03-16 16:27:17 / 个人分类:自动化测试

顺序
  • 顺序:
Watir对下列组件不起作用:
ActiveX,Java Applets, Macromedia Flash

判断waitir是否可用:
在页面查看页面源代码,如果可以看到Html源代码,就可以被watir识别。

按钮:
button
方法click

文本框
text_field
方法set

单选框
radio
方法:
set
clear

复选框
checkbox
方法:
set
clear

按钮:
button
方法;
click

下拉框:
select_list
方法:
clear
options
select''

验证页面是否包含特定文本
include?

文本框:
         <INPUT id="email" name="_fmu.u._0.e" value="" />
     方法1: ie.text_field(:id,'email').set("文本内容")
     方法2: ie.text_field(:name, 'email').set("文本内容")
     方法3: ie.text_field(:name,"email").clear

 

下拉框:
         <SELECT name="cert_no"> 
           <OPTION value="身份证">身份证</OPTION>
         </SELECT>
    方法1: ie.select_list(:name,"cert_no").select("身份证") 
    方法2: ie.select_list(:name,"cert_no").clearSelection

 

超链接:
           <a href = "http://www.google.cn/">google</a> 
    方法1:ie.link(:text,"google").click 
    方法2:ie.link(:url,"http://www.google.cn/").click

 

复选框:
          <input type = "checkbox" name = "checkme" value = "1"> 
    方法1: ie.checkbox(:name,"checkme").set 
    方法2: ie.checkbox(:name,"checkme").clear 
    方法3: values = ie.checkbox(:name,"checkme").value
                      <input type = "checkbox" name = "checkme" value = "2"> 
     #多个同名的复选框处理
    方法1: ie.checkbox(:name,"checkme","2").set 
    方法2: ie.checkbox(:name,"checkme","2").clear

 

单选框:
          <input type = "radio" name = "clickme" id = "1"> 
     方法1: ie.radio(:name, "clickme").set 
     方法2: ie.radio(:name, "clickme").clear

 

一般按钮:
          <input type = "button" name = "clickme" value = "Click Me"> 
     方法1: ie.button(:value, "Click Me").click 
     方法2: ie.button(:name,"clickme").click

 

submit按钮:
          <form. action = "submit" name = "submitform" method = "post">
             <input type = "submit" value = "Submit"></input>
          </form> 
    方法: ie.button(:value."Submit").click

 

图片按钮:
          <form. action ="submit" name = "doitform" method = "post">
             <input type = "image" src = "images/doit.gif" name = "doit">
          </form> 
    方法:ie.button(:name, "doit").click

 

Form中无按钮:
          <form. action = "login" name = "loginform" method = "get">
              <input name = "username" type = "text"></input>
          </form> 
    方法1:ie.form(:name,"loginform").submit 
    方法2:ie.form(:action,"login").submit

 

获取隐含对象值:                                                                                
          <INPUT type=hidden value="您的Email" name="field1">                  
     方法:values = ie.hidden(:name,'field1').value                                             
                                                                                                
获取窗口对象:                                                                                  
    方法1: ie2 = Watir::IE.attach(:url,'http://www.google.cn/')   #根据URL获取             
    方法2: ie3 = Watir::IE.attach(:title,'Google')                #根据窗口标题获取
    方法3: ie4 = Watir::IE.attach(:title, /google.cn/)              #正则表达式匹配获取  
                                                                                                
URL编码:                                                                                       
       require 'cgi'                                                                           
       string =  "URL编码"                                                                      
       string = CGI::escape(string)                                                             
       puts string                 # 转换结果: URL%B1%E0%C2%EB                                 
                                                                                                
URL解码:                                                                                       
      require 'cgi'                                                                             
       string =  "URL%BD%E2%C2%EB"                                                              
       string = CGI::unescape(string)                                                           
       puts string                  # 转换结果:URL解码     

 

            

The HTML Elements that are currently supported include:

支持的HTML的元素

button

<input> tags with type=button, submit, image or reset

radio

<input> tags with the type=radio; known as radio buttons

check_box

<input> tags with type=checkbox

text_field

<input> tags with the type=text (single-line), type=textarea (multi-line), and type=password

hidden

<input> tags with type=hidden

select_list

<select> tags, known as drop-downs or drop-down lists

label

<label> tags (including "for" attribute)

span

<span> tags

div

<div> tags

p

<p> (paragraph) tags

link

<a> (anchor) tags

table

<table> tags, including row and cell methods for accessing nested elements.

image

<img> tags

form

<form> tags

frame

frames, including both the <frame> elements and the corresponding pages.

map

<map> tags

area

<area> tags

li

<li> tags



通过以下内容唯一,可找到对应的字段

:id

Used to find an element that has an "id=" attribute. Since each id should be unique, according to the XHTML specification, this is recommended as the most reliable method to find an object. *

:name

Used to find an element that has a "name=" attribute. This is useful for older versions of HTML, but "name" is deprecated in XHTML. *

:value

Used to find a text field with a given default value, or a button with a given caption, or a text field

:text

Used for links, spans, divs and other element that contain text.

:index

Used to find the nth element of the specified type on a page. For example, button(:index, 2) finds the second button. Current versions of WATIR use 1-based indexing, but future versions will use 0-based indexing.

:class

Used for an element that has a "class=" attribute.

:title

Used for an element that has a "title=" attribute.

:xpath

Finds the item using xpath query.

:method

Used only for forms, the method attribute of a form. is either GET or POST.

:action

Used only for form. elements, specifies the URL where the form. is to be submitted.

:href

Used to identify a link by its "href=" attribute.

:src

Used to identify an image by its URL.

*:idand:nameare the quickest of these to process, and so should be used when possible to speed up scripts.




Supported Methods by Element

:id

:name

:value

:text

:caption

:index

:class

:xpath

:title

:method

:action

:href

:src

multiple attribute support?

button

radio

check_box

text_field

hidden

select_list

label

span

div

p

link

table

image

form

frame

map

area

li




TAG:

 

评分:0

我来说两句

Open Toolbar