0

由于Firefox强制我,我正在重写我的扩展使用WebExtension API,即Chrome的扩展API。我想要进行自动化测试。到目前为止,我已经试过这样:如何测试Chrome扩展程序/ Firefox WebExtension代码?

我有一个package.json,使npm将安装depedencies:

{ 
    "name": "extension-api-tests", 
    "version": "0.0.1", 
    "scripts": { 
    "test": "karma start" 
    }, 
    "devDependencies": { 
    "karma": "^1.3.0", 
    "karma-firefox-launcher": "^1.0.0", 
    "karma-mocha": "^1.3.0", 
    "karma-sinon-chrome": "^0.2.0", 
    "mocha": "^3.1.2", 
    "sinon-chrome": "^2.1.2" 
    } 
} 

我有一个karma.conf.js设置一个测试运行:

module.exports = function(config) { 
    config.set({ 
    frameworks: ['mocha', 'sinon-chrome'], 
    files: ['test.js'], 
    reporters: ['dots'], 
    autoWatch: false, 
    browsers: ['Firefox'], 
    singleRun: true, 
    concurrency: Infinity, 
    }); 
}; 

而且我已经得到了基本测试:

describe('my frustration',() => { 
    it('works when it uses no APIs', done => { 
    done(); 
    }); 

    it('should respond to messages!', done => { 
    console.log('message test starting'); 
    chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { 
     console.log('received message'); 
     sendResponse(true); 
    }); 
    chrome.runtime.sendMessage({}, result => { 
     console.log('received response to message'); 
     done(); 
    }); 
    }); 

    it('should open a tab!', done => { 
    console.log('tab test starting'); 
    chrome.tabs.create({ 
     'active': true, 
     'url': 'http://www.example.com/', 
    }, tab => { 
     console.log('created a tab:', tab); 
     done(); 
    }); 
    }); 
}); 

这是当然是减少测试用例。当我运行npm test,我得到(略有删节):

> [email protected] test .../ext-test 
> karma start 

25 07 2017 11:57:10.395:INFO [karma]: Karma v1.7.0 server started at http://0.0.0.0:9876/ 
25 07 2017 11:57:10.397:INFO [launcher]: Launching browser Firefox with unlimited concurrency 
25 07 2017 11:57:10.404:INFO [launcher]: Starting browser Firefox 
25 07 2017 11:57:14.687:INFO [Firefox 54.0.0 (Ubuntu 0.0.0)]: Connected on socket iIjNRRQfzWj68_GNAAAA with id 42440302 
. 
LOG: 'message test starting' 
Firefox 54.0.0 (Ubuntu 0.0.0) my frustration should respond to messages! FAILED 
     Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. 
LOG: 'tab test starting' 
Firefox 54.0.0 (Ubuntu 0.0.0) my frustration should open a tab! FAILED 
     Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. 
Firefox 54.0.0 (Ubuntu 0.0.0): Executed 3 of 3 (2 FAILED) (3.998 secs/4.001 secs) 
npm ERR! Test failed. See above for more details. 

我的测试中,其尝试使用扩展API全部失败。它没有定义而不是(例如)chrome.runtime(但是,如果我从karma.conf.js中删除'sinon-chrome'),所以我相信我已经设置了sinon。但API从不做任何事情,从不工作。我想测试的代码是全部关于通过这些API传递数据(特别是作为消息,穿过铬/内容边界)。

+0

无法评论主要问题,但是当你解决它时,请注意'runtime.sendMessage'不会自Chrome 49以后发送到它自己的页面/框架,并且显然在Firefox的所有版本中。 – wOxxOm

+0

我*猜测* sinon提供的假冒行为不像真正的API,使测试成为可能。 – arantius

+0

确实,sinon文档中的例子与您使用的直接调用完全不同。我不知道sinon,但不能说这是唯一支持的方法。 – wOxxOm

回答

0

根据https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/sendMessage sendMessage返回一个Promise。根据你的日志,在

chrome.runtime.sendMessage({}, result => { 
    console.log('received response to message'); 
    done(); 
}); 

您的成功处理程序不会被调用的时间比运行测试的主线程。你应该尝试睡眠添加到主线程

function sleep (time) { 
    return new Promise((resolve) => setTimeout(resolve, time)); 
} 

像这样

chrome.runtime.sendMessage({}, result => { 
    console.log('received response to message'); 
}); 

sleep(500).then(() => { 
    done(); 
}); 

只要的sendMessage 500毫秒内返回,这应该工作。