diff options
author | Stas Vilchik <vilchiks@gmail.com> | 2014-07-23 14:01:23 +0200 |
---|---|---|
committer | Stas Vilchik <vilchiks@gmail.com> | 2014-07-23 14:01:23 +0200 |
commit | 3017f1dd753af8baa461cc9901f9d5a737a28946 (patch) | |
tree | db12e5fa32a6df056f90cecb3481c8058029f290 /server | |
parent | 6ec9fec5f6f2f5349a804d9622c05d708fa018d6 (diff) | |
download | sonarqube-3017f1dd753af8baa461cc9901f9d5a737a28946.tar.gz sonarqube-3017f1dd753af8baa461cc9901f9d5a737a28946.zip |
Make the frontend medium tests better
Diffstat (limited to 'server')
4 files changed, 88 insertions, 56 deletions
diff --git a/server/sonar-web/Gruntfile.coffee b/server/sonar-web/Gruntfile.coffee index 845cfb36694..6eb67c1691c 100644 --- a/server/sonar-web/Gruntfile.coffee +++ b/server/sonar-web/Gruntfile.coffee @@ -242,9 +242,8 @@ module.exports = (grunt) -> clean: - options: { + options: force: true - }, css: ['<%= pkg.assets %>css/'] js: ['<%= pkg.assets %>js/'] build: ['<%= pkg.assets %>build/'] @@ -278,7 +277,6 @@ module.exports = (grunt) -> cw: options: test: true - 'no-colors': true src: ['<%= pkg.sources %>js/tests/e2e/tests/component-viewer-spec.js'] diff --git a/server/sonar-web/src/main/js/tests/e2e/lib.js b/server/sonar-web/src/main/js/tests/e2e/lib.js new file mode 100644 index 00000000000..28f75d43c7c --- /dev/null +++ b/server/sonar-web/src/main/js/tests/e2e/lib.js @@ -0,0 +1,61 @@ +var fs = require('fs'); + + +var BASE_URL = 'http://localhost:3000/pages/', + WINDOW_WIDTH = 1200, + WINDOW_HEIGHT = 800; + + +exports.initMessages = function () { + // Dump log messages + casper.on('remote.message', function(message) { + this.echo('Log: '+ message, 'LOG'); + }); + + // Dump uncaught errors + casper.on('page.error', function(msg) { + this.echo('Error: ' + msg, 'ERROR'); + }); +}; + + +exports.changeWorkingDirectory = function (dir) { + // Since Casper has control, the invoked script is deep in the argument stack + var currentFile = require('system').args[4]; + var curFilePath = fs.absolute(currentFile).split(fs.separator); + if (curFilePath.length > 1) { + curFilePath.pop(); // PhantomJS does not have an equivalent path.baseName()-like method + curFilePath.push(dir); + fs.changeWorkingDirectory(curFilePath.join(fs.separator)); + } +}; + + +var mockRequest = function (url, response) { + casper.evaluate(function (url, response) { + jQuery.mockjax({ url: url, responseText: response}); + }, url, response); +}; +exports.mockRequest = mockRequest; + + +exports.mockRequestFromFile = function (url, fileName) { + var response = fs.read(fileName); + mockRequest(url, response); +}; + + +exports.buildUrl = function (urlTail) { + return BASE_URL + urlTail; +}; + + +exports.setDefaultViewport = function () { + casper.viewport(WINDOW_WIDTH, WINDOW_HEIGHT); +}; + + +exports.capture = function (fileName) { + casper.capture(fileName, { top: 0, left: 0, width: WINDOW_WIDTH, height: WINDOW_HEIGHT }); +}; + diff --git a/server/sonar-web/src/main/js/tests/e2e/pages/component-viewer.html b/server/sonar-web/src/main/js/tests/e2e/pages/component-viewer.html index 89a370dee28..4cae4890868 100644 --- a/server/sonar-web/src/main/js/tests/e2e/pages/component-viewer.html +++ b/server/sonar-web/src/main/js/tests/e2e/pages/component-viewer.html @@ -4,10 +4,13 @@ <link href="../css/sonar.css" rel="stylesheet" media="all"> <script src="../js/sonar.js"></script> + <script src="../js/third-party/jquery.mockjax.js"></script> <script> var baseUrl = '..'; var $j = jQuery.noConflict(); window.suppressTranslationWarnings = true; + jQuery.mockjaxSettings.contentType = 'text/json'; + jQuery.mockjaxSettings.responseTime = 250; $j(document).ready(function () { $j('.open-modal').modal(); $j('<scr' + 'ipt data-main="../js/component-viewer/app" src="../js/require.js"></scr' + 'ipt>').appendTo('body'); @@ -25,4 +28,4 @@ </div> </body> -</htmlf +</html> diff --git a/server/sonar-web/src/main/js/tests/e2e/tests/component-viewer-spec.js b/server/sonar-web/src/main/js/tests/e2e/tests/component-viewer-spec.js index cae8ac13b93..6cedb49c60e 100644 --- a/server/sonar-web/src/main/js/tests/e2e/tests/component-viewer-spec.js +++ b/server/sonar-web/src/main/js/tests/e2e/tests/component-viewer-spec.js @@ -1,80 +1,50 @@ -// Dump log messages -casper.on('remote.message', function(message) { - this.echo('Log: '+ message, 'LOG'); -}); - -// Dump uncaught errors -casper.on('page.error', function(msg, trace) { - this.echo('Error: ' + msg, 'ERROR'); -}); - -var fs = require('fs'); -var utils = require('utils'); - -// Since Casper has control, the invoked script is deep in the argument stack -var currentFile = require('system').args[4]; -var curFilePath = fs.absolute(currentFile).split(fs.separator); -if (curFilePath.length > 1) { - curFilePath.pop(); // PhantomJS does not have an equivalent path.baseName()-like method - curFilePath.push('component-viewer-spec'); - fs.changeWorkingDirectory(curFilePath.join(fs.separator)); -} +var lib = require('../lib'); -casper.test.begin('Component Viewer Base Tests', function suite(test) { - var appResponse = fs.read('app.json'), - sourceResponse = fs.read('source.json'); +lib.initMessages(); +lib.changeWorkingDirectory('component-viewer-spec'); - // Register mockjax requests on loading of requirejs - casper.options.onResourceRequested = function(instance, requestData) { - // Here, instance is the running instance of casperjs - if (requestData.url.indexOf('require') >= 0) { - instance.page.includeJs('../js/third-party/jquery.mockjax.js', function () { - // Inject response values to page scope - instance.page.evaluate(function (appResponse, sourceResponse) { - jQuery.mockjaxSettings.contentType = 'text/json'; - jQuery.mockjaxSettings.responseTime = 250; +casper.test.begin('Component Viewer Base Tests', function (test) { - jQuery.mockjax({ url: '../api/l10n/index', responseText: '{}'}); - jQuery.mockjax({ url: '../api/components/app', responseText: appResponse}); - jQuery.mockjax({ url: '../api/sources/show', responseText: sourceResponse}); - }, appResponse, sourceResponse); - }); + casper.options.onResourceRequested = function (instance, requestData) { + if (requestData.url.indexOf('require') >= 0) { + lib.mockRequest('../api/l10n/index', '{}'); + lib.mockRequestFromFile('../api/components/app', 'app.json'); + lib.mockRequestFromFile('../api/sources/show', 'source.json'); } }; - // See API at http://docs.casperjs.org/en/latest/modules/index.html - casper.start('http://localhost:3000/pages/component-viewer.html#component=component', function () { - casper.viewport(1200, 800); + casper.start(lib.buildUrl('component-viewer.html#component=component'), function () { + lib.setDefaultViewport(); }); - casper.wait(1000, function() { + casper.wait(1000, function () { // Check header elements test.assertElementCount('.component-viewer-header', 1); - test.assertSelectorHasText('.component-viewer-header-component-project', 'SonarQube'); - test.assertSelectorHasText('.component-viewer-header-component-project', 'SonarQube :: Batch'); - test.assertSelectorHasText('.component-viewer-header-component-name', + test.assertSelectorContains('.component-viewer-header-component-project', 'SonarQube'); + test.assertSelectorContains('.component-viewer-header-component-project', 'SonarQube :: Batch'); + test.assertSelectorContains('.component-viewer-header-component-name', 'src/main/java/org/sonar/batch/index/Cache.java'); test.assertElementCount('.component-viewer-header-favorite', 1); test.assertElementCount('.component-viewer-header-actions', 1); // Check main measures - test.assertSelectorHasText('.js-header-tab-basic', '379'); - test.assertSelectorHasText('.js-header-tab-issues', 'A'); - test.assertSelectorHasText('.js-header-tab-issues', '3h 30min'); - test.assertSelectorHasText('.js-header-tab-issues', '6'); - test.assertSelectorHasText('.js-header-tab-coverage', '74.3%'); + test.assertSelectorContains('.js-header-tab-basic', '379'); + test.assertSelectorContains('.js-header-tab-issues', 'A'); + test.assertSelectorContains('.js-header-tab-issues', '3h 30min'); + test.assertSelectorContains('.js-header-tab-issues', '6'); + test.assertSelectorContains('.js-header-tab-coverage', '74.3%'); test.assertElementCount('.js-header-tab-scm', 1); // Check source test.assertElementCount('.component-viewer-source .row', 520); - test.assertSelectorHasText('.component-viewer-source', 'public class Cache'); + test.assertSelectorContains('.component-viewer-source', 'public class Cache'); }); - casper.run(function() { + casper.run(function () { test.done(); }); }); |