如何使用selenium IDE将数据加载/写入新文件?

Jac*_*mes 3 selenium selenium-ide

我正在使用selenium IDE来记录和重放一些测试用例.在此期间,我在变量中存储了一些值.现在我要将这些变量的值加载/写入新文件.我该怎么做?可能吗 ?

小智 5

使用Selenium IDE是不可能的,但您可以使用Selenium RC或Webdriver(Junit或Nunit)进行存储.


tom*_*omm 5

这绝对是可能的。您只需要使用 mozilla (firefox) 插件 API https://developer.mozilla.org/en-US/Add-ons创建一些 javascript 函数,将此函数保存在 selenium 核心扩展文件http://www.seleniumhq.org /docs/08_user_extensions.jsp并将此扩展添加到 Selenium IDE 中。

写入文本文件的示例函数:

    // ==================== File Writer ====================
Selenium.prototype.doWriteData = function(textToSave, location)
{   
    var data = textToSave;
    var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath(location);
    var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
    foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);
    var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].createInstance(Components.interfaces.nsIConverterOutputStream);
    converter.init(foStream, "UTF-8", 0, 0);
    converter.writeString(data);
    converter.close();
}
Run Code Online (Sandbox Code Playgroud)

阅读也很简单,只需在此处添加另一个功能,例如“doReadData”更多信息https://developer.mozilla.org/en-US/Add-ons/Code_snippets/File_I_O

要将此脚本添加到 Selenium-IDE,请按照以下说明操作:

  1. 创建您的用户扩展并将其保存为 user-extensions.js。虽然这个名称在技术上不是必需的,但保持一致是一种很好的做法。
  2. 打开 Firefox 并打开 Selenium-IDE。
  3. 点击工具,选项
  4. 在 Selenium Core Extensions 中,单击 Browse 并找到用户扩展。js 文件。单击确定。
  5. 您的用户扩展尚未加载,您必须关闭并重新启动 Selenium-IDE。
  6. 在您的空测试中,创建一个新命令,您的用户扩展名现在应该是 Commands 下拉列表中的一个选项。

  • @sanjuro8998 在 init 方法中使用使用 0x02 | 0x10 而不是 0x02 | 0x08 | 0x20(init 方法的第二个参数) (2认同)