1

I'm starting a new project (Firefox add-on) and I'd like to try using behavior-driven development. I particularly like the Jasmine BDD library. However, I can't find a good way how to use a framework such as Jasmine in the Add-On SDK.

One problem is that Jasmine needs setTimeout (and similar) functions to be specified on the global object, whereas Add-On SDK exports those using "timers" module. But let's say I tweak Jasmine to get those object from "timers" (or add the the methods exported by timers to the global object).

The bigger problem is that I don't know how to actually run the tests. There is a test directory generated by the SDK, however, there's no window or document object there to allow me to see the output (and I'd really like to see the fancy HTML output). I guess I could create a content script that would modify the page, but then I can't access (test) the background script.

Have you ever faced this before? Is there any recommended way how to deal with that?

Thanks! Tomas

4

1 回答 1

0

您可以使用Add-on SDK windows API打开一个新窗口来运行您的测试。您应该能够使用下标加载器加载Jasmine 脚本,并将窗口和文档设置为您想要的范围内的任何内容那个下标:

var windows = require("windows").browserWindows;

windows.open({
  url: "about:blank",
  onOpen: function(window) {
    var script;
      var scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
        getService(Ci.mozIJSSubScriptLoader);
      scriptLoader.loadSubScript(subscriptSpec, script);
      script["window"] = window;
      script["document"] = window.document;
      // ... run your tests here by calling script.someFunc() ...
   }
});

更新:进一步的研究表明 browserWindows 实际上是特殊的包装器,它不允许您访问内容窗口。您可以尝试从隐藏的框架中获取窗口/文档。这是我能看到的从特权代码访问 HTML 文档的唯一方法。

于 2011-09-13T17:31:33.253 回答