3

我正在尝试使用 Karma 在 django/angularjs 应用程序上进行一些基本的端到端测试(e2e),但我遇到了一个奇怪的错误。

这是我的测试代码:

# testacular-e2e.conf.js

describe('Log in tests', function() {
   it('should show the disconnected home', function() {
      browser().navigateTo('/#');
      expect(browser().location().url()).toBe('/#');
   });
});

当我运行此代码时,我得到以下输出:
[DEPRECATED ERROR - left for reference only]
$>karma start testacular-e2e.conf.js [2013-04-30 22:19:26.465] [WARN] config - “/”被代理了,你应该更改 urlRoot 以避免冲突 [2013-04-30 22:19:26.467] [DEBUG] 配置 - autoWatch 设置为 false,因为 singleRun INFO [karma]:Karma 服务器从http开始://local.host:9876/ INFO [launcher]: 启动浏览器 PhantomJS INFO [PhantomJS 1.9 (Mac)]: Connected on socket id cwhp0lnTraNa4ToQ0HkS PhantomJS 1.9 (Mac) 登录测试应该显示断开连接的主页 FAILED TypeError: 'undefined'不是函数(评估'$document.injector()')PhantomJS 1.9 (Mac): Executed 1 of 1 (1 FAILED) (0.098 secs / 0.04 secs)

任何人都可以帮忙吗?
提前致谢 :)

编辑:这是我的业力配置文件:

// Karma configuration

// base path, that will be used to resolve files and exclude
basePath = '';

// frameworks to use
frameworks = ['jasmine', 'ng-scenario'];

// list of files / patterns to load in the browser
files = [
  'app/components/angular/angular.js',
  'app/components/angular-complete/angular-resource.js',
  'app/components/angular-complete/angular-cookies.js',
  'app/components/angular-mocks/angular-mocks.js',
  'app/components/ng-translate/translate.js',
  'app/scripts/*.js',
  'app/scripts/services/services.js',
  'app/scripts/**/*.js',
  'test/spec/**/*.js'
];

// list of files to exclude
exclude = [];

// test results reporter to use
// possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
reporters = ['progress'];

// web server port
port = 9876;

// cli runner port
runnerPort = 9100;


// enable / disable colors in the output (reporters and logs)
colors = true;


// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel = LOG_DEBUG;


// enable / disable watching file and executing tests whenever any file changes
autoWatch = true;


// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers = ['PhantomJS'];

// If browser does not capture in given timeout [ms], kill it
captureTimeout = 60000;

// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun = false;

// plugins to load
plugins = [
  'karma-jasmine',
  'karma-phantomjs-launcher',
  'karma-chrome-launcher',
  'karma-ng-scenario',
];

编辑 2:添加 component.json 文件

# component.json
  {
    "name": "myapp",
    "version": "0.0.0",
    "dependencies": {
    "angular": "~1.0.5",
    "json3": "~3.2.4",
    "es5-shim": "~2.0.5",
    "angular-resource": "~1.0.5",
    "angular-cookies": "~1.0.5",
    "angular-complete" : "*",
    "jquery" : "~1.9",
    "bootstrap" : "~2.3.1",
    "bootstrap-js" : "*",
    "jquery.localScroll" : "",
    "jquery.scrollTo" : "1.4.4",
    "ng-translate" : ""
  },
  "devDependencies": {
    "angular-mocks": "~1.0.5",
    "angular-scenario": "~1.0.5"
  }
}

编辑 3:更新了上面的 Karma conf 文件 + 这是我的 e2e conf 文件:

// e2e karma configuration

var fs = require('fs');

// Load from basic karma configuration
eval(fs.readFileSync('karma.conf.js')+'');

// list of files / patterns to load in the browser
files = [
  'app/components/angular/angular.js',
  'app/components/angular-complete/angular-resource.js',
  'app/components/angular-complete/angular-cookies.js',
  'app/components/angular-mocks/angular-mocks.js',
  'app/components/ng-translate/translate.js',
  'app/scripts/*.js',
  'app/scripts/services/services.js',
  'app/scripts/**/*.js',
  'test/e2e/**/*.js'
];


// Proxy the root path to the location of app server
proxies = {
  '/': 'http://localhost:8000/'
};

如果您需要“文件”参数中导入的文件的详细信息,请告诉我。使用他的实际配置,“正常”测试(不是 e2e 测试)工作正常,但 e2e 测试给我以下错误:

$>karma start karma-e2e.conf.js
[2013-05-01 11:32:19.047] [WARN] config - "/" is proxied, you should probably change urlRoot to avoid conflicts
INFO [karma]: Karma v0.9.2 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9 (Mac)]: Connected on socket id HmPYnc9PJUdaGb35pl-K
PhantomJS 1.9 (Mac) Log in tests should show the disconnected home FAILED
ReferenceError: Can't find variable: browser
    at /Users/hugo/Projs/wc/angular/test/e2e/scenarios.js:9
    at /Users/hugo/Projs/wc/angular/node_modules/karma-jasmine/lib/adapter.js:107
    at http://localhost:9876/karma.js:111
    at http://localhost:9876/context.html:68
PhantomJS 1.9 (Mac): Executed 1 of 1 (1 FAILED) (0.082 secs / 0.006 secs)
4

4 回答 4

6

将此添加到您的 e2e 配置文件中:

urlRoot = '/_karma_/';
于 2013-07-11T00:15:26.697 回答
1

所以我正在使用这个确切的配置......然后我意识到茉莉花没有浏览器的概念。一旦我从框架中删除了 jasmine,这个错误就消失了。

当然,我还有一个,所以去调试一下:)

于 2013-05-31T19:31:48.777 回答
1

对于其他为此苦苦挣扎的人:如果您使用 requirejs 并手动引导页面,请确保您的 HTML 文件中仍有 ng-app 指令。这为我修好了。

karma 0.9.5 requirejs 2.1.6 angularjs 1.0.7 karma-ng-scenario 0.0.2 karma-chrome-launcher 0.0.2

此外,这是我在 localhost:8000 上运行的 Web 服务器进行 e2e 测试的配置:

module.exports = function(config) {
    config.set({
        frameworks: ['ng-scenario'],

        files: [
            'test/e2e/**/*.js'
        ],

        basePath: '../',

        autoWatch: true,

        proxies: {
            '/': 'http://localhost:8000/'
        },

        urlRoot: '__karma__',

        browsers: ['Chrome'],

        reporters: ['dots'],

        plugins: [
            'karma-ng-scenario',
            'karma-chrome-launcher'
        ]
    });
};

编辑:我使用我现在工作的配置进行了更多尝试以重现错误。sleep()导航到应用程序根目录后我没有再次发生这种情况。所以这可能也值得检查。

于 2013-07-27T06:35:44.570 回答
0

您使用的是哪个版本的业力?我在运行您的测试时看到了类似的错误:

test/e2e/scenarios.js:9:14: TypeError: Object [object Object] 
has no method 'injector'

所以这不仅仅是你的环境。这看起来类似于:

https://github.com/angular/angular.js/issues/1518

vojtajina 建议通过更新业力来解决这个问题以获得最新的修复。

不过,作为警告,值得注意的是,如果您使用 npm 拉取 karma@canary,您将需要进行一些配置更改,然后才能运行任何东西。

于 2013-04-30T22:31:29.380 回答