e2e-тестирование AngularJS
Нужно поставить karma (я использовал версию 0.9.1).
Вот описание установки и официальная документация по использованию — http://karma-runner.github.io/0.8/index.html (видео достаточно сильно устарело, годится только для того, чтобы понять в общих чертах).
Установка:
1 2 3 4 |
npm install -g karma-0.9.1 npm install -g karma-ng-scenario npm install -g karma-jasmine npm install -g karma-requirejs |
Конфиг для karma-0.9.1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
// end-to-end tests config // Karma configuration // base path, that will be used to resolve files and exclude basePath = '../'; frameworks = ['ng-scenario']; // list of files / patterns to load in the browser files = [ // ANGULAR_SCENARIO, // ANGULAR_SCENARIO_ADAPTER, './e2e/*Spec.js' ]; // list of files to exclude exclude = [ ]; // test results reporter to use // possible values: 'dots', 'progress', 'junit' 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_INFO; //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 = ['ChromeCanary']; // 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; proxies = { '/': '<url to app here>' }; urlRoot = '/e2e/'; //plugins to load plugins = [ 'karma-ng-scenario', 'karma-html2js-preprocessor', 'karma-chrome-launcher', 'karma-firefox-launcher'/*, 'karma-junit-reporter'*/ ]; |
Пример теста:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
'use strict'; /* http://docs.angularjs.org/guide/dev_guide.e2e-testing */ describe('Touch App:', function () { var credentials = {login: '', pass: ''}; function urlTo(slug) { return '' + slug; } function authenticate() { browser().navigateTo(urlTo('/auth')); input('User.login').enter(credentials.login); input('User.password').enter(credentials.pass); element('input[type="submit"]').click(); sleep(1); } it('should redirect index.html to index.html#/top', function () { browser().navigateTo(urlTo()); expect(browser().location().url()).toBe('/top'); }); it('should redirect not defined url to index.html#/top', function () { browser().navigateTo(urlTo('some_strange_one_adsfasdfasdf')); expect(browser().location().url()).toBe('/top'); }); describe('Unauthorized user', function () { beforeEach(function () { browser().navigateTo(urlTo('/logout')); }); it('should not see /profile', function () { browser().navigateTo(urlTo('/profile')); expect(browser().location().url()).toBe('/auth'); }); it('should not see /profile/settings', function () { browser().navigateTo(urlTo('/profile/settings')); expect(browser().location().url()).toBe('/auth'); }); }); describe('Authenticated user', function () { beforeEach(function () { browser().navigateTo(urlTo('/logout')); authenticate(); }); describe('Authenticated user', function () { it('should see /profile', function () { browser().navigateTo(urlTo('/profile')); expect(browser().location().url()).toBe('/profile'); }); it('should see /folders', function () { browser().navigateTo(urlTo('/folders')); expect(browser().location().url()).toBe('/folders'); expect(repeater('section.messager').count()).toBeGreaterThan(0); }); }); }); }); |
Настройки PhpStorm:
для karma server
Полезные ссылки:
https://blog.bullgare.com/2013/05/unit-%d1%82%d0%b5%d1%81%d1%82%d0%b8%d1%80%d0%be%d0%b2%d0%b0%d0%bd%d0%b8%d0%b5-angularjs/
https://github.com/vetik32/angular-seed/commit/be9e0dc23cba61618b27912e332455880bec2e48