Selenium 核心扩展 (User-Extensions.js)
要理解扩展,首先让我们了解 selenium IDE 的三大支柱
- 定位器策略:我们如何在 UI 中找到元素。
现在, Selenium IDE 有一个非常成熟的库,其中包含大量的操作、断言/评估器和定位器策略。
但有时我们需要为项目添加更多功能。在这种情况下,我们可以通过添加自定义扩展来扩展此库。这些自定义扩展称为“用户扩展”。
例如,我们需要一个操作,它可以在将文本填充到 Web 元素之前将其转换为大写。您无法在默认操作库中找到此操作。在这种情况下,您可以创建自己的“用户扩展”。在本教程中,我们将学习如何创建用户扩展以将文本转换为大写
创建要求 Selenium 用户扩展
创建用户扩展 Selenium IDE,我们需要知道的基本概念 JavaScript 和 Java 脚本原型对象概念。
要创建用户扩展,您需要创建 Java 脚本方法并将它们添加到 selenium 对象原型和 PageBot 对象原型中。
创新中心 Selenium IDE 识别用户扩展吗?
添加用户扩展后 Selenium 启动时 Selenium IDE,javascript 原型中的所有这些扩展都会被加载,并且 Selenium IDE 通过名称识别它们。
如何创建用户扩展
步骤1)行动– 所有操作都以“do”开头,即如果操作针对大写文本,则其名称将是 执行文本大写。 当我们在中添加此操作方法时 Selenium SDI, Selenium IDE 本身会为该操作创建一个 wait 方法。因此,在这种情况下,当我们创建 执行文本大写 行动, Selenium IDE 将创建相应的等待函数 TextUpperCaseAndWait.它可以接受两个参数
示例:大写文本操作
Selenium.prototype.doTextUpperCase = function(locator, text) { // Here findElement is itself capable to handle all type of locator(xpath,css,name,id,className), We just need to pass the locator text var element = this.page().findElement(locator); // Create the text to type text = text.toUpperCase(); // Replace the element text with the new text this.page().replaceText(element, text); };
步骤 2)评估员/断言- 在 selenium 对象原型中注册的所有评估者都将带有前缀
通过“get”或“is” 例如 getValueFromCompoundTable,isValueFromCompoundTable。它可以接受两个参数,一个用于目标,另一个用于测试用例中的值字段。
对于每个评估器,都会有相应的验证函数,前缀为“verify”、“assert”,以及等待函数前缀为“waitFor”
例如:对于大写文本评估者
Selenium.prototype.assertTextUpperCase = function(locator, text) { // All locator-strategies are automatically handled by "findElement" var element = this.page().findElement(locator); // Create the text to verify text = text.toUpperCase(); // Get the actual element value var actualValue = element.value; // Make sure the actual value matches the expected Assert.matches(expectedValue, actualValue); }; Selenium.prototype.isTextEqual = function(locator, text) { return this.getText(locator).value===text; }; Selenium.prototype.getTextValue = function(locator, text) { return this.getText(locator).value; };
步骤3)定位器策略- 如果我们希望创建自己的函数来定位元素,那么
我们需要使用以“locateElementBy”为前缀的函数来扩展 PageBot 原型。
它将接受两个参数,第一个是定位器字符串,第二个是文档
需要在哪里进行搜索。
例如:大写文本定位器
// The "inDocument" is a document you are searching. PageBot.prototype.locateElementByUpperCase = function(text, inDocument) { // Create the text to search for var expectedValue = text.toUpperCase(); // Loop through all elements, looking for ones that have // a value === our expected value var allElements = inDocument.getElementsByTagName("*"); // This star '*' is a kind of regular expression it will go through every element (in HTML DOM every element surely have a tag name like<body>,<a>,<h1>,<table>,<tr>,<td> etc. ). Here our motive is to find an element which matched with the Upper Case text we have passed so we will search it with all elements and when we get match we will have the correct web element. for (var i = 0; i < allElements.length; i++) { var testElement = allElements[i]; if (testElement.innerHTML && testElement.innerHTML === expectedValue) { return testElement; } } return null; };
如何使用新创建的核心扩展?
- 在MyCAD中点击 软件更新 Selenium IDE
点击选项 -> 选项…
- 您将在命令列表中找到扩展
以下是常用的扩展/插件列表 Selenium IDE
姓名 | 目的 |
---|---|
收藏 | 将测试套件标记为收藏并单击执行 |
Flex Pilot X | 对于基于 Flex 的自动化 |
FlexMonkium | 用于基于 Adobe Flex 的录制和播放 测试 in Selenium IDE |
文件记录 | 用于将日志保存在文件中 |
流量控制 | 控制测试执行流程 |
突出显示元素 | 突出显示 Web 控件 |
隐式等待 | 等待元素一定的时间限制 |
截图失败 | 失败时截屏 |
测试结果 | 已保存 测试用例 一次点击即可查看测试套件的结果 |
您可以从 SeleniumHQ官方网站的下载部分
http://docs.seleniumhq.org/download/
总结
- 有三个部分 Selenium IDE、行动、评估者/断言、定位器策略。
- 创建用户扩展时, Selenium IDE 无法满足当前要求。
- 要创建用户扩展,需要将 javascript 添加到 selenium 的对象原型中。
- 创建扩展后,需要将其添加到 Selenium IDE 并重新启动 IDE。