前言
SeleniumLibrary 是针对 Robot Framework 开发的 Selenium 库。它也 Robot Framework 下面最流程的库之一。主要用于编写 Web UI 自动化测试。今天我们一起来学习SeleniumLibrary的用法。
安装
pip install --pre --upgrade robotframework-seleniumlibrary
简单使用示例
创建 robot_se.robot 文件。调用 SeleniumLibrary 中所提供的关键字,编写 Web 自动化测试。
*** Settings ***
Library SeleniumLibrary
*** Test Cases ***
Baidu search case
Open Browser http://www.baidu.com chrome
Input text id=kw robot framework
click button id=su
close Browser
代码解析:
第二行 :Library SeleniumLibrary,我们导入了SeleniumLibrary 模块
第五行 :我们使用SeleniumLibrary 关键字Open Browser,启动了浏览器,并打开了指定网址。
第七行 :我们使用SeleniumLibrary 关键字Input text 向百度输入框(id=kw)中输入内容。
第七行 :我们使用SeleniumLibrary 关键字click button,点击了搜索按钮(id=su)
第八行 :我们使用SeleniumLibrary 关键字close Browser 关闭关闭浏览器。
元素定位
SeleniumLibrary提供了两种指定前缀的显式定位器策略。
第一种: strategy:value 这种语法只支持 SeleniumLibrary 3 版本以上,是新的定位写法。
第二种: strategy=value 这种语法是 Robot Framework 通常所使用的命令参数的语法。
SeleniumLibrary 支持的元素方法:
| = Strategy = | = Match based on = | = Example = |
|---|---|---|
| id | Element id. | id:example |
| name | name attribute. | name:example |
| identifier | Either id or name. | identifier:example |
| class | Element class. | class:example |
| tag | Tag name. | tag:div |
| xpath | XPath expression. | xpath://div[@id="example"] |
| css | CSS selector. | css:div#example |
| dom | DOM expression. | dom:document.images[5] |
| link | Exact text a link has. | link:The example |
| partial | link Partial link text. | partial link:he ex |
| sizzle | Sizzle selector provided by jQuery. | sizzle:div.example |
| jquery | Same as the above. | jquery:div.example |
| default | Keyword specific default behavior. | default:example |
分割符号的空格将被忽略, 所以, id : foo, id: foo 和 id:foo 都是等价的。
例如:
| demo | --- |
|---|---|
| Click Element | id:container |
| Click Element | css:div#container h1 |
| Click Element | xpath: //div[@id="container"]//h1 |
如果定位器的开头为 “//” 或 “ (// ” 测被当做 Xpath 定位。换句话说,用 //div 和 xpath://div是等价的。
| demo | --- |
|---|---|
| Click Element | //div[@id="container"] |
| Click Element | (//div)[2] |
除了可以直接操作元素外,也可以通过 Get WebElement 关键字获取元素对象。
| demo | -- | -- |
|---|---|---|
${elem} = | Get WebElement | id=example |
| Click Element | ${elem} |
分层测试
和前面的接口测试一样,我们也可以对测试用例进行封装:
*** Settings ***
Documentation Simple example using SeleniumLibrary.
Library SeleniumLibrary
*** Variables ***
${URL} https://www.baidu.com
${BROWSER} Chrome
*** Test Cases ***
case1
Open Browser ${URL} ${BROWSER}
${title} Baidu Search robot framework
should contain ${title} robot framework_百度搜索
close browser
case2
Open Browser ${URL} ${BROWSER}
${title} Baidu Search selenium
should contain ${title} selenium_百度搜索
close browser
*** Keywords ***
Baidu Search
[Arguments] ${search_key}
Input text id:kw ${search_key}
click button id:su
Evaluate time.sleep(2) time
${title} Get Title
[Return] ${title}
代码解析:
第四行 :*** Variables *** 用于定义公共变量。${URL} 和 ${BROWSER} 为定义的公共变量,
总结
- selenium Web自动化
- SeleniumLibrary 元素定位
- 数据驱动
- 全局变量
本文详细介绍SeleniumLibrary在RobotFramework下的应用,包括安装、基本用法、元素定位策略及示例,展示如何通过SeleniumLibrary实现WebUI自动化测试。

3万+

被折叠的 条评论
为什么被折叠?



