如何测试Chrome扩展? [英] How to test chrome extensions?
问题描述
是的,现有的框架非常实用。
在最近的过程中,我将所有测试都放在了一个嵌入到应用程序中的测试页面上,但除非物理类型不可用,否则无法访问。
例如,I将可以在 chrome-extension://asdasdasdasdad/unittests.html
下访问页面中的所有测试。测试可以访问 localStorage
等等。对于访问内容脚本,理论上你可以通过测试页面中的嵌入式IFRAME来测试,但是这些更多的是集成级别测试,单元测试将要求您将其从真实网页中抽象出来,以便您不依赖它们,同样可以访问localStorage。
如果您想直接测试页面,可以编排您的扩展以打开新标签(chrome.tab.create({url:someurl})。对于每个新标签,您的内容脚本应该运行您可以使用您的测试框架来检查您的代码是否完成了它应该做的事情。
至于框架, JsUnit 或更新的 Jasmine 应该工作正常。
Is there a good way to do this? I'm writing an extension that interacts with a website as a content script and saves data using localstorage. Are there any tools, frameworks, etc. that I can use to test this behavior? I realize there are some generic tools for testing javascript, but are those sufficiently power to test an extension? Unit testing is most important, but I'm also interested in other types of testing (such as integration testing).
Yes, the existing frameworks are pretty useful..
In the recent past, I have placed all my tests on a "test" page that was embedded in to the application but not reachable unless physically typed.
For instance, I would have all the tests in a page accessible under chrome-extension://asdasdasdasdad/unittests.html
The tests would have access to localStorage
etc. For accessing content scripts, in theory you could test that through embedded IFRAMEs in your test page, however these are more integration level testing, unit tests would require you to abstract that away from real pages so that you don't depend on them, likewise with access to localStorage.
If you want to test pages directly, you can orchestrate your extension to open new tabs (chrome.tab.create({"url" : "someurl"}). For each of the new tabs your content script should run and you can use your testing framework to check that your code has done what it should do.
As for frameworks, JsUnit or the more recent Jasmine should work fine.
这篇关于如何测试Chrome扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!