*** Settings ***
Library Selenium2Library
*** Keywords ***
Checkbox应该不被选择
[Arguments] ${locator}
Checkbox Should Not Be Selected ${locator}
Frame应该包含文本
[Arguments] ${locator} ${text} ${loglevel}=INFO
Frame Should Contain ${locator} ${text} ${loglevel}
List应该无选项
[Arguments] ${locator}
List Should Have No Selections ${locator}
List选项应该是
[Arguments] ${locator} @{items}
List Selection Should Be ${locator} @{items}
Radio按钮不应该被选择
[Arguments] ${group_name}
Radio Button Should Not Be Selected ${group_name}
Radio按钮应该被设置成
[Arguments] ${group_name} ${value}
Radio Button Should Be Set To ${group_name} ${value}
Url应该包含
[Arguments] ${expected}
Location Should Contain ${expected}
Xpath应该匹配X次
[Arguments] ${xpath} ${expected_xpath_count} ${msg}= ${loglevel}=INFO
Xpath Should Match X Times ${xpath} ${expected_xpath_count} ${msg} ${loglevel}
checkbox应该被选择
[Arguments] ${locator}
checkbox should be selected ${locator}
url应该是
[Arguments] ${url}
Location Should Be ${url}
下次确认选择取消
Choose Cancel On Next Confirmation
下次确认选择确定
Choose Ok On Next Confirmation
从List中取消选项
[Arguments] ${locator} @{items}
Unselect From List ${locator} @{items}
从List选择选项
[Arguments] ${locator} @{items}
Select From List ${locator} @{items}
偏移拖拽
[Arguments] ${source} ${xoffset} ${yoffset}
Drag And Drop By Offset ${source} ${xoffset} ${yoffset}
元素应该不包含文本
[Arguments] ${locator} ${expected} ${msg}=
Element Should Not Contain ${locator} ${expected} ${msg}
元素应该不可用
[Arguments] ${locator}
Element Should Be Disabled ${locator}
元素应该不可见
[Arguments] ${locator} ${msg}=
Element Should Not Be Visible ${locator} ${msg}
元素应该包含文本
[Arguments] ${locator} ${text}
Element Should Contain ${locator} ${text}
元素应该匹配x次
[Arguments] ${locator} ${count} ${msg}= ${loglevel}=INFO
Locator Should Match X Times ${locator} ${count} ${msg} ${loglevel}
元素应该可用
[Arguments] ${locator}
Element Should Be Enabled ${locator}
元素应该可见
[Arguments] ${locator}
Element Should Be Visible ${locator}
元素文本应该是
[Arguments] ${locator} ${expected} ${msg}=
Element Text Should Be ${locator} ${expected} ${msg}
全选List元素
[Arguments] ${locator}
Select All From List ${locator}
关闭所有浏览器
Close All Browsers
关闭浏览器
Close Browser
关闭窗口
Close Window
切换浏览器
[Arguments] ${index}
Switch Browser ${index}
创建Webdriver
[Arguments] ${driver_name} ${alias}= ${kwargs}={}
Create Webdriver ${driver_name} ${alias} ${kwargs}
删除Cookie
[Arguments] ${name}
Delete Cookie ${name}
删除所有Cookie
Delete All Cookies
删除本地策略
[Arguments] ${strategy_name}
Remove Location Strategy ${strategy_name}
刷新页面
Reload Page
双击元素
[Arguments] ${locator}
Double Click Element ${locator}
取消选择Checkbox
[Arguments] ${locator}
Unselect Checkbox ${locator}
取消选择Frame
Unselect Frame
回退
Go Back
在图片上按下鼠标左键
[Arguments] ${locator}
Mouse Down On Image ${locator}
在连接上按下鼠标左键
[Arguments] ${locator}
Mouse Down On Link ${locator}
应该出现警告弹窗
[Arguments] ${text}=
Alert Should Be Present ${text}
当前Frame不应该包含
[Arguments] ${text} ${loglvl}=INFO
Current Frame Should Not Contain ${text} ${loglvl}
当前Frame包含
[Arguments] ${text} ${loglevel}=INFO
Current Frame Contains ${text} ${loglevel}
截屏
[Arguments] @{filename}
Capture Page Screenshot @{filename}
打开浏览器
[Arguments] ${url} ${browser}=chrome @{args}
Open Browser ${url} ${browser} @{args}
打开菜单
[Arguments] ${locator}
Open Context Menu ${locator}
执行js
[Arguments] ${code}
Execute Javascript ${code}
执行异步js
[Arguments] ${code}
Execute Async Javascript ${code}
拖拽元素
[Arguments] ${source} ${target}
Drag And Drop ${source} ${target}
按下鼠标左键
[Arguments] ${locator}
Mouse Down ${locator}
按键
[Arguments] ${locator} ${key}
[Documentation] 按键 \ \ text_field \ \ q
...
... 按键 \ \ login_button \ \\13 \ \ #ASCII Code for enter Key
Press Key ${locator} ${key}
提交表单
[Arguments] ${locator}=
Submit Form ${locator}
文本区值应该是
[Arguments] ${locator} ${expected} ${msg}=
Textarea Value Should Be ${locator} ${expected} ${msg}
文本区应该包含
[Arguments] ${locator} ${expected} ${msg}=
Textarea Should Contain ${locator} ${expected} ${msg}
文本框值应该是
[Arguments] ${locator} ${expected} ${msg}=
Textfield Value Should Be ${locator} ${expected} ${msg}
文本框应该包含
[Arguments] ${locator} ${expected} ${msg}=
Textfield Should Contain ${locator} ${expected} ${msg}
最大化
Maximize Browser Window
标题应该是
[Arguments] ${title}
Title Should Be ${title}
根据Index从List中取消选项
[Arguments] ${locator} @{indexes}
Unselect From List By Index ${locator} @{indexes}
根据Index从List选择
[Arguments] ${locator} @{indexes}
Select From List By Index ${locator} @{indexes}
根据Label从List中取消选项
[Arguments] ${locator} @{Labels}
Unselect From List By Label ${locator} @{Labels}
根据Label从List选择
[Arguments] ${locator} @{labels}
Select From List By Label ${locator} @{labels}
根据Value从List中取消选项
[Arguments] ${locator} @{values}
Unselect From List By Value ${locator} @{values}
根据Value从List选择
[Arguments] ${locator} @{values}
Select From List By Value ${locator} @{values}
根据值选择列表元素
[Arguments] ${locator} ${value}
Select From List By Value ${locator} ${value}
模拟
[Arguments] ${locator} ${event}
Simulate ${locator} ${event}
注册失败后运行的关键字
[Arguments] ${kwd}
Register Keyword To Run On Failure ${kwd}
添加cookie
[Arguments] ${name} ${value} ${path}= ${domain}= ${secure}= ${expiry}=
添加本地策略
[Arguments] ${strategy_name} ${strategy_keyword} ${persist}=False
Add Location Strategy ${strategy_name} ${strategy_keyword} ${persist}
清除元素文本
[Arguments] ${locator}
Clear Element Text ${locator}
点击元素
[Arguments] ${path}
Click Element ${path}
点击元素坐标
[Arguments] ${locator} ${xoffset} ${yoffset}
Click Element At Coordinates ${locator} ${xoffset} ${yoffset}
点击图片
[Arguments] ${locator}
Click Image ${locator}
点击按钮
[Arguments] ${path}
Click Button ${path}
点击链接
[Arguments] ${locator}
Click Link ${locator}
睡眠
[Arguments] ${time}
Sleep ${time}
确认
Confirm Action
窗口列表
@{list} List Windows
[Return] @{list}
等待元素不包含文本
[Arguments] ${locator} ${text} ${timeout}= ${error}=
Wait Until Element Does Not Contain ${locator} ${text} ${timeout} ${error}
等待元素不可见
[Arguments] ${locator} ${timeout}= ${error}=
Wait Until Element Is Not Visible ${locator} ${timeout} ${error}
等待元素包含文本
[Arguments] ${locator} ${text} ${timeout}= ${error}=
Wait Until Element Contains ${locator} ${text} ${timeout} ${error}
等待元素可用
[Arguments] ${locator} ${timeout}= ${error}=
Wait Until Element Is Enabled ${locator} ${timeout} ${error}
等待元素可见
[Arguments] ${locator} ${timeout}=30
Wait Until Element Is Visible ${locator} ${timeout}
等待执行成功
[Arguments] ${timeout} ${retry} ${keyword} @{args}
Wait Until Keyword Succeeds ${timeout} minutes ${retry} sec ${keyword} @{args}
等待条件成立
[Arguments] ${condition} ${timeout}= ${error}=
Wait For Condition ${condition} ${timeout} ${error}
等待页面不包含元素
[Arguments] ${locator} ${timeout}= ${error}=
Wait Until Page Does Not Contain Element ${locator} ${timeout} ${error}
等待页面包含元素
[Arguments] ${locator} ${timeout}= ${error}=
Wait Until Page Contains Element ${locator} ${timeout} ${error}
等待页面包含文本
[Arguments] ${text} ${timeout}= ${error}=
Wait Until Page Contains ${text} ${timeout} ${error}
给元素分配ID
[Arguments] ${locator} ${id}
Assign Id To Element ${locator} ${id}
获取Cookie值
[Arguments] ${name}
${value} Get Cookie Value ${name}
[Return] ${value}
获取List元素
[Arguments] ${locator}
@{items} Get List Items ${locator}
获取List多个选定值
[Arguments] ${locator}
@{values} Get Selected List Values ${locator}
[Return] @{values}
获取List多个选定标签
[Arguments] ${locator}
@{labels} Get Selected List Labels ${locator}
[Return] @{labels}
获取List选定值
[Arguments] ${locator}
${value} Get Selected List Value ${locator}
[Return] ${value}
获取List选定标签
[Arguments] ${locator}
${label} Get Selected List Label ${locator}
[Return] ${label}
获取Selenium等待时间
${sec} Get Selenium Implicit Wait
[Return] ${sec}
获取Selenium超时
${timeout} Get Selenium Timeout
[Return] ${timeout}
获取Selenium速率
${res} Get Selenium Speed
[Return] ${res}
获取xpath匹配次数
[Arguments] ${xpath}
${count} Get Matching Xpath Count ${xpath}
[Return] ${count}
获取元素值
[Arguments] ${locator}
${value} Get Value ${locator}
[Return] ${value}
获取元素属性
[Arguments] ${attribute_locator}
Get Element Attribute ${attribute_locator}
获取元素文本
[Arguments] ${locator}
${text} Get Text ${locator}
[Return] ${text}
获取坐标
${location} Get Location
[Return] ${location}
获取垂直坐标
[Arguments] ${locator}
${vertical} Get Vertical Position ${locator}
[Return] ${vertical}
获取当前Url
${url} Log Location
[Return] ${url}
获取当前页面标题
${title} Log Title
[Return] ${title}
获取所有Cookie
@{cookies} Get Cookies
[Return] @{cookies}
获取所有连接
@{links} Get All Links
[Return] @{links}
获取文本
[Arguments] ${path}
${text} get text ${path}
[Return] ${text}
获取标题
${title} get title
[Return] ${title}
获取水平坐标
[Arguments] ${locator}
@{position} Get Horizontal Position ${locator}
[Return] @{position}
获取焦点
[Arguments] ${locator}
Focus ${locator}
获取窗口名字
@{names} Get Window Names
[Return] @{names}
获取窗口坐标
${x} ${y}= Get Window Position
[Return] ${x} ${y}
获取窗口大小
${width} ${higth}= GET Window Size
[Return] ${width} ${higth}
获取窗口标识
@{windows} Get Window Identifiers
[Return] @{windows}
获取窗口标题
@{titles} Get Window Titles
[Return] @{titles}
获取表格内容
[Arguments] ${table_locator} ${row} ${column} ${loglevel}=INFO
${cell} Get Table Cell ${table_locator} ${row} ${column} ${loglevel}
[Return] ${cell}
获取警告信息
${msg} Get Alert Message
[Return] ${msg}
获取资源
${source} Get Source
[Return] ${source}
获取页面元素
[Arguments] ${locator}
@{elements} Get Webelements ${locator}
[Return] @{elements}
获取页面标题
${title} Get Title
[Return] ${title}
获取页面资源
[Arguments] ${loglevel}=INFO
${source} Log Source ${loglevel}
[Return] ${source}
表头应该包含
[Arguments] ${table_locator} ${expected} ${loglevel}=INFO
table header should contain ${table_locator} ${expected} ${loglevel}
表头应该包含文本
[Arguments] ${table_locator} ${expected} ${loglevel}=INFO
Table Header Should Contain ${table_locator} ${expected} ${loglevel}
表应该包含文本
[Arguments] ${table_locator} ${expected} ${loglevel}=INFO
Table Should Contain ${table_locator} ${expected} ${loglevel}
表格Footer应该包含文本
[Arguments] ${table_locator} ${expected} ${loglevel}
Table Footer Should Contain ${table_locator} ${expected} ${loglevel}
表格列应该包含文本
[Arguments] ${table_locator} ${col} ${expected} ${loglevel}=INFO
Table Column Should Contain ${table_locator} ${col} ${expected} ${loglevel}
表格应该包含
[Arguments] ${table_locator} ${row} ${column} ${expected}
table cell should contain ${table_locator} ${row} ${column} ${expected}
表格应该包含文本
[Arguments] ${table_locator} ${row} ${column} ${expected} ${loglevel}=INFO
Table Cell Should Contain ${table_locator} ${row} ${column} ${expected} ${loglevel}
表格行应该包含文本
[Arguments] ${table_locator} ${row} ${expected} ${loglvl}=INFO
Table Row Should Contain ${table_locator} ${row} ${expected} ${loglvl}
设置Selenium等待
[Arguments] ${seconds}
${return} Set Selenium Implicit Wait ${seconds}
[Return] ${return}
设置Selenium超时
[Arguments] ${seconds}
Set Selenium Timeout ${seconds}
设置Selenium速率
[Arguments] ${speed}
Set Selenium Speed ${speed} seconds
设置浏览器等待时间
[Arguments] ${seconds}
Set Browser Implicit Wait ${seconds}
设置窗口坐标
[Arguments] ${x} ${y}
Set Window Position ${x} ${y}
设置窗口大小
[Arguments] ${width} ${higth}
Set Window Size ${width} ${higth}
跳转
[Arguments] ${url}
Go To ${url}
跳转到
[Arguments] ${url}
go to ${url}
输入密码
[Arguments] ${path} ${passwd}
Input password ${path} ${passwd}
输入文本
[Arguments] ${path} ${str}
Input Text ${path} ${str}
选择Checkbox
[Arguments] ${locator}
Select Checkbox ${locator}
选择Frame
[Arguments] ${locator}
Select Frame ${locator}
选择Radio按钮
[Arguments] ${group_name} ${value}
Select Radio Button ${group_name} ${value}
选择文件
[Arguments] ${locator} ${file_path}
Choose File ${locator} ${file_path}
选择窗口
[Arguments] ${locator}=
Select Window ${locator}
页面应该不包含Checkbox
[Arguments] ${locator} ${msg}= ${loglevel}=INFO
Page Should Not Contain Checkbox ${locator} ${msg} ${loglevel}
页面应该不包含Link
[Arguments] ${locator} ${msg}= ${loglevel}=INFO
Page Should Not Contain Link ${locator} ${msg} ${loglevel}
页面应该不包含List
[Arguments] ${locator} ${msg}= ${loglevel}=INFO
Page Should Not Contain List ${locator} ${msg} ${loglevel}
页面应该不包含Radio按钮
[Arguments] ${locator} ${msg}= ${loglevel}=INFO
Page Should Not Contain Radio Button ${locator} ${msg} ${loglevel}
页面应该不包含元素
[Arguments] ${locator} ${msg}= ${loglevel}=INFO
Page Should Not Contain Element ${locator} ${msg} ${loglevel}
页面应该不包含图片
[Arguments] ${locator} ${msg}= ${loglevel}=INFO
Page Should Not Contain Image ${locator} ${msg} ${loglevel}
页面应该不包含按钮
[Arguments] ${locator} ${msg}= ${loglevel}=INFO
Page Should Not Contain Button ${locator} ${msg} ${loglevel}
页面应该不包含文本
[Arguments] ${text} ${lvl}=INFO
Page Should Not Contain ${text} ${lvl}
页面应该不包含文本框
[Arguments] ${locator} ${msg}= ${loglevel}=INFO
Page Should Not Contain Textfield ${locator} ${msg} ${loglevel}
页面应该包含Checkbox
[Arguments] ${locator} ${msg}= ${loglevel}=INFO
Page Should Contain Checkbox ${locator} ${msg} ${loglevel}
页面应该包含Link
[Arguments] ${locator} ${msg}= ${loglevel}=INFO
Page Should Contain Link ${locator} ${msg} ${loglevel}
页面应该包含List
[Arguments] ${locator} ${msg}= ${loglevel}=INFO
Page Should Contain List ${locator} ${msg} ${loglevel}
页面应该包含Radio按钮
[Arguments] ${locator} ${msg}= ${loglevel}=INFO
Page Should Contain Radio Button ${locator} ${msg} ${loglevel}
页面应该包含元素
[Arguments] ${locator} ${msg}= ${loglevel}=INFO
Page Should Contain Element ${locator} ${msg} ${loglevel}
页面应该包含图片
[Arguments] ${locator} ${msg}= ${loglevel}=INFO
Page Should Contain Image ${locator} ${msg} ${loglevel}
页面应该包含按钮
[Arguments] ${locator} ${msg}= ${loglevel}=INFO
Page Should Contain Button ${locator} ${msg} ${loglevel}
页面应该包含文本
[Arguments] ${text} ${loglevel}=INFO
Page Should Contain ${text} ${loglevel}
页面应该包含文本框
[Arguments] ${locator} ${msg}= ${loglevel}=INFO
Page Should Contain Textfield ${locator} ${msg} ${loglevel}
鼠标悬停
[Arguments] ${locator}
Mouse Over ${locator}
鼠标移出
[Arguments] ${locator}
Mouse Out ${locator}
鼠标释放
[Arguments] ${locator}
Mouse Up ${locator}
RFS自动化测试工具安装与使用总结
一,调试
1,在调试时,总时提示“无法打开浏览器”
解决办法:
1,把浏览器的代理关闭
2,把浏览器的显示比例调到100%
3,在IE设置的安全选项中,把启用保护模式开启
4,去除进入系统弹出的登录提示
在浏览器中---工具---internet选项---安全---把只在intranet自动登录,改为匿名登录
二,安装加载库
1,RequestsLibrary
先安装Requests工具,再安装RequestsLibrary
再在robot中进行加载lib,如果加载的库还是红色的,重启就可以了
三,常用关键字
open browser 打开浏览器
input text 在文本框中录入内容
input password 在文本框中录入密码
click button 点击按钮
page should (NOT)contain (link,button,element,checkbox,list) 页面是否出现指定的内容
close browser 关闭浏览器
多个打开浏览器切换
open browser http://127.0.0.1:8000/ ie local
switch browser local
针对页面弹出窗口的确认键
confirm action----类似于点击弹出窗的确认或OK键
alert should be present 当前弹窗的内容 -----判断当前指定内容的弹窗是否存在
get alert message---获取弹出窗的信息
${msg}= get alert message
should be equal as string ${msg} *****
在弹窗中选择取消操作
a,choose cancle on next confirmaction
b,confirm action
设置日志级别
Set Log Level TRACE
下拉列表
select from list xpath value
select from list by value xpath value
select from list by index xpath value
框架
select frame id=fra
unselect frame
复选框
select checkbox id=CheckYes
unselect checkbox id=CheckNo
checkbox should not be selected
select checkbox
checkbox should be selected
单选框
select radio button radio1---单选框名称 A--value)
click element id=registerGenderMale
Oracle数据库
connect to database using custom params(关键字) cx_Oracle 用户名,密码,SID
${row} row count select * from 表1;
等待页面加载
wait until page contains element
sleep
reload page---刷新
注释
Comment
几个关键字的区别
Wait Until Keyword Succeeds
wait until page contains element
SLEEP
element should contain
page should contain element
page should contain
Element Should Not Be Visible
判断的使用
判断条件---数字对比
Run Keyword If ${res} == 0 执行的关键字
... ELSE 执行的关键字2
判断条件---字符对比
Run Keyword If ‘${res}’ == ‘ttttt' 执行的关键字
判断条件---布尔类型对比
Run Keyword If '${res}' == '${true}' 执行的关键字
HTTP接口
create session httpserver http://localhost:8000
${addr} Get request httpserver users/s
should be equal as strings ${addr.status_code} 200
log ${addr.content}
${resp} tojson ${addr.content}
四,常见问题解答
1,定位不到元素
Element locator '//input[@id="registerName"]' did not match any elements.
前端代码
<td>
<div class="c">
<span id="disp_registerName" class="cp edit_hide" style="display: none;"></span>
<input id="registerName" class="pa_ui_text edit_show pa_ui_element_normal" type="text" otitle="车主姓名" value="" name="register.name" style="display: inline;">
</div>
</td>
解决方法:
先从最里面的元素开始一级一级往上找,找到iframe,然后先select frame这个ID就可以定位到里面的元素了。
2,InvalidElementStateException: Message: u'Element must not be hidden, disabled or read-only'
针对根据复选框的状态,解决文本框中默认带出内容时。如果这样写就会报错
select checkbox id=noLicenseFlag
input text id=registerName 湘A*
像这样的组合,后面那句input text id=registerName 湘A*直接删除就可以了。
3,页面操作按钮提示找不到elements
click button id=carInfoSubmitBtn
改为click element id=carInfoSubmitBtn
4,根据录入框内容弹出框框,定位不到元素
采用相对路径的方式定位,用firefox浏览器打开地址,按F12获取元素位置,右击---选择复制XPATH
放在脚本中形式为:xpath=/html/body/div[9]/div[2]/table/tbody/tr[4]/td[4]
6,InvalidElementStateException: Message: u'Element must not be hidden, disabled or read-only'
出现这个现象是因为脚本执行快,而页面的元素还没有加载完成造成的。
等待页面加载
wait until page contains element或sleep
7,页面执行时没有报错,但一直停在那里了
利用Mouse out 关键字,移开鼠标动作。
8,使用close broswer关闭浏览器不起作用
首先安装pywin32-217.win32-py2.7.exe,再安装AutoItLibrary库,在close broswer关键字
下一行,增加删除进线的关键字: process close iexplore.exe
10,查看所有有关robot相关库的版本
pip list
11,通过pip升级库或程序版本
1,python -m pip install --upgrade pip
报错:
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
2,根据提示,执行命令
python -m pip install --upgrade pip
报错:Requirement already up-to-date: pip in c:\python27\lib\site-packages
3,找到c:\python27\lib\site-packages下这个目录pip-8.1.1.dist-info并删除,重新执行
python -m pip install --upgrade pip
4,查看是否更新成功
pip list后显示最新版本
12,利用pip更新库或RIDE或robot
pip install robotframework-ride --upgrade
再用pip list可以看到升级成功的版本号了
13,Run Keyword And return status关键字识别不了
利用pip更新RIDE,robot,selenium2Library
14,ImportError:No module named cx_Oracle
根据连接oracle客户端的版本如:11G,操作系统是32位还是64位
安装对应的cx_Oracle安装包,如:cx_Oracle-5.1.2-11g.win32-py2.7
15,Unable to acquire Oracle environment handle
在oracle客户端的目录中把oci.dll,oraocci11.dll,oraociei11.dll文件复制到C:\Python27\Lib\site-packages目录中
16,日志目录
Log: d:\users\liujun~1\appdata\local\temp\RIDEwzyfth.d\log.html
Report: d:\users\liujun~1\appdata\local\temp\RIDEwzyfth.d\report.html
17,在IE中如果click不起作用,用如下关键字
press key id=kw \\13
18,单选框实例
<p class="checkBox">
<input id="registerGenderMale" class="pa_ui_element_normal" type="radio" value="M" name="register.gender" otitle="车主-性别男">
<label class="radiotxt" hidefocus="hidefoces" for="registerGenderMale">男</label>
<input id="registerGenderFemale" class="pa_ui_element_normal" type="radio" value="F" name="register.gender" otitle="车主-性别女">
<label class="radiotxt" hidefocus="hidefoces" for="registerGenderFemale">女</label>
</p>
select radio button register.gender M
19,下拉列表实例
<div class="pa_ui_dropselect_inner paui-clearfix">
<span class="pa_ui_dropselect_input_Container" style="width: 71px;">
<input class="pa_ui_dropselect_input" type="text" readonly="readonly" otitle="V3版-送单地址-县级市" style="width: 71px; opacity: 1;">
</span>
<span class="pa_ui_dropselect_button" otitle="V3版-送单地址-县级市">
</div>
select from list class="pa_ui_dropselect_inner paui-clearfix" 江北区
20,通过PIP安装Selenium2Library报错
C:\Users\Administrator>pip install Selenium2Library
Collecting Selenium2Library
Could not find a version that satisfies the requir
versions: )
No matching distribution found for Selenium2Library
解决办法:
pip install robotframework-Selenium2Library
就可以了
21,安装IEDriver报错
WebDriverException: Message: 'IEDriverServer.exe' executable needs to be in PATH. Please download fromhttp://selenium-release.storage.googleapis.com/index.html and read up at https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver
解决办法:
下载对应系统版本的IEDriverServer.exe放在IE的安装目录下和Python的根目录下,并设置PATH。
22,滚动鼠标拖动滚动条到最顶部或最底部
execute javascript id.scrollTop其中id为滚动条区域对应的id,用法:
如:execute javascript J_introCon.scrollTop=0是拖到最顶部,execute javascript J_introCon.scrollTop=100000是拖到最底部
23,在CASE中的setup中可以设置运行CASE前,运行SETUP里面的关键字。teardown中可以设置运行CASE后,指定要运行的关键字
24,满足条件结束脚本
exit for loop
如:如果count值为0,则结束CASE
run keyword if ${count}==0 exit for loop
25,执行SQL时报错:DatabaseError:ORA-00911:Invalid character
去掉SQL后面的 ;
26,执行SQL查询
Execute sql String select * from 表名
Robot Framework与Web界面自动化测试学习笔记:简单例子
假设环境已经搭建好了。这里用RIDE( Robot Framework Test Data Editor)工具来编写用例。下面我们对Robot Framework简称rf。
我们先考虑下一个最基本的登录功能的测试用例。
一、自动化测试与人工测试
在开始编写用例之前,我们先来思考下自动化测试和人工测试的区别。对于web页面的人工测试,我们想下,如果去测试,怎么操作呢?不外乎如下的基本动作:
1)打开浏览器
2)输入url (前提web服务器要正常启动运行着)
3)等待页面显示出来
4)用眼睛看页面显示的内容是否与自己想象的一致,如果一致,认为功能正常,否则,会认为程序有问题。
5)通过鼠标、键盘执行相关的操作,通过页面的变化和内容显示继续进行检查功能是否正常。
那么什么是自动化测试呢?其本质就是将人的操作过程(打开浏览器、输入url、鼠标点击、键盘输入等)以及验收标准(在人脑中验收)转换为测试代码。
有了测试代码,就可以让其自动运行了。
二、登录用例设计
一个登录功能,想象下如果是人工测试,那基本的测试过程一般如下:
1)打开浏览器、输入登录url
2)输入用户名、密码(也许还有别的输入项,如验证码,则取决于程序本身),点击登录按钮
3)如果是正确的用户名密码,应该出来相应的页面;如果是错误的,应该出来错误页面或错误提示信息。
那我们看看利用Robot Framework怎么写用例。
三、用例编写
1、成功登陆的用例1
successLogin
open browser http://localhost/nau/login ff
input text id=userid xxx
input password id=password yyy
click button id=loginBtn
Wait Until Element Is Visible id=userid
close browser
下面我们看下上面代码的含义
1)successLogin是用例名,是自己取的
2)后面的语句,每句都是 “关键字+参数(0个或多个)”的格式,其中蓝色的词组就是关键字。这个例子的关键字是rf框架中的内置关键字。
用户可以定义自己的关键字。
open browser http://localhost/nau/login ff
这句代码的含义,其实看上去就能理解。open browser是关键字,表示打开浏览器;http://localhost/nau/login 是第一个参数,是要打开的页面url;ff是第二个参数,代表要用的浏览器类型,其中ff表示是firefox浏览器,gc表示是chrome浏览器,ie表示是Internet Explorer浏览器。这里我们用的是firefox浏览器。
input text id=userid xxx
这句代码,input text 是关键字,表示要在html组件(如文本框)中输入信息, id=userid 是 第一个参数,用于定位用来输入的html组件,这里的id表示通过元素的id来定位,userid就是元素的id值。如果页面中存在一个id为userid的输入框,则就能找到。 xxx是第二个参数,表示要输入的值。想象下,如果是人工操作,就是在界面中找到这个输入框,敲击键盘输入xxxx这几个字符。
看到这里,我们可以把关键字当作一个函数(其实它本质上就是一个函数),后面跟的是函数参数,有的关键字有参数,有的没有。
input password id=password yyy
这句代码,也非常好理解了,就是在密码框中输入密码了。其中input password为关键字。
click button id=loginBtn
这句代码,看上去也明白了,click button 是关键字,表示点击按钮;id=loginBtn是第一个参数,用于定位要点击的按钮,这里也是用id来定位的。
Wait Until Element Is Visible id=userid
前面的几句代码,进行的相关的操作,这句代码就是检查操作结果。如果登录成功,会出现新的页面,并且页面上应该有个元素会显示用户的登录名。
这里的Wait Until Element Is Visible 是关键字,顾名思义,就是等待元素可见; id=userid就是要显示的元素,这里同样是通过id定位。
实际上这个检查是不完善的,这里只是检查了是否有id为userid的元素,但元素的内容呢(正常内容应该是xxx),没有检查。这点我么在下个用例介绍。
close browser
上面是这个用例的最后一个语句。这很好理解了,就是关闭浏览器。close browser是关键字,该关键字没有参数。
执行该用例。如果系统存在用户名为xxx和密码为yyy的用户,则该用例就会成功。
2、成功登陆用例2
上面是一个最基本的用例。上面用例存在一个问题,就是检查用例成功的标准是看希望的html元素是否存在。但很多时候不仅需要判断希望的元素是否存在,还需要判断元素的内容是否符合预期。就上面这个用例,希望 id为userid的元素的内容是xxx。那用例应该如何写呢?
successLogin2
open browser http://localhost/nau/login ff
input text id=userid xxx
input password id=password yyy
click button id=loginBtn
${value} get text id=userid
Should Be Equal ${value} xxx
close browser
和上个用例相比,区别就是检查语句(上面红色的2行)。
其中${value} get text id=userid的含义是 利用关键字 get text 获取id为userid元素的内容放到变量${value}中。
而 Should Be Equal ${value} xxx 语句,Should Be Equal 是关键字,用来断言两个参数的值是否相等,如果不等,则表示失败。
3、失败登录用例
上面是验证成功登录的用例。那应该也有验证登录失败的用例。其思路差不多,就是当登录失败后的页面是啥。通过代码来进行检查。
我们假设登录失败后会出现一个新的页面,页面中有文本 “用户名或密码错误”字样。那么用例就可以这么写。
errorLogin
open browser http://localhost/nau/login ff
input text id=userid xxx
input password id=password yyy123
click button id=loginBtn
Wait Until Page Contains 用户名或密码错误
close browser
相比前面的用例,区别还是验收语句。上面的验收语句一看就明白,Wait Until Page Contains 是关键字,用于检查页面中是否包含预期的信息。
4、自定义关键字
可以看出,上面3个用例,前面的4个语句,区别只是输入的参数 用户名和密码的值区别,我们自然会想到,可以把这4个语句封装成一个关键字,包含两个参数用户名和密码。
关键字定义如下:
login
[Arguments] ${username} ${password}
open browser http://localhost/boot/login ff
input text id=userid ${username}
input password id=password ${password}
click button id=loginBtn
使用该关键字的用例如下
loginSuccessWithKeywords
login xxx yyy
${value} get text id=userid
Should Be Equal ${value} xxx
close browser
loginErrorWithKeywords
login xxx yyy123
Wait Until Page Contains 用户名或密码错误
close browser
5、用例的setup和teardown
每个用例,从整个实现讲。一般包括如下三大部分:
1)执行条件设置部分(setup部分):因为用例的执行,通常需要一些条件。如前面的登录成功用例,需要用户已经存在。
2)用例执行部分:包括该用例进行的操作,对结果的验证
3)清理部分(teardown部分):在自动化测试中,我们希望用例执行后,对当前环境不会有任何破坏。因此一般需要做些清理工作,包括第一部分条件设置对系统造成的影响和用例执行过程中对系统造成的影响。
rf对这也提供了支持,可以把一个用例分成三部分。虽然我们写成一部分,从最终效果上讲没区别。但分开了,会更加符合逻辑,用例也非常清楚。
对于我们这个登录用例,可以看出,每个用例完成的最后一步都要关闭浏览器,这里用的是close browser。
因为各个用例都是公共的,我们可以把这放到用例包的teardown中。
*** Settings ***
Test Teardown close browser
这样每个用例的最后语句close browser都不需要了。
把一些语句放到teardown中,还有一个好处是,无论用例执行部分出现什么问题,最后的teardown都会保证被执行。
三、小结
本篇文章,我们通过一个登录例子来介绍了rf在web界面自动化测试上的基本应用。虽然比较简单,但已经把用例编写最核心的概念和步骤做了介绍。其它功能的用例与之区别在于用例的复杂程度区别(比如准备测试条件、用例执行步骤、结果验证的复杂性、用例执行影响的清理)。