xpath在watir下的应用

本文介绍如何使用Watir结合XPath进行GUI测试,特别是在页面元素缺乏唯一标识符的情况下。通过示例代码展示了如何针对不同类型的HTML元素进行定位和交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.watir 测试GUI,在某些情况下,页面元素没有ID或name或其它可以让watir 定位的信息,这时用xpath可以解决这种问题.

2.在安装有watir的情况下,直接安装REXML.(下载REXML后,在命令行下进入bin目录,运行ruby install.rb就行.)

3示例:

html代码:

 

<html>
<head>
</head>
<body>
<table>
 <tr>
   <td><img src="1.jpg">First Image</td>
 </tr>
</table>
<table>
 <tr>
   <td><a href="http://www.google.com" target="_blank"><img src="2.jpg">Second Image</td>
 </tr>
</table>
<table>
 <tr>
   <td><img src="3.jpg" onclick="javascript:alert('img 3 click')">Third Image</td>
<td>
</td>
 </tr>
</table>
<select foo="bar">
<option value="1">1</option>
<option value="2">2</option>
</select>
<p><a href="http://www.google.com" target="_blank"><span >tips:</span></p>
</body>
</html>

 

watir代码:

require "watir"
test_site= "G://xpp.html"
ie=Watir::IE.new
ie.goto test_site
ie.cell(:xpath, "//img[@src='1.jpg']/").click()
ie.cell(:xpath,"//select[@foo='bar']").click
ie.cell(:xpath,"//span[contains(text(),'tips:')]").click
ie.element_by_xpath("//span[contains(text(),'tips:')]").click
ie.select_list(:xpath,"//select[@foo='bar']").select("2")
#ie.link(:url,"http://www.google.com").click
#unable to locate object

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值