2
* Copyright 2004 ThoughtWorks, Inc
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
18
// An object representing the current test, used external
19
var currentTest = null; // TODO: get rid of this global, which mirrors the htmlTestRunner.currentTest
23
var HtmlTestRunner = classCreate();
24
objectExtend(HtmlTestRunner.prototype, {
25
initialize: function() {
26
this.metrics = new Metrics();
27
this.controlPanel = new HtmlTestRunnerControlPanel();
28
this.testFailed = false;
29
this.currentTest = null;
30
this.runAllTests = false;
31
this.appWindow = null;
32
// we use a timeout here to make sure the LOG has loaded first, so we can see _every_ error
33
setTimeout(fnBind(function() {
34
this.loadSuiteFrame();
38
getTestSuite: function() {
39
return suiteFrame.getCurrentTestSuite();
42
markFailed: function() {
43
this.testFailed = true;
44
this.getTestSuite().markFailed();
47
loadSuiteFrame: function() {
48
if (selenium == null) {
49
var appWindow = this._getApplicationWindow();
50
try { appWindow.location; }
52
// when reloading, we may be pointing at an old window (Perm Denied)
53
setTimeout(fnBind(function() {
54
this.loadSuiteFrame();
58
selenium = Selenium.createForWindow(appWindow);
59
this._registerCommandHandlers();
61
this.controlPanel.setHighlightOption();
62
var testSuiteName = this.controlPanel.getTestSuiteName();
65
suiteFrame.load(testSuiteName, function() {setTimeout(fnBind(self._onloadTestSuite, self), 50)} );
66
selenium.browserbot.baseUrl = absolutify(testSuiteName, window.location.href);
68
// DGF or should we use the old default?
69
// selenium.browserbot.baseUrl = window.location.href;
70
if (this.controlPanel.getBaseUrl()) {
71
selenium.browserbot.baseUrl = this.controlPanel.getBaseUrl();
75
_getApplicationWindow: function () {
76
if (this.controlPanel.isMultiWindowMode()) {
77
return this._getSeparateApplicationWindow();
79
return $('myiframe').contentWindow;
82
_getSeparateApplicationWindow: function () {
83
if (this.appWindow == null) {
84
this.appWindow = openSeparateApplicationWindow('TestRunner-splash.html', this.controlPanel.isAutomatedRun());
86
return this.appWindow;
89
_onloadTestSuite:function () {
90
if (! this.getTestSuite().isAvailable()) {
93
if (this.controlPanel.isAutomatedRun()) {
94
this.startTestSuite();
95
} else if (this.controlPanel.getAutoUrl()) {
96
//todo what is the autourl doing, left to check it out
97
addLoadListener(this._getApplicationWindow(), fnBind(this._startSingleTest, this));
98
this._getApplicationWindow().src = this.controlPanel.getAutoUrl();
100
this.getTestSuite().getSuiteRows()[0].loadTestCase();
104
_startSingleTest:function () {
105
removeLoadListener(getApplicationWindow(), fnBind(this._startSingleTest, this));
106
var singleTestName = this.controlPanel.getSingleTestName();
107
testFrame.load(singleTestName, fnBind(this.startTest, this));
110
_registerCommandHandlers: function () {
111
this.commandFactory = new CommandHandlerFactory();
112
this.commandFactory.registerAll(selenium);
115
startTestSuite: function() {
116
this.controlPanel.reset();
117
this.metrics.resetMetrics();
118
this.getTestSuite().reset();
119
this.runAllTests = true;
123
runNextTest: function () {
124
this.getTestSuite().updateSuiteWithResultOfPreviousTest();
125
if (!this.runAllTests) {
128
this.getTestSuite().runNextTestInSuite();
131
startTest: function () {
132
this.controlPanel.reset();
133
testFrame.scrollToTop();
134
//todo: move testFailed and storedVars to TestCase
135
this.testFailed = false;
136
storedVars = new Object();
137
this.currentTest = new HtmlRunnerTestLoop(testFrame.getCurrentTestCase(), this.metrics, this.commandFactory);
138
currentTest = this.currentTest;
139
this.currentTest.start();
142
runSingleTest:function() {
143
this.runAllTests = false;
144
this.metrics.resetMetrics();
151
/** SeleniumFrame encapsulates an iframe element */
152
var SeleniumFrame = classCreate();
153
objectExtend(SeleniumFrame.prototype, {
155
initialize : function(frame) {
157
addLoadListener(this.frame, fnBind(this._handleLoad, this));
160
getDocument : function() {
161
return this.frame.contentWindow.document;
164
_handleLoad: function() {
165
this._attachStylesheet();
167
if (this.loadCallback) {
169
this.loadCallback = null;
173
_attachStylesheet: function() {
174
var d = this.getDocument();
175
var head = d.getElementsByTagName('head').item(0);
176
var styleLink = d.createElement("link");
177
styleLink.rel = "stylesheet";
178
styleLink.type = "text/css";
179
if (browserVersion && browserVersion.isChrome) {
180
// DGF We have to play a clever trick to get the right absolute path.
181
// This trick works on most browsers, (not IE), but is only needed in
183
var tempLink = window.document.createElement("link");
184
tempLink.href = "selenium-test.css"; // this will become an absolute href
185
styleLink.href = tempLink.href;
187
// this works in every browser (except Firefox in chrome mode)
188
var styleSheetPath = window.location.pathname.replace(/[^\/\\]+$/, "selenium-test.css");
189
if (browserVersion.isIE && window.location.protocol == "file:") {
190
styleSheetPath = "file://" + styleSheetPath;
192
styleLink.href = styleSheetPath;
194
head.appendChild(styleLink);
197
_onLoad: function() {
200
scrollToTop : function() {
201
this.frame.contentWindow.scrollTo(0, 0);
204
_setLocation: function(location) {
205
var isChrome = browserVersion.isChrome || false;
206
var isHTA = browserVersion.isHTA || false;
207
// DGF TODO multiWindow
208
location += "?thisIsChrome=" + isChrome + "&thisIsHTA=" + isHTA;
209
if (browserVersion.isSafari) {
210
// safari doesn't reload the page when the location equals to current location.
211
// hence, set the location to blank so that the page will reload automatically.
212
this.frame.src = "about:blank";
213
this.frame.src = location;
215
this.frame.contentWindow.location.replace(location);
219
load: function(/* url, [callback] */) {
220
if (arguments.length > 1) {
221
this.loadCallback = arguments[1];
224
this._setLocation(arguments[0]);
229
/** HtmlTestSuiteFrame - encapsulates the suite iframe element */
230
var HtmlTestSuiteFrame = classCreate();
231
objectExtend(HtmlTestSuiteFrame.prototype, SeleniumFrame.prototype);
232
objectExtend(HtmlTestSuiteFrame.prototype, {
234
getCurrentTestSuite: function() {
235
if (!this.currentTestSuite) {
236
this.currentTestSuite = new HtmlTestSuite(this.getDocument());
238
return this.currentTestSuite;
243
/** HtmlTestFrame - encapsulates the test-case iframe element */
244
var HtmlTestFrame = classCreate();
245
objectExtend(HtmlTestFrame.prototype, SeleniumFrame.prototype);
246
objectExtend(HtmlTestFrame.prototype, {
248
_onLoad: function() {
249
this.currentTestCase = new HtmlTestCase(this.getDocument(), htmlTestRunner.getTestSuite().getCurrentRow());
252
getCurrentTestCase: function() {
253
return this.currentTestCase;
258
function onSeleniumLoad() {
259
suiteFrame = new HtmlTestSuiteFrame(getSuiteFrame());
260
testFrame = new HtmlTestFrame(getTestFrame());
261
htmlTestRunner = new HtmlTestRunner();
267
function getSuiteFrame() {
268
var f = $('testSuiteFrame');
271
// proxyInjection mode does not set myiframe
276
function getTestFrame() {
277
var f = $('testFrame');
280
// proxyInjection mode does not set myiframe
285
var HtmlTestRunnerControlPanel = classCreate();
286
objectExtend(HtmlTestRunnerControlPanel.prototype, URLConfiguration.prototype);
287
objectExtend(HtmlTestRunnerControlPanel.prototype, {
288
initialize: function() {
289
this._acquireQueryString();
291
this.runInterval = 0;
293
this.highlightOption = $('highlightOption');
294
this.pauseButton = $('pauseTest');
295
this.stepButton = $('stepTest');
297
this.highlightOption.onclick = fnBindAsEventListener((function() {
298
this.setHighlightOption();
300
this.pauseButton.onclick = fnBindAsEventListener(this.pauseCurrentTest, this);
301
this.stepButton.onclick = fnBindAsEventListener(this.stepCurrentTest, this);
304
this.speedController = new Control.Slider('speedHandle', 'speedTrack', {
306
onSlide: fnBindAsEventListener(this.setRunInterval, this),
307
onChange: fnBindAsEventListener(this.setRunInterval, this)
310
this._parseQueryParameter();
313
setHighlightOption: function () {
314
var isHighlight = this.highlightOption.checked;
315
selenium.browserbot.setShouldHighlightElement(isHighlight);
318
_parseQueryParameter: function() {
319
var tempRunInterval = this._getQueryParameter("runInterval");
320
if (tempRunInterval) {
321
this.setRunInterval(tempRunInterval);
323
this.highlightOption.checked = this._getQueryParameter("highlight");
326
setRunInterval: function(runInterval) {
327
this.runInterval = runInterval;
330
setToPauseAtNextCommand: function() {
331
this.runInterval = -1;
334
pauseCurrentTest: function () {
335
this.setToPauseAtNextCommand();
336
this._switchPauseButtonToContinue();
339
continueCurrentTest: function () {
341
currentTest.resume();
345
// this.runInterval = this.speedController.value;
346
this._switchContinueButtonToPause();
349
_switchContinueButtonToPause: function() {
350
this.pauseButton.className = "cssPauseTest";
351
this.pauseButton.onclick = fnBindAsEventListener(this.pauseCurrentTest, this);
354
_switchPauseButtonToContinue: function() {
355
$('stepTest').disabled = false;
356
this.pauseButton.className = "cssContinueTest";
357
this.pauseButton.onclick = fnBindAsEventListener(this.continueCurrentTest, this);
360
stepCurrentTest: function () {
361
this.setToPauseAtNextCommand();
362
currentTest.resume();
365
isAutomatedRun: function() {
366
return this._isQueryParameterTrue("auto");
369
shouldSaveResultsToFile: function() {
370
return this._isQueryParameterTrue("save");
373
closeAfterTests: function() {
374
return this._isQueryParameterTrue("close");
377
getTestSuiteName: function() {
378
return this._getQueryParameter("test");
381
getSingleTestName: function() {
382
return this._getQueryParameter("singletest");
385
getAutoUrl: function() {
386
return this._getQueryParameter("autoURL");
389
getResultsUrl: function() {
390
return this._getQueryParameter("resultsUrl");
393
_acquireQueryString: function() {
394
if (this.queryString) return;
395
if (browserVersion.isHTA) {
396
var args = this._extractArgs();
397
if (args.length < 2) return null;
398
this.queryString = args[1];
400
this.queryString = location.search.substr(1);
406
var AbstractResultAwareRow = classCreate();
407
objectExtend(AbstractResultAwareRow.prototype, {
409
initialize: function(trElement) {
410
this.trElement = trElement;
413
setStatus: function(status) {
415
this.trElement.className = this.trElement.className.replace(/status_[a-z]+/, "");
417
addClassName(this.trElement, "status_" + status);
422
addClassName(this.trElement, "selected");
423
safeScrollIntoView(this.trElement);
426
unselect: function() {
427
removeClassName(this.trElement, "selected");
430
markPassed: function() {
431
this.setStatus("passed");
434
markDone: function() {
435
this.setStatus("done");
438
markFailed: function() {
439
this.setStatus("failed");
444
var TitleRow = classCreate();
445
objectExtend(TitleRow.prototype, AbstractResultAwareRow.prototype);
446
objectExtend(TitleRow.prototype, {
448
initialize: function(trElement) {
449
this.trElement = trElement;
450
trElement.className = "title";
455
var HtmlTestCaseRow = classCreate();
456
objectExtend(HtmlTestCaseRow.prototype, AbstractResultAwareRow.prototype);
457
objectExtend(HtmlTestCaseRow.prototype, {
459
getCommand: function () {
460
return new SeleniumCommand(getText(this.trElement.cells[0]),
461
getText(this.trElement.cells[1]),
462
getText(this.trElement.cells[2]),
463
this.isBreakpoint());
466
markFailed: function(errorMsg) {
467
AbstractResultAwareRow.prototype.markFailed.call(this, errorMsg);
468
this.setMessage(errorMsg);
471
setMessage: function(message) {
472
setText(this.trElement.cells[2], message);
476
this.setStatus(null);
477
var thirdCell = this.trElement.cells[2];
479
if (thirdCell.originalHTML) {
480
thirdCell.innerHTML = thirdCell.originalHTML;
482
thirdCell.originalHTML = thirdCell.innerHTML;
487
onClick: function() {
488
if (this.trElement.isBreakpoint == undefined) {
489
this.trElement.isBreakpoint = true;
490
addClassName(this.trElement, "breakpoint");
492
this.trElement.isBreakpoint = undefined;
493
removeClassName(this.trElement, "breakpoint");
497
addBreakpointSupport: function() {
498
elementSetStyle(this.trElement, {"cursor" : "pointer"});
499
this.trElement.onclick = fnBindAsEventListener(function() {
504
isBreakpoint: function() {
505
if (this.trElement.isBreakpoint == undefined || this.trElement.isBreakpoint == null) {
508
return this.trElement.isBreakpoint;
512
var HtmlTestSuiteRow = classCreate();
513
objectExtend(HtmlTestSuiteRow.prototype, AbstractResultAwareRow.prototype);
514
objectExtend(HtmlTestSuiteRow.prototype, {
516
initialize: function(trElement, testFrame, htmlTestSuite) {
517
this.trElement = trElement;
518
this.testFrame = testFrame;
519
this.htmlTestSuite = htmlTestSuite;
520
this.link = trElement.getElementsByTagName("a")[0];
521
this.link.onclick = fnBindAsEventListener(this._onClick, this);
525
this.setStatus(null);
528
_onClick: function() {
529
this.loadTestCase(null);
533
loadTestCase: function(onloadFunction) {
534
this.htmlTestSuite.unselectCurrentRow();
536
this.htmlTestSuite.currentRowInSuite = this.trElement.rowIndex - 1;
537
// If the row has a stored results table, use that
538
var resultsFromPreviousRun = this.trElement.cells[1];
539
if (resultsFromPreviousRun) {
540
// todo: delegate to TestFrame, e.g.
541
// this.testFrame.restoreTestCase(resultsFromPreviousRun.innerHTML);
542
var testBody = this.testFrame.getDocument().body;
543
testBody.innerHTML = resultsFromPreviousRun.innerHTML;
544
this.testFrame._onLoad();
545
if (onloadFunction) {
549
this.testFrame.load(this.link.href, onloadFunction);
553
saveTestResults: function() {
554
// todo: GLOBAL ACCESS!
555
var resultHTML = this.testFrame.getDocument().body.innerHTML;
556
if (!resultHTML) return;
558
// todo: why create this div?
559
var divElement = this.trElement.ownerDocument.createElement("div");
560
divElement.innerHTML = resultHTML;
562
var hiddenCell = this.trElement.ownerDocument.createElement("td");
563
hiddenCell.appendChild(divElement);
564
hiddenCell.style.display = "none";
566
this.trElement.appendChild(hiddenCell);
571
var HtmlTestSuite = classCreate();
572
objectExtend(HtmlTestSuite.prototype, {
574
initialize: function(suiteDocument) {
575
this.suiteDocument = suiteDocument;
576
this.suiteRows = this._collectSuiteRows();
577
this.titleRow = new TitleRow(this.getTestTable().rows[0]);
583
this.currentRowInSuite = -1;
584
this.titleRow.setStatus(null);
585
for (var i = 0; i < this.suiteRows.length; i++) {
586
var row = this.suiteRows[i];
591
getSuiteRows: function() {
592
return this.suiteRows;
595
getTestTable: function() {
596
var tables = $A(this.suiteDocument.getElementsByTagName("table"));
600
isAvailable: function() {
601
return this.getTestTable() != null;
604
_collectSuiteRows: function () {
606
var tables = $A(this.suiteDocument.getElementsByTagName("table"));
607
var testTable = tables[0];
608
for (rowNum = 1; rowNum < testTable.rows.length; rowNum++) {
609
var rowElement = testTable.rows[rowNum];
610
result.push(new HtmlTestSuiteRow(rowElement, testFrame, this));
613
// process the unsuited rows as well
614
for (var tableNum = 1; tableNum < $A(this.suiteDocument.getElementsByTagName("table")).length; tableNum++) {
615
testTable = tables[tableNum];
616
for (rowNum = 1; rowNum < testTable.rows.length; rowNum++) {
617
var rowElement = testTable.rows[rowNum];
618
new HtmlTestSuiteRow(rowElement, testFrame, this);
624
getCurrentRow: function() {
625
if (this.currentRowInSuite == -1) {
628
return this.suiteRows[this.currentRowInSuite];
631
unselectCurrentRow: function() {
632
var currentRow = this.getCurrentRow()
634
currentRow.unselect();
638
markFailed: function() {
640
this.titleRow.markFailed();
643
markDone: function() {
645
this.titleRow.markPassed();
649
_startCurrentTestCase: function() {
650
this.getCurrentRow().loadTestCase(fnBind(htmlTestRunner.startTest, htmlTestRunner));
653
_onTestSuiteComplete: function() {
655
new TestResult(this.failed, this.getTestTable()).post();
658
updateSuiteWithResultOfPreviousTest: function() {
659
if (this.currentRowInSuite >= 0) {
660
this.getCurrentRow().saveTestResults();
664
runNextTestInSuite: function() {
665
this.currentRowInSuite++;
667
// If we are done with all of the tests, set the title bar as pass or fail
668
if (this.currentRowInSuite >= this.suiteRows.length) {
669
this._onTestSuiteComplete();
671
this._startCurrentTestCase();
679
var TestResult = classCreate();
680
objectExtend(TestResult.prototype, {
682
// Post the results to a servlet, CGI-script, etc. The URL of the
683
// results-handler defaults to "/postResults", but an alternative location
684
// can be specified by providing a "resultsUrl" query parameter.
686
// Parameters passed to the results-handler are:
687
// result: passed/failed depending on whether the suite passed or failed
688
// totalTime: the total running time in seconds for the suite.
690
// numTestPasses: the total number of tests which passed.
691
// numTestFailures: the total number of tests which failed.
693
// numCommandPasses: the total number of commands which passed.
694
// numCommandFailures: the total number of commands which failed.
695
// numCommandErrors: the total number of commands which errored.
697
// suite: the suite table, including the hidden column of test results
698
// testTable.1 to testTable.N: the individual test tables
700
initialize: function (suiteFailed, suiteTable) {
701
this.controlPanel = htmlTestRunner.controlPanel;
702
this.metrics = htmlTestRunner.metrics;
703
this.suiteFailed = suiteFailed;
704
this.suiteTable = suiteTable;
708
if (!this.controlPanel.isAutomatedRun()) {
711
var form = document.createElement("form");
712
document.body.appendChild(form);
714
form.id = "resultsForm";
715
form.method = "post";
716
form.target = "myiframe";
718
var resultsUrl = this.controlPanel.getResultsUrl();
720
resultsUrl = "./postResults";
723
var actionAndParameters = resultsUrl.split('?', 2);
724
form.action = actionAndParameters[0];
725
var resultsUrlQueryString = actionAndParameters[1];
727
form.createHiddenField = function(name, value) {
728
input = document.createElement("input");
729
input.type = "hidden";
732
this.appendChild(input);
735
if (resultsUrlQueryString) {
736
var clauses = resultsUrlQueryString.split('&');
737
for (var i = 0; i < clauses.length; i++) {
738
var keyValuePair = clauses[i].split('=', 2);
739
var key = unescape(keyValuePair[0]);
740
var value = unescape(keyValuePair[1]);
741
form.createHiddenField(key, value);
745
form.createHiddenField("selenium.version", Selenium.version);
746
form.createHiddenField("selenium.revision", Selenium.revision);
748
form.createHiddenField("result", this.suiteFailed ? "failed" : "passed");
750
form.createHiddenField("totalTime", Math.floor((this.metrics.currentTime - this.metrics.startTime) / 1000));
751
form.createHiddenField("numTestPasses", this.metrics.numTestPasses);
752
form.createHiddenField("numTestFailures", this.metrics.numTestFailures);
753
form.createHiddenField("numCommandPasses", this.metrics.numCommandPasses);
754
form.createHiddenField("numCommandFailures", this.metrics.numCommandFailures);
755
form.createHiddenField("numCommandErrors", this.metrics.numCommandErrors);
757
// Create an input for each test table. The inputs are named
758
// testTable.1, testTable.2, etc.
759
for (rowNum = 1; rowNum < this.suiteTable.rows.length; rowNum++) {
760
// If there is a second column, then add a new input
761
if (this.suiteTable.rows[rowNum].cells.length > 1) {
762
var resultCell = this.suiteTable.rows[rowNum].cells[1];
763
form.createHiddenField("testTable." + rowNum, resultCell.innerHTML);
764
// remove the resultCell, so it's not included in the suite HTML
765
resultCell.parentNode.removeChild(resultCell);
769
form.createHiddenField("numTestTotal", rowNum);
771
// Add HTML for the suite itself
772
form.createHiddenField("suite", this.suiteTable.parentNode.innerHTML);
774
if (this.controlPanel.shouldSaveResultsToFile()) {
775
this._saveToFile(resultsUrl, form);
779
document.body.removeChild(form);
780
if (this.controlPanel.closeAfterTests()) {
785
_saveToFile: function (fileName, form) {
786
// This only works when run as an IE HTA
787
var inputs = new Object();
788
for (var i = 0; i < form.elements.length; i++) {
789
inputs[form.elements[i].name] = form.elements[i].value;
791
var objFSO = new ActiveXObject("Scripting.FileSystemObject")
792
var scriptFile = objFSO.CreateTextFile(fileName);
793
scriptFile.WriteLine("<html><body>\n<h1>Test suite results </h1>" +
794
"\n\n<table>\n<tr>\n<td>result:</td>\n<td>" + inputs["result"] + "</td>\n" +
795
"</tr>\n<tr>\n<td>totalTime:</td>\n<td>" + inputs["totalTime"] + "</td>\n</tr>\n" +
796
"<tr>\n<td>numTestPasses:</td>\n<td>" + inputs["numTestPasses"] + "</td>\n</tr>\n" +
797
"<tr>\n<td>numTestFailures:</td>\n<td>" + inputs["numTestFailures"] + "</td>\n</tr>\n" +
798
"<tr>\n<td>numCommandPasses:</td>\n<td>" + inputs["numCommandPasses"] + "</td>\n</tr>\n" +
799
"<tr>\n<td>numCommandFailures:</td>\n<td>" + inputs["numCommandFailures"] + "</td>\n</tr>\n" +
800
"<tr>\n<td>numCommandErrors:</td>\n<td>" + inputs["numCommandErrors"] + "</td>\n</tr>\n" +
801
"<tr>\n<td>" + inputs["suite"] + "</td>\n<td> </td>\n</tr>");
802
var testNum = inputs["numTestTotal"];
803
for (var rowNum = 1; rowNum < testNum; rowNum++) {
804
scriptFile.WriteLine("<tr>\n<td>" + inputs["testTable." + rowNum] + "</td>\n<td> </td>\n</tr>");
806
scriptFile.WriteLine("</table></body></html>");
811
/** HtmlTestCase encapsulates an HTML test document */
812
var HtmlTestCase = classCreate();
813
objectExtend(HtmlTestCase.prototype, {
815
initialize: function(testDocument, htmlTestSuiteRow) {
816
if (testDocument == null) {
817
throw "testDocument should not be null";
819
if (htmlTestSuiteRow == null) {
820
throw "htmlTestSuiteRow should not be null";
822
this.testDocument = testDocument;
823
this.htmlTestSuiteRow = htmlTestSuiteRow;
824
this.headerRow = new TitleRow(this.testDocument.getElementsByTagName("tr")[0]);
825
this.commandRows = this._collectCommandRows();
826
this.nextCommandRowIndex = 0;
827
this._addBreakpointSupport();
830
_collectCommandRows: function () {
831
var commandRows = [];
832
var tables = $A(this.testDocument.getElementsByTagName("table"));
834
for (var i = 0; i < tables.length; i++) {
835
var table = tables[i];
836
var tableRows = $A(table.rows);
837
for (var j = 0; j < tableRows.length; j++) {
838
var candidateRow = tableRows[j];
839
if (self.isCommandRow(candidateRow)) {
840
commandRows.push(new HtmlTestCaseRow(candidateRow));
847
isCommandRow: function (row) {
848
return row.cells.length >= 3;
853
* reset the test to runnable state
855
this.nextCommandRowIndex = 0;
858
for (var i = 0; i < this.commandRows.length; i++) {
859
var row = this.commandRows[i];
863
// remove any additional fake "error" row added to the end of the document
864
var errorElement = this.testDocument.getElementById('error');
866
errorElement.parentNode.removeChild(errorElement);
870
getCommandRows: function () {
871
return this.commandRows;
874
setStatus: function(status) {
875
this.headerRow.setStatus(status);
878
markFailed: function() {
879
this.setStatus("failed");
880
this.htmlTestSuiteRow.markFailed();
883
markPassed: function() {
884
this.setStatus("passed");
885
this.htmlTestSuiteRow.markPassed();
888
addErrorMessage: function(errorMsg, currentRow) {
889
errorMsg = errorMsg.replace(/ /g, String.fromCharCode(160)).replace("\n", '\\n');
891
currentRow.markFailed(errorMsg);
893
var errorElement = this.testDocument.createElement("p");
894
errorElement.id = "error";
895
setText(errorElement, errorMsg);
896
this.testDocument.body.appendChild(errorElement);
897
errorElement.className = "status_failed";
901
_addBreakpointSupport: function() {
902
for (var i = 0; i < this.commandRows.length; i++) {
903
var row = this.commandRows[i];
904
row.addBreakpointSupport();
908
hasMoreCommandRows: function() {
909
return this.nextCommandRowIndex < this.commandRows.length;
912
getNextCommandRow: function() {
913
if (this.hasMoreCommandRows()) {
914
return this.commandRows[this.nextCommandRowIndex++];
922
// TODO: split out an JavascriptTestCase class to handle the "sejs" stuff
924
var get_new_rows = function() {
925
var row_array = new Array();
926
for (var i = 0; i < new_block.length; i++) {
928
var new_source = (new_block[i][0].tokenizer.source.slice(new_block[i][0].start,
929
new_block[i][0].end));
931
var row = '<td style="display:none;" class="js">getEval</td>' +
932
'<td style="display:none;">currentTest.doNextCommand()</td>' +
933
'<td style="white-space: pre;">' + new_source + '</td>' +
942
var Metrics = classCreate();
943
objectExtend(Metrics.prototype, {
944
initialize: function() {
945
// The number of tests run
946
this.numTestPasses = 0;
947
// The number of tests that have failed
948
this.numTestFailures = 0;
949
// The number of commands which have passed
950
this.numCommandPasses = 0;
951
// The number of commands which have failed
952
this.numCommandFailures = 0;
953
// The number of commands which have caused errors (element not found)
954
this.numCommandErrors = 0;
955
// The time that the test was started.
956
this.startTime = null;
958
this.currentTime = null;
961
printMetrics: function() {
962
setText($('commandPasses'), this.numCommandPasses);
963
setText($('commandFailures'), this.numCommandFailures);
964
setText($('commandErrors'), this.numCommandErrors);
965
setText($('testRuns'), this.numTestPasses + this.numTestFailures);
966
setText($('testFailures'), this.numTestFailures);
968
this.currentTime = new Date().getTime();
970
var timeDiff = this.currentTime - this.startTime;
971
var totalSecs = Math.floor(timeDiff / 1000);
973
var minutes = Math.floor(totalSecs / 60);
974
var seconds = totalSecs % 60;
976
setText($('elapsedTime'), this._pad(minutes) + ":" + this._pad(seconds));
979
// Puts a leading 0 on num if it is less than 10
980
_pad: function(num) {
981
return (num > 9) ? num : "0" + num;
984
resetMetrics: function() {
985
this.numTestPasses = 0;
986
this.numTestFailures = 0;
987
this.numCommandPasses = 0;
988
this.numCommandFailures = 0;
989
this.numCommandErrors = 0;
990
this.startTime = new Date().getTime();
995
var HtmlRunnerCommandFactory = classCreate();
996
objectExtend(HtmlRunnerCommandFactory.prototype, {
998
initialize: function(seleniumCommandFactory, testLoop) {
999
this.seleniumCommandFactory = seleniumCommandFactory;
1000
this.testLoop = testLoop;
1002
//todo: register commands
1005
getCommandHandler: function(command) {
1006
if (this.handlers[command]) {
1007
return this.handlers[command];
1009
return this.seleniumCommandFactory.getCommandHandler(command);
1014
var HtmlRunnerTestLoop = classCreate();
1015
objectExtend(HtmlRunnerTestLoop.prototype, new TestLoop());
1016
objectExtend(HtmlRunnerTestLoop.prototype, {
1017
initialize: function(htmlTestCase, metrics, seleniumCommandFactory) {
1019
this.commandFactory = new HtmlRunnerCommandFactory(seleniumCommandFactory, this);
1020
this.metrics = metrics;
1022
this.htmlTestCase = htmlTestCase;
1025
global.se = selenium;
1027
this.currentRow = null;
1028
this.currentRowIndex = 0;
1030
// used for selenium tests in javascript
1031
this.currentItem = null;
1032
this.commandAgenda = new Array();
1033
this.expectedFailure = null;
1034
this.expectedFailureType = null;
1036
this.htmlTestCase.reset();
1038
this.sejsElement = this.htmlTestCase.testDocument.getElementById('sejs');
1039
if (this.sejsElement) {
1040
var fname = 'Selenium JavaScript';
1041
parse_result = parse(this.sejsElement.innerHTML, fname, 0);
1043
var x2 = new ExecutionContext(GLOBAL_CODE);
1044
ExecutionContext.current = x2;
1046
execute(parse_result, x2)
1050
_advanceToNextRow: function() {
1051
if (this.htmlTestCase.hasMoreCommandRows()) {
1052
this.currentRow = this.htmlTestCase.getNextCommandRow();
1053
if (this.sejsElement) {
1054
this.currentItem = agenda.pop();
1055
this.currentRowIndex++;
1058
this.currentRow = null;
1059
this.currentItem = null;
1063
nextCommand : function() {
1064
this._advanceToNextRow();
1065
if (this.currentRow == null) {
1068
return this.currentRow.getCommand();
1071
commandStarted : function() {
1072
$('pauseTest').disabled = false;
1073
this.currentRow.select();
1074
this.metrics.printMetrics();
1077
commandComplete : function(result) {
1078
this._checkExpectedFailure(result);
1079
if (result.failed) {
1080
this.metrics.numCommandFailures += 1;
1081
this._recordFailure(result.failureMessage);
1082
} else if (result.passed) {
1083
this.metrics.numCommandPasses += 1;
1084
this.currentRow.markPassed();
1086
this.currentRow.markDone();
1090
_checkExpectedFailure : function(result) {
1091
if (this.expectedFailure != null) {
1092
if (this.expectedFailureJustSet) {
1093
this.expectedFailureJustSet = false;
1096
if (!result.failed) {
1097
result.passed = false;
1098
result.failed = true;
1099
result.failureMessage = "Expected " + this.expectedFailureType + " did not occur.";
1101
if (PatternMatcher.matches(this.expectedFailure, result.failureMessage)) {
1102
var failureType = result.error ? "error" : "failure";
1103
if (failureType == this.expectedFailureType) {
1104
result.failed = false;
1105
result.passed = true;
1107
result.failed = true;
1108
result.failureMessage = "Expected "+this.expectedFailureType+", but "+failureType+" occurred instead";
1111
result.failed = true;
1112
result.failureMessage = "Expected " + this.expectedFailureType + " message '" + this.expectedFailure
1113
+ "' but was '" + result.failureMessage + "'";
1116
this.expectedFailure = null;
1117
this.expectedFailureType = null;
1121
commandError : function(errorMessage) {
1122
var tempResult = {};
1123
tempResult.passed = false;
1124
tempResult.failed = true;
1125
tempResult.error = true;
1126
tempResult.failureMessage = errorMessage;
1127
this._checkExpectedFailure(tempResult);
1128
if (tempResult.passed) {
1129
this.currentRow.markDone();
1132
errorMessage = tempResult.failureMessage;
1133
this.metrics.numCommandErrors += 1;
1134
this._recordFailure(errorMessage);
1137
_recordFailure : function(errorMsg) {
1138
LOG.warn("currentTest.recordFailure: " + errorMsg);
1139
htmlTestRunner.markFailed();
1140
this.htmlTestCase.addErrorMessage(errorMsg, this.currentRow);
1143
testComplete : function() {
1144
$('pauseTest').disabled = true;
1145
$('stepTest').disabled = true;
1146
if (htmlTestRunner.testFailed) {
1147
this.htmlTestCase.markFailed();
1148
this.metrics.numTestFailures += 1;
1150
this.htmlTestCase.markPassed();
1151
this.metrics.numTestPasses += 1;
1154
this.metrics.printMetrics();
1156
window.setTimeout(function() {
1157
htmlTestRunner.runNextTest();
1161
getCommandInterval : function() {
1162
return htmlTestRunner.controlPanel.runInterval;
1165
pause : function() {
1166
htmlTestRunner.controlPanel.pauseCurrentTest();
1169
doNextCommand: function() {
1170
var _n = this.currentItem[0];
1171
var _x = this.currentItem[1];
1173
new_block = new Array()
1175
if (new_block.length > 0) {
1176
var the_table = this.htmlTestCase.testDocument.getElementById("se-js-table")
1177
var loc = this.currentRowIndex
1178
var new_rows = get_new_rows()
1180
// make the new statements visible on screen...
1181
for (var i = 0; i < new_rows.length; i++) {
1182
the_table.insertRow(loc + 1);
1183
the_table.rows[loc + 1].innerHTML = new_rows[i];
1184
this.commandRows.unshift(the_table.rows[loc + 1])
1192
Selenium.prototype.doPause = function(waitTime) {
1193
/** Wait for the specified amount of time (in milliseconds)
1194
* @param waitTime the amount of time to sleep (in milliseconds)
1196
// todo: should not refer to currentTest directly
1197
currentTest.pauseInterval = waitTime;
1200
Selenium.prototype.doBreak = function() {
1201
/** Halt the currently running test, and wait for the user to press the Continue button.
1202
* This command is useful for debugging, but be careful when using it, because it will
1203
* force automated tests to hang until a user intervenes manually.
1205
// todo: should not refer to controlPanel directly
1206
htmlTestRunner.controlPanel.setToPauseAtNextCommand();
1209
Selenium.prototype.doStore = function(expression, variableName) {
1210
/** This command is a synonym for storeExpression.
1211
* @param expression the value to store
1212
* @param variableName the name of a <a href="#storedVars">variable</a> in which the result is to be stored.
1214
storedVars[variableName] = expression;
1218
* Click on the located element, and attach a callback to notify
1219
* when the page is reloaded.
1221
// DGF TODO this code has been broken for some time... what is it trying to accomplish?
1222
Selenium.prototype.XXXdoModalDialogTest = function(returnValue) {
1223
this.browserbot.doModalDialogTest(returnValue);
1226
Selenium.prototype.doEcho = function(message) {
1227
/** Prints the specified message into the third table cell in your Selenese tables.
1228
* Useful for debugging.
1229
* @param message the message to print
1231
currentTest.currentRow.setMessage(message);
1234
Selenium.prototype.assertSelected = function(selectLocator, optionLocator) {
1236
* Verifies that the selected option of a drop-down satisfies the optionSpecifier. <i>Note that this command is deprecated; you should use assertSelectedLabel, assertSelectedValue, assertSelectedIndex, or assertSelectedId instead.</i>
1238
* <p>See the select command for more information about option locators.</p>
1240
* @param selectLocator an <a href="#locators">element locator</a> identifying a drop-down menu
1241
* @param optionLocator an option locator, typically just an option label (e.g. "John Smith")
1243
var element = this.page().findElement(selectLocator);
1244
var locator = this.optionLocatorFactory.fromLocatorString(optionLocator);
1245
if (element.selectedIndex == -1)
1247
Assert.fail("No option selected");
1249
locator.assertSelected(element);
1252
Selenium.prototype.assertFailureOnNext = function(message) {
1254
* Tell Selenium to expect a failure on the next command execution.
1255
* @param message The failure message we should expect. This command will fail if the wrong failure message appears.
1258
throw new SeleniumError("Message must be provided");
1261
currentTest.expectedFailure = message;
1262
currentTest.expectedFailureType = "failure";
1263
currentTest.expectedFailureJustSet = true;
1266
Selenium.prototype.assertErrorOnNext = function(message) {
1268
* Tell Selenium to expect an error on the next command execution.
1269
* @param message The error message we should expect. This command will fail if the wrong error message appears.
1271
// This command temporarily installs a CommandFactory that generates
1272
// CommandHandlers that expect an error.
1274
throw new SeleniumError("Message must be provided");
1277
currentTest.expectedFailure = message;
1278
currentTest.expectedFailureType = "error";
1279
currentTest.expectedFailureJustSet = true;