[Watir]Web對象屬性識別

來源:互聯網
上載者:User

我們知道,QTP提供object spy工具,物件程式庫錄製功能,或描述性編程提供給我們快捷識別對象,
一定程度上可以做到指令碼批處理產生。相應的,我在想如此好的工具和技術,為啥watir沒有支援?
也許將來,在自動化同學與watirTeam Dev努力下,可滿足這一需求。

另一個問題拋出,是否為了準備watir指令碼,我們一定要在那麼冗長的代碼,尋找並挑選我們想要操作的對象?
給出我的簡單總結,

1, 類似object spy工具,為微軟為開發人員做的工具ie developer toolbar,可以藉助它通過指標指示
對象,在attribute view中找到相應屬性。

2,類似描述性編程,

在cmd中運行irb,

如: puts 2+2
列印4,可以通過命令實現所見即所得 (WYSIWYG),

如:
require ‘watir’
ie=Watir::IE.start(‘http://localhost:8080’)
ie.show_all_objects

結果:

-----------Objects in  page -------------
text              name=name           id= 

如:ie.text_field(:name, 'name').flash
結果:使操控對象webedit框有黃色閃動效果.

更有意思的是,當我把滑鼠焦點移到某個webedit上,
然後:
irb> puts ie.show_active
text         name=name          
id=          value=             
alt=         src=               
innerText=
=> nil
在這裡,watir將協助我們標識當前active的對象.
對於button和其他對象,也可以通過tab key來實現焦點移動,
再用alt + tab 啟用irb視窗,用相同show active方法實現.

再如:

irb> puts ie.show_active
submit              name=submit_logon  
id=                 value=Login        
alt=                src=               
innerText=
=> nil

irb> ie.text_field(:name, 'name').set(‘Test1’)
=> nil

可以參照watir api對web edit對象進行內容清除,追加等.
另外可以輸出當前IE對象的HTML

irb> puts ie.html
<H1>
<P align=center>Create the First Job </P></H1>
<TABLE cellSpacing=0 cellPadding=3 border=0>
<TBODY>
<TR>
<TD align=middle width="40%">
<FORM id=job name=job action=job method=get><INPUT type=hidden value=22909012 name=session></INPUT>
<TABLE cellSpacing=0 cellPadding=3 border=0>
<TBODY>

或只輸出text :

irb> puts ie.text
Create the First Job
 Please create the first job

 Make it a background job
If you make this job a background job, it will be a job that accumulates time when you're not doing any specific task. You'll start it in the morning,
 do your work - starting and pausing other jobs - then stop it when you're done for the day.
Each time you pause another job, the background job will resume accumulating time.
If you don't have a background job, you'll have to manage time more explicitly.
=> nil
irb(main):024:0>

對於頁面中只有一個同特徵對象時可以用index, 如:
ie.text_field(:index, 1).set(‘Testing for Bugs’)

irb> ie.button(:index,1).click
=> nil

可以借用show_all_objects方法顯示頁面中所有對象
也可以顯示某一特定類型對象, 像:

show_images Show all the images in the document
show_spans Show all the span tags in the document
show_labels Show all the labels in the document
show_links Show all the links in the document
show_divs Show all the div tags in the document
show_frames Show all the frames in the document
show_forms Show all the forms in the document

如這裡用ie.show_table作為樣本
irb> ie.show_tables
Found 8 tables
1  id=      rows=1   columns=3
2  id=      rows=2   columns=1
3  id=      rows=3   columns=1
4  id=      rows=2   columns=1
5  id=      rows=3   columns=1
6  id=      rows=1   columns=1
7  id=      rows=3   columns=1
8  id=recent_records      rows=2   columns=1
=> nil

我們可用flash方法來標識對象,
irb> ie.table(:id , ‘recent_records’).flash

也可以只顯示特定table的html code,

irb> puts ie.table(:id , ‘recent_records’).html

<TABLE id=recent_records cellSpacing=0 cellPadding=3 width="66%" align=center border=1><TBODY>
<TR bgColor=#66ffff>
<TD align=middle colSpan=4>Recent Records </TD></TR>
<TR bgColor=#ccffff>
<TD>Testing for Bugs </TD>
<TD>10:05 PM </TD>
<TD>0.00 hours </TD>
<TD><B>running</B> </TD></TR></TBODY></TABLE>

這裡可用ruby內建函數 to_a輸出行記錄數組,

irb> irb(main):009:0> my_array = ie.table(:id , 'recent_records').to_a
=> [["Recent Records"], ["Testing for Bugs", "10:05 PM",

以上可以看到,採用 show_XXX方法,枚舉同特徵對象,
是否感覺到類似QTP:create descrīption.../ mic class='web object'/...的方法?

類似,甚至可以通過修改底層show_XXX代碼,來批處理產生指令碼,
如產生已經填值的text field操作指令碼,呵呵...

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.