jQuery技术

使用jquery辅助进行测试

字号+ 作者:H5之家 来源:H5之家 2015-12-13 13:17 我要评论( )

Selenium-webdriver系列教程(11)————使用jquery辅助进行测试 Jquery是当下比较流行的1个js框架,通过使用webdriver的execute_script方法,我们可以将jquery库结合到自动化测试中去。 结合jquery进行自动化测试的思想是这样的:首先将jquery的源码读到1个

Selenium-webdriver系列教程(11)————使用jquery辅助进行测试

Jquery是当下比较流行的1个js框架,通过使用webdriver的execute_script方法,我们可以将jquery库结合到自动化测试中去。

结合jquery进行自动化测试的思想是这样的:首先将jquery的源码读到1个string中去,然后使用execute_script执行该string。执行完毕后我们就可以通过execute_script方法来调用jquery库了。

下面的html代码中有一个隐藏的div,当鼠标移动到(mouseover)页面上名为Mouse Over Here的链接时,隐藏的div将会显示出来。

<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的库函数实现了不去触发Mouse Over Here链接而直接显示隐藏div的效果(仅在ruby1.9.2下测试过,ruby1.8x应该都不支持)

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来辅助测试实用性应该不是很强,不过有些时候可以使用jquery方法来获得dom节点的css属性,从而达到简化脚本的目的。

本文源码请点击下面链接下载

jquery

posted @

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 7个有用的jQuery小技巧

    7个有用的jQuery小技巧

    2016-02-26 13:02

  • jQuery制作select双向选择列表

    jQuery制作select双向选择列表

    2016-02-26 11:00

  • 全面详细的jQuery常见开发技巧手册

    全面详细的jQuery常见开发技巧手册

    2016-02-26 10:02

  • 强大的jQuery移动插件Top 10

    强大的jQuery移动插件Top 10

    2016-02-25 09:05

网友点评