aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/test/lib.js
blob: f1bec2eb442a2a5301250549d296836ab40ddb81 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * SonarQube, open source software quality management tool.
 * Copyright (C) 2008-2014 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * SonarQube is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * SonarQube is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
/* globals casper: false, exports: false */

var fs = require('fs');

var getPort = function () {
  return casper.cli.options.port;
};

var BASE_URL = 'http://localhost:' + getPort() + '/pages/',
    BASE_ROOT_URL = 'http://localhost:' + getPort(),
    WINDOW_WIDTH = 1200,
    WINDOW_HEIGHT = 800;


exports.initMessages = function () {
  if (casper.cli.options.verbose) {
    // Dump log messages
    casper.removeAllListeners('remote.message');
    casper.on('remote.message', function (message) {
      this.echo('Log: ' + message, 'LOG');
    });

    // Dump uncaught errors
    casper.removeAllListeners('page.error');
    casper.on('page.error', function (msg) {
      this.echo('Error: ' + msg, 'ERROR');
    });
  }
};


exports.changeWorkingDirectory = function (dir) {
  var commandLineArgs = require('system').args;
  // Since Casper has control, the invoked script is deep in the argument stack
  // commandLineArgs = casper/bin/bootstrap.js,--casper-path=.../casperjs,--cli,--test,[file(s) under test],[options]
  var currentFile = commandLineArgs[4];
  var curFilePath = currentFile.split(fs.separator);
  if (curFilePath.length > 1) {
    curFilePath.pop(); // test name
    curFilePath.pop(); // "js" dir
    curFilePath.push('json');
    curFilePath.push(dir);
    casper.log('changing working dir to: ' + curFilePath.join(fs.separator));
    fs.changeWorkingDirectory(curFilePath.join(fs.separator));
  }
};


exports.configureCasper = function () {
  casper.options.waitTimeout = 20000;
};


exports.testName = function () {
  var head = Array.prototype.slice.call(arguments, 0);
  return function () {
    var tail = Array.prototype.slice.call(arguments, 0),
        body = head.concat(tail);
    return body.join(' :: ');
  };
};


var mockRequest = function (url, response, options) {
  return casper.evaluate(function (url, response, options) {
    return jQuery.mockjax(_.extend({ url: url, responseText: response}, options));
  }, url, response, options || {});
};
exports.mockRequest = mockRequest;


exports.mockRequestFromFile = function (url, fileName, options) {
  var response = fs.read(fileName);
  return mockRequest(url, response, options);
};


exports.clearRequestMocks = function () {
  casper.evaluate(function() {
    jQuery.mockjaxClear();
  });
};


exports.clearRequestMock = function (mockId) {
  casper.evaluate(function(mockId) {
    jQuery.mockjaxClear(mockId);
  }, mockId);
};


function patchWithTimestamp(url) {
  var t = Date.now(),
      hashStart = url.indexOf('#'),
      hash =  hashStart !== -1 ? url.substr(hashStart) : '',
      base = hashStart !== -1 ? url.substr(0, hashStart) : url;
  return base + '?' + t + hash;
}


exports.buildUrl = function (urlTail) {
  return patchWithTimestamp(BASE_URL + urlTail);
};


exports.buildRootUrl = function (urlTail) {
  return patchWithTimestamp(BASE_ROOT_URL + urlTail);
};


exports.setDefaultViewport = function () {
  casper.viewport(WINDOW_WIDTH, WINDOW_HEIGHT);
};


exports.capture = function (fileName) {
  if (!fileName) {
    fileName = 'screenshot.png';
  }
  casper.capture(fileName, { top: 0, left: 0, width: WINDOW_WIDTH, height: WINDOW_HEIGHT });
};


exports.waitForElementCount = function (selector, count, callback) {
  return casper.waitFor(function () {
    return casper.evaluate(function (selector, count) {
      return document.querySelectorAll(selector).length === count;
    }, selector, count);
  }, callback);
};


exports.waitWhileElementCount = function (selector, count, callback) {
  return casper.waitFor(function () {
    return casper.evaluate(function (selector, count) {
      return document.querySelectorAll(selector).length !== count;
    }, selector, count);
  }, callback);
};


exports.assertLinkHref = function assertElementCount(selector, href, message) {
  var linkHref = this.casper.evaluate(function(selector) {
      return document.querySelector(selector);
  }, selector);
  return this.assert(elementCount === count, message, {
    type: "assertElementCount",
    standard: f('%d element%s matching selector "%s" found',
        count,
        count > 1 ? 's' : '',
        selector),
    values: {
      selector: selector,
      expected: count,
      obtained: elementCount
    }
  });
};


exports.sendCoverage = function () {
  return casper.evaluate(function () {
    jQuery.ajax({
      type: 'POST',
      url: '/coverage/client',
      data: JSON.stringify(window.__coverage__),
      processData: false,
      contentType: 'application/json; charset=UTF-8',
      async: false
    });
  });
  casper.wait(500);
};