简体   繁体   English

如何测试使用Jasmine在Chrome扩展程序中打开新标签页?

[英]How to test opening new tab with Jasmine in Chrome extension?

I'm building chrome extension and I'm having some trouble getting tests to work the way I'd like them to. 我正在构建chrome扩展程序,但在使测试按我希望的方式工作时遇到了一些麻烦。 I access this page by simply going to extension url ending tests.html. 我可以通过简单地转到以tests.html结尾的扩展名url来访问此页面。

Let's say I want to test the following very simple scenario: new tab is opened in chrome and it is loaded, this event is detected by my extension, new tab is added to array of tabs within my apps. 假设我要测试以下非常简单的场景:chrome中打开了新标签页并加载了该文件,我的扩展程序检测到了此事件,新标签页已添加到应用程序中的标签页数组中。 I have all my Jasmine tests in a separate extension page. 我所有的Jasmine测试都在单独的扩展页中。

I tried the following things, and they all fail. 我尝试了以下操作,但全部失败。

One, trying to call Jasmine "expect" with some Timeout (needed for loading the page). 一种,尝试通过一些超时将Jasmine称为“期望”(加载页面所需)。

describe("Tab collection", function () {
    it("after opening new tab it should be in app.tabs", function () { 
        chrome.tabs.create({"url":"http://www.google.com"});
        setTimeout(function () {
            expect(app.tabs.length).toBe(1);
            console.log("app.tabs.length after timeout", app.tabs.length);
        },100);
    });

Console.log does not log anything, tests is green, even tough clearly it wasn't run. Console.log不会记录任何内容,测试是绿色的,甚至显然没有运行也很难。

Two, trying to run 'expect' in chrome.tabs.create callback function. 第二,尝试在chrome.tabs.create回调函数中运行“期望”。

    it("after opening new tab it shoule be in app.tabs", function () {
        chrome.tabs.create({"url":"http://www.google.com"}, function () {
            expect(app.tabs.length).toBe(1); //
            console.log("app.tabs.length in callback to chrome.tabs.create",app.tabs.length); 
        })
    });
});

Console.log logs 0, but for some reasons test passes. Console.log记录为0,但由于某些原因测试通过。 Why it passes if app.tabs.length is 0? 如果app.tabs.length为0,为什么会通过?

Third scenario involves running all my tests after opening new chrome tab. 第三种情况涉及打开新的chrome标签后运行我的所有测试。 Something along the lines 沿线的东西

chrome.tabs.open({some_url},function (tab) {
    execJasmine()
    chrome.tabs.remove(tab['id'])
})

this works, but is extremely inconvenient, all my tests now run with this open tab. 这可行,但是非常不方便,现在我所有的测试都使用此打开的选项卡运行。 It doesn't really allow me to test things. 它真的不允许我测试。

Fourth I tried to do this with Jasmine BeforeEach 第四,我尝试使用Jasmine BeforeEach做到这一点

beforeEach(function () {
    chrome.tabs.create(some_url);
}); 

This is closest to being ok, but now the tests fails, app.tabs.length is 0, because the page is not yet loaded when the test suite is called. 这几乎可以,但是现在测试失败,app.tabs.length为0,因为调用测试套件时尚未加载页面。

Has anyone encountered simlar problems? 有没有人遇到过类似的问题? How can I test described scenario? 如何测试描述的场景?

Your test is synchronous. 您的测试是同步的。 You have to declare a second parameter in the callback of it to make your test asynchronous: 您必须声明中的回调第二个参数it能让你的测试异步:

describe("Tab collection", function () {
    it("after opening new tab it should be in app.tabs", function(done) {
        //                                                        ^^^^
        chrome.tabs.create({"url":"http://www.google.com"}, function() {
            expect(app.tabs.length).toBe(1);
            console.log("app.tabs.length after timeout", app.tabs.length);
            done(); // <------
        });
    });
    // ...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM