---
title: "Selenium用户指南 - 第九章 用户扩展"
author: "lsjlym"
date: "2012-02-21T08:58:00Z"
tags: []
slug: "selenium"
draft: false
lastmod: "2015-09-08T20:24:30Z"
id: 2241832
url: "https://lsjlym.iteye.com/blog/2241832"
---

&lt;style type="text/css"&gt; &lt;!-- .h1 {font-size:18px; font-weight:bold} .h2 {font-size:16px; font-weight:bold} .key {font-weight:bold} .note {color:gray; font-style:italic} .d1 {text-indent:20px} .d2 {text-indent:40px} .d3 {text-indent:60px} --&gt; &lt;/style&gt;

[目录](http://blog.csdn.net/planisnothing/article/details/7234926)[上一页](http://blog.csdn.net/planisnothing/article/details/7252960)[下一页](http://blog.csdn.net/planisnothing/article/details/7234926)

用户扩展

注释：本节接近完成，但还没有被审核和编辑。

引言

通过增加你自己的动作，断言，和定位器策略扩展Selenium可能是相当简单的。增加Javascript方法到Selenium对象原型和PageBot对象原型。在启动时，Selenium会自动查找在这些原型中的方法，使用名称模式来识别那个是动作，断言和定位器。下面的示例给出了Selenium可以如何用Javascript扩展的指导。

动作

在Selenium原型中，以“do”开始的所有方法被添加做动作。对每个动作foo，也有一个注册的动作fooAndWait 。一个动作可以最多带有两个参数，在测试中被传递做第二和第三列值。例如：增加一个“typeRepeated” 动作到Selenium，键入文本两次到一个文本框。

Selenium.prototype.doTypeRepeated = function(locator, text) {

// 所有的定位器策略由"findElement"自动处理

var element = this.page().findElement(locator);

// 创建要键入的文本

var valueToType = text + text;

//替代元素文本用新文本

this.page().replaceText(element, valueToType);

};

存取器/断言

在Selenium原型中，所有的getFoo和isFoo方法被增加做存取器方法（storeFoo），为每一个存取器都有注册的一个assertFoo，verifyFoo 和waitForFoo。一个断言方法带有最多两个参数，在测试中被传递做第二和第三列值。你也可以定义你自己断言做简单的“assert”方法，也会自动生成 “verify” 和“waitFor” 命令。例如：增加一个valueRepeated断言，用来确保元素只是由提供的文本经重复组成。在测试中，可得到的2个命令是assertValueRepeated 和 verifyValueRepeated。

Selenium.prototype.assertValueRepeated = function(locator, text) {

// 所有的定位器策略由"findElement"自动处理

var element = this.page().findElement(locator);

// 创建要验证的文本

var expectedValue = text + text;

//得到实际的元素

var actualValue = element.value;

// 确信实际的值匹配预期的

Assert.matches(expectedValue, actualValue);

};

原型生成附加的命令

所有的在Selenium原型的getFoo和isFoo方法自动地导致可得到的storeFoo， assertFoo，assertNotFoo，verifyFoo，verifyNotFoo，waitForFoo，waitForNotFoo命令。例如，如果你增加一个getTextLength() 方法，下面的命令将自动可得到：storeTextLength，assertTextLength，assertNotTextLength，verifyTextLength，verifyNotTextLength，waitForTextLength，和waitForNotTextLength命令。

Selenium.prototype.getTextLength = function(locator, text) {  
return this.getText(locator).length;  
};

同样要注意上面描述的assertValueRepeated也可以实现做isValueRepeated，带有额外地好处，同样自动可以得到assertNotValueRepeated，storeValueRepeated，waitForValueRepeated 和waitForNotValueRepeated。

定位器策略

所有的在PageBot原型的locateElementByFoo被增加做定位器策略。一个定位器策略带有2个参数，第一个是定位串（减去前缀），和第二个是被搜索的文档。例如：增加一个“valuerepeated=”定位器，查找第一个元素的value属性等于提供的值的重复。

// "inDocument"是一个你正在搜索的文档

PageBot.prototype.locateElementByValueRepeated = function(text, inDocument) {

// 创建要搜索的文本

var expectedValue = text + text;

// 循环通过所有的元素，查找一个有value === 我们的预期值的

var allElements = inDocument.getElementsByTagName("\*");

for (var i = 0; i &lt; allElements.length; i++) {

var testElement = allElements\[i];

if (testElement.value && testElement.value === expectedValue) {

return testElement;

}

}

return null;

};

在Selenium IDE中只用用户扩展

在Selenium IDE中用户扩展非常容易使用

1、创建你的用户扩展，并存储作user-extensions.js。尽管这个名称在技术上不是必须的，保持一致性是一个良好的实践。

2、打开Firefox，然后打开Selenium IDE

3、在工具栏上，点击Options

4、在Selenium核心扩展（Selenium Core Extensions）点击浏览（Browse），并查找user-extensions. js文件，然后点击OK。

5、你的用户扩展仍然不会被装载，你必须关闭和重新启动Selenium IDE。

6、在你的空白测试中，创建一个新命令，你的用户扩展现在是一个在命令下拉列表中的选项。

在Selenium RC中，使用用户扩展

如果你Google“Selenium RC user-extension”十次，你会找到十个不同的使用这个特征的方法，下面，是官方的Selenium建议的方法。

示例

C#

1、放置你的用户扩展在与你的Selenium服务器相同的目录下。

2、如果你正在使用由Selenium IDE生成的客户端代码，你将需要做出几个小的修改。首先，你会需要在类的作用域创建一个HttpCommandProcessor对象（在SetupTest方法的外面，紧接着私有的private StringBuilder verificationErrors下面）

3、接着，实例化HttpCommandProcessor对象，使用下面的测试步骤。

HttpCommandProcessor proc;

proc = new HttpCommandProcessor("localhost", 4444, "\*iexplore", "http://google.ca/");

4、使用你创建的HttpCommandProcessor实例化DefaultSelenium 对象

selenium = new DefaultSelenium(proc);

5、在你的测试代码中，用HttpCommandProcessor的方法DoCommand() ，执行你的用户扩展。这个方法带有两个参数：一个字符串标识你想要使用的用户扩展方法，以及字符串数组传递做参数。注意你的函数的第一个字母是小写字母，而不用管在你的用户扩展中的大小写。Selenium自动地处理去保证公共Javascript的命名惯例。因为Javascript是大小写敏感的，你的测试将失败，如果你用大写字母开始这个命令。inputParams是你希望传递给Javascript用户扩展的参数数组。在上述情况中，数组只有一个字符串，因为我们的扩展只有一个参数，而更长的数组将映射每个索引到相应的参数。牢记为Selenium IDE设计的用户扩展只能带有两个参数。

string\[] inputParams = {"Hello World"};

proc.DoCommand("alertWrapper", inputParams);

6、使用-userExtensions 参数并传递你的user-extensions.js文件启动测试服务器。

java -jar selenium-server.jar -userExtensions user-extensions.js

© Copyright 2008-2012, Selenium Project. Last updated on Feb 02, 2012.

[目录](http://blog.csdn.net/planisnothing/article/details/7234926)[上一页](http://blog.csdn.net/planisnothing/article/details/7252960)[下一页](http://blog.csdn.net/planisnothing/article/details/7234926)