「Selenium IDE」- 针对 Web 环境,记录和回放的自动化测试工具

  CREATED BY JENKINSBOT

问题描述

在编写传统的 Selenium 自动化测试脚本时,我们需要借助编程语言并使用 Selenium 类库;

虽然能够完成测试任务,但还是具有一定的工作量:环境搭建、编写测试用例、脚本调试;

解决方案

通过 Selenium IDE 工具,我们能够快速创建 Web 自动化测试任务,并减轻编码带来的工作量;

插件安装

安装 Selenium IDE 的过程大致如下:
1)在 Chrome / Firefox 中,安装 Selenium Plugin 即可;
2)在 Selenium Plugin 中,通过图形化操作来定义自动化任务;

使用方法

创建测试任务

参考 Selenium IDE/Getting Started 文档,以获取其详细使用方法;

首先,在界面中,填写 Playback Base URL 参数,元素选择也将在该页面上进行;

然后,在界面中,添加相关补充及任务。简单示例如下:

Command   | Target                                                     | Value
----------------------------------------------------------------------------------------
open      | https://www.example.com                                    |
execute   | script return document.getElementById("inputBox") !== null | inputBoxExists
if        | ${inputBoxExists} > 0                                      |
send keys | id=inputBox                                                | 123456
end       |                                                            |
click     | css=.table .button                                         |
close     |                                                            |

最后,在界面中,点击 Run current test(播放按钮)按钮,开始执行我们定义的任务;

当开始执行测试任务时,将自动创建新的浏览器窗口,该窗口来执行测试任务。

Q:浏览器将停止在 preparing to run your test 页面;
A:未填写 Playback Base URL 参数;或者通过 open 命令,来打开新页面;

命令终端运行

Selenium IDE 支持将脚本保存到本地,其为 .side 文件;

下载 Command-line Runner 命令,以在命令行运行脚本;

# npm install -g selenium-side-runner

# selenium-side-runner foo.side

常见问题处理

判断元素是否存在

selenium – How to use conditional statements with verifyElementPresent
Selenium IDE if element exists – Lance’s Tech Blog

页面功能比较复杂,我们需要判断元素是否存在,然后执行不同的动作;

我们尝试通过 Verify Element Present 来判断元素是否存在,然后通过 if 语句来控制分支的执行。但是,我们没有找到具体的文档和使用示例,导致程序无法成功运行;

针对该问题,通过 store xpath count 命令,来计算个数,然后在 if 语句中进行检查

...
store xpath count | //*[@id="accessCode"]  | accessCodeCount
if                | ${accessCodeCount} > 0 |
sendKeys          | id=accessCode          | 123456
end               |                        |
...

参考文献

Selenium IDE · Open source record and playback test automation for the web
Command-line Runner · Selenium IDE