【Robot Framework 项目实战 02】SeleniumLibrary Web UI 自动化

前言

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 =
idElement id.id:example
namename attribute.name:example
identifierEither id or name.identifier:example
classElement class.class:example
tagTag name.tag:div
xpathXPath expression.xpath://div[@id="example"]
cssCSS selector.css:div#example
domDOM expression.dom:document.images[5]
linkExact text a link has.link:The example
partiallink Partial link text.partial link:he ex
sizzleSizzle selector provided by jQuery.sizzle:div.example
jquerySame as the above.jquery:div.example
defaultKeyword specific default behavior.default:example

分割符号的空格将被忽略, 所以, id : foo, id: fooid:foo 都是等价的。

例如:

demo---
Click Elementid:container
Click Elementcss:div#container h1
Click Elementxpath: //div[@id="container"]//h1

如果定位器的开头为 “//” 或 “ (// ” 测被当做 Xpath 定位。换句话说,用 //div 和 xpath://div是等价的。

demo---
Click Element//div[@id="container"]
Click Element(//div)[2]

除了可以直接操作元素外,也可以通过 Get WebElement 关键字获取元素对象。

demo----
${elem} =Get WebElementid=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 元素定位
  • 数据驱动
  • 全局变量

转载于:https://www.cnblogs.com/Detector/p/10264841.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值