Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: browser is not defined #10

Closed
bitliner opened this issue Oct 9, 2013 · 10 comments
Closed

Error: browser is not defined #10

bitliner opened this issue Oct 9, 2013 · 10 comments

Comments

@bitliner
Copy link

bitliner commented Oct 9, 2013

The test case throws the following error:

Chrome 29.0.1547 (Linux) Test test 1 FAILED
    ReferenceError: browser is not defined
        at null.<anonymous> (http://localhost:9876/base/test/test_index.html.js?1381331939000:6:9)
Chrome 29.0.1547 (Linux): Executed 1 of 1 (1 FAILED) ERROR (0.254 secs / 0.07 secs)

My karma config file is:

// Karma configuration
// Generated on Wed Oct 09 2013 17:04:44 GMT+0200 (CEST)

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

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


        // frameworks to use
        frameworks:['jasmine'],


        // list of files / patterns to load in the browser
        files:[
            'test/*.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,


        // 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,

        urlRoot:'/__karma/',
        proxies:{
            '/':'http://localhost:3000/'
        },


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


        // 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


    });
};

My test case is:



describe('Test', function(){

    beforeEach(function(){
        browser().navigateTo('/index.html')
    })

    it('test 1', function(){

        console.log('doc',document)
        expect(true).toBe(true)
    })

})

Even if i move browser().navigateTo('/index.html') outside of beforeEach, nothing changes

@bitliner
Copy link
Author

bitliner commented Oct 9, 2013

I solved adding ng-scenario to to karma-config.js

frameworks:['ng-scenario','jasmine'],

@bitliner bitliner closed this as completed Oct 9, 2013
@ponmani
Copy link

ponmani commented Mar 21, 2014

Hello bitliner.I used angular seed project adding extra simple login page. I added frameworks
frameworks:['ng-scenario','jasmine'], of my karma.conf.js. See below my karma.conf.js

login.html




{{message}}

Controller code

$scope.login = function () {
auth.login($scope.username, $scope.password)
.then(function () {
session.username = $scope.username;
$location.path('/index');
}, function (reason) {
$scope.message = reason;
});
};

e2e
.scenarios.js

describe('login', function () {

    beforeEach(function () {
        browser.get('index.html#/login');
    });


    it('should then be.', function () {
        browser().navigateTo('/login');

        var username = input('username').enter('steph');
        console.log(username);
        input('password').enter('steph');
        element('#login').click();

        expect(browser().location().url()).toBe("/index");

        expect(element('#user').text()).toEqual('steph');

    });

});

unit test

controllersSpec.js

it('should login', function () {

    browser().navigateTo('/login');

    input('username').enter('steph');
    input('password').enter('steph');
    element('#login').click();

    expect(browser().location().url()).toBe("/index");

    expect(element('#user').text()).toEqual('steph');
});

my karma.config.js

module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['ng-scenario'],
files: [
'app/lib/angular/angular.js',
'app/lib/angular/angular-.js',
'test/lib/angular/angular-mocks.js',
'app/js/
.js',
'test/unit/*.js'
],

exclude: [
  'app/lib/angular/angular-loader.js',
  'app/lib/angular/*.min.js',
  'app/lib/angular/angular-scenario.js'
],

preprocessors: {

},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false

});
};

@vojtajina
Copy link
Contributor

Guys, do not use "jasmine" and "ng-scenario" together. That won't work. These are two different frameworks with very similar syntax.

If you wanna write unit tests (low level, JS tests, asserting that some objects or functions work as expected), use Karma with Jasmine.

If you wanna write end-to-end test for an AngularJS app (such as, open my app, click on "OK" button, etc), use Protractor. You can also use Karma with "ng-scenario" runner, but it's deprecated.

@ponmani
Copy link

ponmani commented Mar 31, 2014

Hi vojtajina,

I am beginner for karma unit testing. Please help me. You download angular seed project https://github.com/angular/angular-seed. Added Extra login page and controller.The page was username and password textfield. How to write karma unit test. Please send modified angular seed your project. Urgent please.

@WayneYe
Copy link

WayneYe commented Apr 10, 2014

@vojtajina
Hi Vojta, simple question, without specifying "ng-scenario" in karma.conf.js, "browser" would be undefined, if we want to use karma+jasmine, and also, I have a few browser based test cases, what should I do please?

@sylvain-hamel
Copy link

@WayneYe I think your have to use two different karma.conf.js files. One for unit tests and one for 2e2.

@WayneYe
Copy link

WayneYe commented Apr 10, 2014

@sylvain-hamel Thanks for the reply! Buy even if I use two conf file, I still need Jasmine (or mocha, qunit, etc) to do assertions, I googled for hours but didn't find a example with the following things running together:
karma, jasmine 2.0 (done style async test), browser()

@sylvain-hamel
Copy link

@WayneYe I don't know then. I'm still new to the project.

@vojtajina
Copy link
Contributor

Did you read my comment above? (#10 (comment))

If you wanna test your app from a user point of view, do things like "open google.com", "type foo", "click a button", etc... that's what we call end2end test. Check out Protractor https://github.com/angular/protractor

Karma is for unit testing, where you test individual objects, whether they do what they are suppose to do. These tests run in a browser too. There is DOM and all the stuff. These tests are faster, but are not suitable for the entire app tests (stuff like navigating to a page), because Karma just loads a bunch of JavaScript files and runs that code.

@WayneYe
Copy link

WayneYe commented Apr 29, 2014

@vojtajina Thanks for the reply, I missed your last sentence:) I realised it now and already working with protractor. Appreciate your splendid work on Karma, make it better!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants