您的位置:軟件測(cè)試 > 開源軟件測(cè)試 > 開源功能測(cè)試工具 > Selenium
Selenium教程之使用jquery輔助進(jìn)行測(cè)試
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2016/12/23 15:46:29 ] 推薦標(biāo)簽:Selenium 功能測(cè)試

  Jquery是當(dāng)下比較流行的1個(gè)js框架,通過使用webdriver的execute_script方法,我們可以將jquery庫(kù)結(jié)合到自動(dòng)化測(cè)試中去。
  結(jié)合jquery進(jìn)行自動(dòng)化測(cè)試的思想是這樣的:首先將jquery的源碼讀到1個(gè)string中去,然后使用execute_script執(zhí)行該string。執(zhí)行完畢后我們可以通過execute_script方法來調(diào)用jquery庫(kù)了。
  下面的html代碼中有一個(gè)隱藏的div,當(dāng)鼠標(biāo)移動(dòng)到(mouseover)頁(yè)面上名為Mouse Over Here的鏈接時(shí),隱藏的div將會(huì)顯示出來。
  <html>
  <head>
  <title>FireEvent</title>
  <style>
  .mo {color: blue;}
  .tips {display:none;border: 1px solid #ccc; background-color:#EFEFEF;width: 100px;height:100px}
  </style>
  <script>
  function show_tips(){
  document.getElementById("t").style.display = "block";
  }
  function hide_tips(){
  document.getElementById("t").style.display = "none";
  }
  </script>
  </head>
  <body>
  <a class = "mo" href = "#" onmouseover = "show_tips()" onmouseout = "hide_tips()">Mouse Over Here</a>
  <div id = "t" class = "tips">This is the tips of link</div>
  </body>
  </html>
  下面的代碼使用jquery的庫(kù)函數(shù)實(shí)現(xiàn)了不去觸發(fā)Mouse Over Here鏈接而直接顯示隱藏div的效果(僅在ruby1.9.2下測(cè)試過,ruby1.8x應(yīng)該都不支持)
  jquery_helper.rb
  #encoding: utf-8
  module JqueryHelper
  def load_jquery dr,jquery_path
  jq = read_jquery(jquery_path)
  jq.force_encoding('utf-8')
  dr.execute_script jq
  end
  def read_jquery(jquery_path)
  js = ''
  File.open(File.expand_path(jquery_path), 'r') do |f|
  js = f.read
  end
  js
  end
  end
  fire_event.rb
  require 'rubygems'
  require 'selenium-webdriver'
  require './jquery_helper'
  include JqueryHelper
  dr = Selenium::WebDriver.for :firefox
  select_file = 'file:///'.concat File.expand_path(File.join(File.dirname(__FILE__), 'fire_event.html'))
  dr.navigate.to select_file
  jquery_path = './jquery-1.6.4.min.js'
  load_jquery dr, jquery_path
  jq = <<JQ
  $("#t").show();
  JQ
  dr.execute_script jq
  使用jquery來輔助測(cè)試實(shí)用性應(yīng)該不是很強(qiáng),不過有些時(shí)候可以使用jquery方法來獲得dom節(jié)點(diǎn)的css屬性,從而達(dá)到簡(jiǎn)化腳本的目的。

軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd