【问题标题】:ReferenceError: browser is not defined - Using Karma and JasmineReferenceError: browser is not defined - 使用 Karma 和 Jasmine
【发布时间】:2017-07-29 05:48:56
【问题描述】:

我在尝试使用 Jasmine 和 Karma 设置一些基本测试时收到 ReferenceError: browser is not defined

这是我的 Karma 配置文件

// Karma configuration
// Generated on Wed Mar 08 2017 13:29:09 GMT+0000 (GMT)

module.exports = function(config) {
   config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine'],


    // list of files / patterns to load in the browser
    files: [
        './spec/**/*.js'
    ],


    // list of files to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-    preprocessor
    preprocessors: {
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity
  })
};

然后我有一个名为loginSpec.js 的测试文件显示在这里:

describe('login page', function() {

    beforeEach(function(){
        browser().navigateTo('/');
    });

    it('should have the correct title', function() {
        expect(browser.getTitle()).toEqual('Title');

    });
});

每当我在 Karma 工具窗口中运行测试时,我都会收到以下错误:

ReferenceError: browser is not defined
    at Object.<anonymous> (spec/loginSpec.js:11:9)
ReferenceError: browser is not defined
    at Object.<anonymous> (spec/loginSpec.js:15:16)

我不明白为什么突然没有定义浏览器,因为我在设置 Karma 之前进行了测试(使用 WebdriverIO 和 selenium-standalone)。这些测试以相同的方式编写,没有关于browser的错误

我也研究过,发现很多其他人也有同样的问题,但他们是因为 Angular 的问题而遇到的,而我没有使用它?

【问题讨论】:

    标签: javascript node.js express jasmine karma-jasmine


    【解决方案1】:

    使用 karma,jasmine 没有像 WebdriverIO 和 selenium-standalone 一样的 browser 处理程序/对象。

    如果您希望使用 jasmine 编写单元测试用例来测试路由,可以通过注入 angular 的$location 服务来完成,如下所示:

    describe('login page', function() {
    
        beforeEach(angular.mock.inject((_$rootScope_, _$location_) => {
           _$location_.path('/');
           _$rootScope_.$digest();
        }));
    
        it('should have the correct title', function() {
            expect(browser.getTitle()).toEqual('Title');
    
        });
    });
    

    【讨论】:

    • 我不能使用 $location 的原因是因为我没有在我的应用程序中使用 Angular
    • @CGrant 好的。你能告诉我你想通过在单元测试中导航到“/”来实现/测试什么吗?
    • 我最初试图测试诸如“它应该使用有效凭据登录”和“它不应该使用无效凭据登录”等。但是经过大量研究,我认为我已经学会了我要以错误的方式测试我的应用程序。我应该测试routes 文件夹中的功能,而且似乎这样做我应该使用 mocha (或类似的框架)和 jasmine 来测试我的 node.js 路由。
    猜你喜欢
    • 2019-12-31
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多