如何为chrome扩展编写集成测试?

时间:2016-05-25 07:46:05

标签: javascript node.js google-chrome-extension phantomjs selenium-chromedriver

我已经编写了一个chrome扩展程序,它在开发人员窗口中加载。它与当前加载的网页进行通信,并将所需数据存储在数据库中。

我想使用selenium,phantomjs / chromedriver,mocha和chai在节点中为此扩展编写自动单元测试。 我用硒,氯化物,摩卡和柴写了一个简单的测试。它现在失败了。我已附上以下错误。

它的外观如下: client.js

exports.client = require('/usr/local/lib/node_modules/webdriverjs').remote({
    // Settings
    // Use webdriverjs to create a Selenium Client
    desiredCapabilities: {
        browserName: 'chrome',
        chromeOptions: {
            binary: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
            args: [],
            extensions: ['/Users/Divyanshu/Downloads/STM_UPDATED_20160308051700.crx']
        }
    },
    logLevel: 'silent'
});

test.js

var client = require('./client').client;
var expect = require('chai').expect;

describe('Test stackoverflow.com', function(){
    before(function(done) {
        client.init().url('chrome-extension://bfedbpidmfabedhnemcibiciahnjdnid/index.html', done);
    });

    describe('Check homepage', function(){
        it('should see the correct title', function(done) {
            client.getTitle(function(err, title){
                expect(title).to.have.string(
                    'STM'
                );
                done();
            });
        });

        it('should see the body', function(done) {
            client.getText('#header', function(err, span){
                expect(span).to.have.string(
                    'Browser'
                );
                done();
            })
        });
    });

    after(function(done) {
        client.end();
        done();
    });
});

这就是我运行selenium服务器的方式: java -jar ~/Downloads/selenium-server-standalone-2.53.0.jar -Dwebdriver.chrome.driver=node_modules/chromedriver/bin/chromedriver

这就是我运行测试的方式: mocha test.js -t 10000

但我无法加载扩展程序并收到以下错误: 测试运行的错误输出:

Test stackoverflow.com
[13:08:00]:  COMMAND    POST     {"protocol":"http:","slashes":true,"auth":null,"host":"127.0.0.1:4444","port":"4444","hostname":"127.0.0.1","hash":null,"search":null,"query":null,"pathname":"/wd/hub/session","path":"/wd/hub/session","href":"http://127.0.0.1:4444/wd/hub/session"}
[13:08:00]:  DATA        {"desiredCapabilities":{"browserName":"chrome","version":"","javascriptEnabled":true,"platform":"ANY","chromeOptions":{"binary":"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome","args":[],"extensions":["/Users/Divyanshu/Downloads/STM_UPDATED_20160308051700.crx"]}}}
[13:08:00]:  ERROR  UnknownError    An unknown server-side error occurred while processing the command.
            unknown error: cannot process extension #1
from unknown error: cannot base64 decode
  (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Mac OS X 10.11.4 x86_64) (WARNING: The server did not provide any stacktrace information)

[13:08:00]:  ERROR  Couldn't get a session ID - undefined
    1) "before all" hook

[13:08:00]:  COMMAND    DELETE   {"protocol":"http:","slashes":true,"auth":null,"host":"127.0.0.1:4444","port":"4444","hostname":"127.0.0.1","hash":null,"search":null,"query":null,"pathname":"/wd/hub/session/","path":"/wd/hub/session/","href":"http://127.0.0.1:4444/wd/hub/session/"}
[13:08:00]:  DATA        {}

  0 passing (730ms)
  1 failing

  1) Test stackoverflow.com "before all" hook:
     Uncaught Error: [init()] <= 
An unknown server-side error occurred while processing the command.
      at makeError (/usr/local/lib/node_modules/webdriverjs/lib/utils/makeError.js:9:17)
      at RequestHandler.<anonymous> (/usr/local/lib/node_modules/webdriverjs/lib/utils/RequestHandler.js:170:25)
      at Request.self.callback (/usr/local/lib/node_modules/webdriverjs/node_modules/request/request.js:122:22)
      at Request.<anonymous> (/usr/local/lib/node_modules/webdriverjs/node_modules/request/request.js:1019:14)
      at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/webdriverjs/node_modules/request/request.js:970:12)
      at endReadableNT (_stream_readable.js:913:12)

selenium服务器中的错误输出:Exception: unknown error: cannot process extension #1

我使用fs.readFileSync('/Users/Divyanshu/Downloads/STM_UPDATED_20160308051700.crx', 'base64')对我的crx文件进行编码以克服此错误并收到错误:

Exception: unknown error: cannot parse capability: chromeOptions

然后我尝试加载没有crx文件的扩展名。在'chromeOptions'中的'args'中添加了这个:

args: ['load-extension=/Users/Divyanshu/Downloads/STM_UPDATED_20160308051700']

这有助于我解决错误:cannot process extension #1

但现在我偶然发现了新的错误。我无法加载我的html文件(index.html)。它说“找不到您的文件。它可能已被移动或删除.ERR_FILE_NOT_FOUND。重新加载”。我怎么能克服这个错误?

这是我的manifest.json文件的外观:

{
    "name": "Extension",
    "description": "Tool.",
    "icons": {
        "16":  "icons/icon16.png",
        "48":  "icons/icon48.png",
        "128": "icons/icon128.png"
    },
    "version": "1.1.2",
    "manifest_version": 2,
    "permissions": [
        "webRequest",
        "<all_urls>",
        "history",
        "background",
        "storage",
        "cookies",
        "clipboardWrite",
        "tabs"
    ],
    "options_page": "options.html",
    "background": {
        "scripts": [
            "background-script.js",
            "panes/js/jquery.js"
        ]
    },
    "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
    "devtools_page": "devtools.html",
    "web_accessible_resources": [
        "panes/*"
    ]
}

这是我的devtools.js文件,我从一个html文件加载,我已经指定了devtools.html的位置:

var panelWindow, injectedPanel = false, injectedPage = false,
    panelVisible = false, savedStack = [];

chrome.devtools.panels.create("Extension", "panes/img/logo.jpg",
    "panes/index.html", function(panel) {});

所以,我的扩展程序有多个html文件。我想为所有那些编写单元测试,逐个移过每个文件,比如,首先我必须登录我的扩展(这是login.html),然后我将选择一个客户端(这是selectClient.html)等等。

从devtools.js中可以看到

index.html首先加载,从中开始所有处理并逐个加载其他html文件。

0 个答案:

没有答案