最算学到 Selenium 了,希望你没被前面的 Robot Framework 基本语法吓退!

SeleniumLibrary 是针对 Robot Framework 开发的 Selenium 库。它也 Robot Framework 下面最流程的库之一。主要用于编写 Web UI 自动化测试。

安装 SeleniumLibrary


项目地址:https://github.com/robotframework/SeleniumLibrary

SeleniumLibrary(推荐使用 pip 命令)安装:

> pip install --pre --upgrade robotframework-seleniumlibrary

Collecting robotframework-seleniumlibrary
  Downloading robotframework_seleniumlibrary-3.0.0b3-py2.py3-none-any.whl (72kB)
    100% |████████████████████████████████| 81kB 44kB/s Requirement already up-to-date: robotframework>=2.8.7 in c:\python36\lib\site-packages (from robotframework-seleniumlibrary) Requirement already up-to-date: selenium>=2.53.6 in c:\python36\lib\site-packages (from robotframework-seleniumlibrary) Installing collected packages: robotframework-seleniumlibrary Successfully installed robotframework-seleniumlibrary-3.0.0b3 

编写第一个例子


创建 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 

代码解析:

*** Settings ***: 用来定义设置部分。

Library SeleniumLibrary: 在 Robot Framework 脚本中导入 SeleniumLibrary 模块

Baidu search case: 创建一条百度搜索的测试用例。

Open Browser: SeleniumLibrary 关键字,用于启动浏览器,并指定打开网址。

Input text: SeleniumLibrary 关键字,用于向输入框中输入内容。id=kw 定位要操作的元素。

click button: SeleniumLibrary 关键字,用于点击元素。id=su 定位要操作元素

close Browser: SeleniumLibrary 关键字,用于关闭浏览器。

运行测试


通过 pybot 命令运行测试。

λ pybot robot_se.robot

==============================================================================
Robot Se :: Simple example using SeleniumLibrary.
==============================================================================
Baidu search case                                                     | PASS |
------------------------------------------------------------------------------
Robot Se :: Simple example using SeleniumLibrary.                     | PASS |
1 critical test, 1 passed, 0 failed 1 test total, 1 passed, 0 failed ============================================================================== Output: D:\rf_test\robotSe\output.xml Log: D:\rf_test\robotSe\log.html Report: D:\rf_test\robotSe\report.html 

运行结果你可以通过生成的 log.html 或 report.html 文件查看。