sinon-chrome, 使用 node.js 测试 Chrome 扩展.zip

  • d1_759794
    了解作者
  • 232.4KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-28 04:31
    上传日期
sinon-chrome, 使用 node.js 测试 Chrome 扩展 sinonsinon chrome是 单元测试 Chromium 和 Firefox 扩展和应用的helper 工具。 它使用sinon存根模拟所有扩展 api,允许你在 node.js 中运行测试而无需实际浏览器。架构支
sinon-chrome.zip
内容介绍
[![Build Status](https://travis-ci.org/acvetkov/sinon-chrome.svg?branch=master)](https://travis-ci.org/acvetkov/sinon-chrome) [![npm version](https://badge.fury.io/js/sinon-chrome.svg)](https://www.npmjs.com/package/sinon-chrome) # Sinon-chrome Sinon-chrome is helper tool for unit-testing chromium and Firefox extensions and apps. It mocks all extensions api with sinon stubs that allows you to run tests in Node.js without actual browser. # Schema support API mocks are generated using official chromium extensions API (Firefox webextensions) schemas that ensures consistency with real API. Actual schemas are taken from Chrome 53 and Firefox 49. # How it works Sinon-chrome mocks all chrome api, replaced methods by [sinon stubs](http://sinonjs.org/docs/#stubs) with some sugar. Chrome events replaced by classes with same behavior, so you can test your event handlers with manual triggering chrome events. All properties has values from chrome schema files. # Install We recommend use `sinon-chrome` on Node.js platform. ``` npm install sinon-chrome --save-dev ``` But, if you want... You can download `sinon-chrome` bundle from [release](https://github.com/acvetkov/sinon-chrome/releases) page and include it on your page ```html <script src="/path/to/sinon-chrome.min.js"> ``` or ```html <script src="/path/to/sinon-chrome-apps.min.js"> ``` # Usage For mock extensions Api ```js const chrome = require('sinon-chrome'); // or const chrome = require('sinon-chrome/extensions'); ``` For mock apps Api ```js const chrome = require('sinon-chrome/apps'); // stable apps api ``` # Examples Let's write small navigation helper, which use chrome api methods. ```js export const navigationTarget = { NEW_WINDOW: 'new-window', NEW_TAB: 'new-tab', CURRENT_TAB: 'current-tab', }; /** * Navigate user * @param {String} url * @param {String} [target] * @returns {*} */ export function navigate(url, target = navigationTarget.NEW_TAB) { switch (target) { case navigationTarget.NEW_WINDOW: return chrome.windows.create({url: url, focused: true, type: 'normal'}); case navigationTarget.CURRENT_TAB: return chrome.tabs.update({url: url, active: true}); default: return chrome.tabs.create({url: url, active: true}); } } ``` Test it ```js import chrome from '../src'; // from 'sinon-chrome' import {assert} from 'chai'; import {navigate, navigationTarget} from './navigate'; describe('navigate.js', function () { const url = 'http://my-domain.com'; before(function () { global.chrome = chrome; }); it('should navigate to new window', function () { assert.ok(chrome.windows.create.notCalled, 'windows.create should not be called'); navigate(url, navigationTarget.NEW_WINDOW); assert.ok(chrome.windows.create.calledOnce, 'windows.create should be called'); assert.ok( chrome.windows.create.withArgs({url, focused: true, type: 'normal'}).calledOnce, 'windows.create should be called with specified args' ); }); }); ``` You can run this example by command ``` npm run test-navigate ``` More tests in `examples` dir. ## stubs api With original sinon stubs [api](http://sinonjs.org/docs/#stubs) we add `flush` method, which reset stub behavior. Sinon stub has same method `resetBehavior`, but it has some issues. **Example** ```js chrome.cookie.getAll.withArgs({name: 'my_cookie'}).yields([1, 2]); chrome.cookie.getAll.withArgs({}).yields([3, 4]); chrome.cookie.getAll({}, list => console.log(list)); // [3, 4] chrome.cookie.getAll({name: 'my_cookie'}, list => console.log(list)); // [1, 2] chrome.cookie.getAll.flush(); chrome.cookie.getAll({name: 'my_cookie'}, list => console.log(list)); // not called chrome.cookie.getAll({}, list => console.log(list)); // not called ``` ### events Let's write module, which depends on chrome events ```js export default class EventsModule { constructor() { this.observe(); } observe() { chrome.tabs.onUpdated.addListener(tab => this.handleEvent(tab)); } handleEvent(tab) { chrome.runtime.sendMessage(tab.url); } } ``` And test it ```js import chrome from '../src'; // from 'sinon-chrome' import {assert} from 'chai'; import EventsModule from './events'; describe('events.js', function () { before(function () { global.chrome = chrome; this.events = new EventsModule(); }); beforeEach(function () { chrome.runtime.sendMessage.flush(); }); it('should subscribe on chrome.tabs.onUpdated', function () { assert.ok(chrome.tabs.onUpdated.addListener.calledOnce); }); it('should send correct url on tabs updated event', function () { assert.ok(chrome.runtime.sendMessage.notCalled); chrome.tabs.onUpdated.dispatch({url: 'my-url'}); assert.ok(chrome.runtime.sendMessage.calledOnce); assert.ok(chrome.runtime.sendMessage.withArgs('my-url').calledOnce); }); after(function () { chrome.flush(); delete global.chrome; }); }); ``` You can run this test via ``` npm run test-events ``` ### properties You can set property values. `chrome.flush` reset properties to default values (`null` or specified by schema). Let's create module, which wraps chrome api with Promise. If `chrome.runtime.lastError` is set, promise will be rejected. ```js export const api = { tabs: { /** * Wrapper for chrome.tabs.query * @param {Object} criteria * @returns {Promise} */ query(criteria) { return new Promise((resolve, reject) => { chrome.tabs.query(criteria, tabs => { if (chrome.runtime.lastError) { reject(chrome.runtime.lastError); } else { resolve(tabs); } }); }); } } }; ``` And our tests ```js import chrome from '../src'; // from 'sinon-chrome' import chai from 'chai'; import {api} from './then-chrome'; import chaiAsPromised from 'chai-as-promised'; chai.use(chaiAsPromised); const assert = chai.assert; describe('then-chrome.js', function () { before(function () { global.chrome = chrome; }); beforeEach(function () { chrome.flush(); }); it('should reject promise', function () { chrome.tabs.query.yields([1, 2]); chrome.runtime.lastError = {message: 'Error'}; return assert.isRejected(api.tabs.query({})); }); it('should resolve promise', function () { chrome.runtime.lastError = null; chrome.tabs.query.yields([1, 2]); return assert.eventually.deepEqual(api.tabs.query({}), [1, 2]); }); after(function () { chrome.flush(); delete global.chrome; }); }); ``` You can run this test via ``` npm run test-then ``` # Plugins Sinon chrome module supports plugins, that emulates browser behavior. More info on [example page](https://github.com/acvetkov/sinon-chrome/wiki/Cookie-plugin). - [Cookie plugin](https://github.com/acvetkov/sinon-chrome/wiki/Cookie-plugin) - [i18n plugin](https://github.com/acvetkov/sinon-chrome/tree/master/src/plugins/i18n) ```js const chrome = require('sinon-chrome/extensions'); const CookiePlugin = require('sinon-chrome/plugins').CookiePlugin; chrome.registerPlugin(new CookiePlugin()); ``` ## Extension namespaces * [chrome.alarms](https://developer.chrome.com/extensions/alarms) * [chrome.bookmarks](https://developer.chrome.com/extensions/bookmarks) * [chrome.browserAction](https://developer.chrome.com/extensions/browserAction) * [chrome.browsingData](https://developer.chrome.com/extensions/browsingData) * [chrome.commands](https://developer.chrome.com/extensions/commands) * [chrome.contentSettings](https://developer.chrome.com/extensions/contentSettings) * [chrome.contextMenus](https://deve
评论
    相关推荐
    • LabVIEW单元测试架构工具包(英文版).rar
      LabVIEW单元测试架构工具包(英文版)rar,使用LabVIEW软件设计工具,可以保证高品质以及稳定应用。单元测试架构可以运行自动软件测试,复原测试以及最后确认。为了更好的下载和评估NI LabVIEW单元测试架构,您需先购买...
    • sqrt:测试软件基础架构工具和思想的平台
      SQRT 用Fortran编写的基本平方根计算程序,它为评估各种软件基础结构工具和思想提供了基础。 此处演示的方法: CMake的编译器指令 这可以简单地用类似的方法完成 ...我选择在此处评估和实施的单元测试框架是NASA Godd
    • 分层架构单元测试
      分层架构下的单元测试,使用Mock的机制剥离依赖关系
    • gtest单元测试框架
      自己根据需求写的一个c++单元测试框架,写这个基础是阅读了gtest源码和许多架构,和为了方便大家的需要,写的一个通用性框架。如果大家需要,可以修改里面的相关代码,实现自己的独特需求。
    • Flex持续集成之单元测试
      Flex持续集成之单元测试
    • 单元测试架构工具包与执行跟踪工具包.zip
      单元测试架构工具包与执行跟踪工具包zip,本讲主要为您介绍单元测试架构工具包与执行跟踪工具包的具体使用。欲了解更多LabVIEW相关信息,请点击ni.com/LabVIEW 分类:在线视频
    • MVP架构开发中应用GooglTest进行单元测试的Demo
      MVP架构开发中应用GooglTest进行单元测试的Demo 在UI开发中,经常使用MVP(MVC)模式进行界面和业务的逻辑解耦 MVP模式的一个优势是方便对M层和P层进行单元测试 在本Demo中提供了对MVP模式的P层进行单元测试的使用...
    • TDD单元测试
      TDD 测试
    • 单元测试架构工具包与执行跟踪工具包后续资源包.zip
      欢迎下载LabVIEW网络讲坛之单元测试架构工具包与执行跟踪工具包后续资源包,内含此次视频的相关例程以及演讲讲义. 欲了解
    • cppunit单元测试
      cppunit进行单元测试框架构建与gtest的单元测试框架构建