ruby+watir-webdriver自动化测试入门

温举
2023-12-01

百度搜索(python):

from selenium import webdriver

driver=webdriver.Chrome()
driver.get("http://www.baidu.com")
driver.find_element_by_id("kw").send_keys("cz9025")
driver.find_element_by_id("su").click()
driver.quit()

百度搜索(ruby):

require 'rubygems'
require "watir-webdriver"

b = Watir::Browser.new :chrome
b.goto "http://www.baidu.com"
b.text_field(:id => 'kw').set "cz9025"
b.button(:id => "su").click
sleep 3
b.quit

两者对比,使用方式大同小异。

ruby定位元素:

标签说明
buttoninput tags with type=button, submit, image or reset
radioinput tags with the type=radio; known as radio buttons
check_boxinput tags with type=checkbox
text_fieldinput tags with the type=text (single-line), type=textarea (multi-line), and type=password
hiddeninput tags with type=hidden
selectselect tags, known as drop-downs or drop-down lists
labellabel tags (including “for” attribute)
spanspan tags
divdiv tags
pp (paragraph) tags
linka (anchor) tags
tabletable tags, including row and cell methods for accessing nested elements.
imageimg tags
formform tags
frameframes, including both the frame elements and the corresponding pages.
mapmap tags
areaarea tags
lili tags

部分使用示例:

element

b.element(:id => "su").click

button

b.button(:id => "su").click

text_field

set是设置值,value是取值

b.text_field(:id => 'kw').set "cz9025"

select

b.select(:name => "NR").select "每页显示20条"

ruby+watir-webdriver,支持的浏览器有Firefox、Chrome 、IE等大部分浏览器。

定位:支持所有的HTML元素

定位方式:支持id、name、class、xpath等常用的方式

 类似资料: