qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Selenium webdriver系列教程(5)—如何定位frame中的元素

           在web ui自動化測試中,frame一直是令人頭痛的問題,就像上班必須擠公車坐地鐵一般,frame的問題總是令人氣悶糾結為之黯然神傷。

            以前在使用watir 1.6x的時候,frame也是頗為棘手的一個問題。不但要照本宣科的進行一系列的設置,而且在進行實際代碼編寫的過程中會遇到各種奇奇怪怪的問題。frame就像中國男足的后防線,問題多多難以解決。

            selenium webdriver處理frame比較簡單,這點比某些測試工具要先進一些,令人身心愉悅。

            以下面的html代碼為例,我們看一下如何定位frame上的元素。

          frame.html
          <html>
          <head>
          <title>Frame</title>
          <style>
          #f_1 {width: 10em; height: 10em; border: 1px solid #ccc; }
          #f_2 {display: none}
          </style>
          </head>
          <body>
          <p id = "p">Outside frame</p>
          <iframe id = "f_1" f1" src = "part1.htm"></iframe>
          <iframe id = "f_2" src = "part2.htm"></iframe>
          </body>
          </html>
          part1.htm
          <html>
          <head><title>Part1</title></head>
          <body>
          <p id = "f_p">This is part 1</p>
          <input id = "btn" type = "button" value = "click me" onclick = "alert('hello')" />
          </body>
          </html>

            switch_to方法會new1個TargetLocator對象,使用該對象的frame方法可以將當前識別的"主體"移動到需要定位的frame上去。

          require 'rubygems'
          require 'selenium-webdriver'
          dr = Selenium::WebDriver.for :firefox
          frame_file = 'file:///'+File.expand_path(File.join(File.dirname(__FILE__), 'frame.html'))
          dr.navigate.to frame_file
          # 定位default content上的p元素
          p dr.find_element(:id => 'p')
          # 將當前識別主體移動到id為f_1的frame上去
          dr.switch_to.frame('f_1')
          # 點擊frame上的button
          dr.find_element(:id =>'btn').click # --> a alert will popup
          # 此時再去定位frame外的p元素將出現錯誤
          p dr.find_element(:id => 'p') # --> error
          # 將識別的主體切換出frame
          dr.switch_to.default_content
          p dr.find_element(:id => 'p') # --> ok

            webdriver的frame處理方式讓人感覺那個不痛越來越輕松,這點進步值得肯定。

            下一節我們將介紹如何定位彈出的新窗口

          相關文章

          Selenium webdriver系列教程(4)—如何定位測試元素

          Selenium webdriver系列教程(6)—如何捕獲彈出窗口

          posted on 2013-09-22 10:18 順其自然EVO 閱讀(777) 評論(0)  編輯  收藏 所屬分類: selenium and watir webdrivers 自動化測試學習

          <2013年9月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 武穴市| 大石桥市| 嘉黎县| 德化县| 嫩江县| 林周县| 如皋市| 聊城市| 行唐县| 金寨县| 九龙县| 安康市| 吴堡县| 喀什市| 嘉兴市| 崇州市| 宁南县| 沈丘县| 资阳市| 孟津县| 抚远县| 镇康县| 栾川县| 宣化县| 玛沁县| 克拉玛依市| 甘谷县| 新宁县| 津南区| 曲阜市| 霍州市| 菏泽市| 平舆县| 前郭尔| 乌拉特中旗| 阜康市| 兰坪| 潜山县| 内乡县| 衡东县| 新宁县|