简体   繁体   English

ReferenceError:未定义浏览器 - 使用Karma和Jasmine

[英]ReferenceError: browser is not defined - Using Karma and Jasmine

I am getting ReferenceError: browser is not defined when trying to set up some basic tests with Jasmine and Karma. 我得到了ReferenceError: browser is not defined在尝试使用Jasmine和Karma设置一些基本测试时ReferenceError: browser is not defined

Here is my Karma config file 这是我的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
  })
};

And then I have one test file called loginSpec.js shown here: 然后我有一个名为loginSpec.js测试文件, loginSpec.js所示:

describe('login page', function() {

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

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

    });
});

Whenever I run the test in the Karma tool window I get the following error: 每当我在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)

I don't understand why browser is suddenly not defined as I had tests working before setting up Karma (using WebdriverIO and selenium-standalone). 我不明白为什么浏览器突然没有定义,因为我在设置Karma之前进行了测试(使用WebdriverIO和selenium-standalone)。 These tests were written in the same way and there were no errors regarding browser 这些测试以相同的方式编写,并且没有关于browser错误

I have also researched and found that a lot of other people have had the same issue, but they are having it because of issues with Angular, which i am not using? 我也研究过并发现很多其他人也遇到过同样的问题,但他们因为Angular的问题而得到它,我没有使用它?

Using karma,jasmine there is no handler/object as browser available like WebdriverIO and selenium-standalone. 使用karma,jasmine没有像WebdriverIO和selenium-standalone这样的browser可用的处理程序/对象。

If you are looking to write a unit test case using jasmine to test the route, it can be done via injecting $location service of angular as below: 如果您正在寻找使用茉莉花来测试路线的单元测试用例,可以通过注入角度的$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');

    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 业力中的茉莉花测试:未捕获的ReferenceError:未定义require - jasmine tests in karma: Uncaught ReferenceError: require is not defined 使用Jasmine和Karma测试离子-未捕获的ReferenceError:角度未定义 - Testing Ionic with Jasmine and Karma - Uncaught ReferenceError: angular is not defined ReferenceError:未定义模块 - 使用Angular / Laravel应用程序的Karma / Jasmine配置 - ReferenceError: module is not defined - Karma/Jasmine configuration with Angular/Laravel app Karma:未定义的ReferenceError $未定义 - Karma: uncaught ReferenceError $ is not defined 未定义arma茉莉角 - Karma Jasmine Angular not defined ReferenceError: $ is not defined - 茉莉花 - ReferenceError: $ is not defined - Jasmine ReferenceError:模块未在jasmine中定义 - ReferenceError: module is not defined in jasmine afterAll ReferenceError:模块未定义,nodejs中的Karma / Jasmine测试错误引发了错误 - An error was thrown in afterAll ReferenceError: module is not defined, Karma/Jasmine test error in nodejs Webpack和Karma测试:未捕获的ReferenceError:未使用定义jQuery - Webpack and Karma Testing: Uncaught ReferenceError: jQuery is not defined using 在Karma测试中,未定义ReferenceError:describe - In Karma Testing, ReferenceError: describe is not defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM