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.
19
* This script provides the Javascript API to drive the test application contained within
22
* Add support for more events (keyboard and mouse)
23
* Allow to switch "user-entry" mode from mouse-based to keyboard-based, firing different
24
* events in different modes.
27
// The window to which the commands will be sent. For example, to click on a
28
// popup window, first select that window, and then do a normal click command.
29
var BrowserBot = function(topLevelApplicationWindow) {
30
this.topWindow = topLevelApplicationWindow;
31
this.topFrame = this.topWindow;
32
this.baseUrl=window.location.href;
34
// the buttonWindow is the Selenium window
35
// it contains the Run/Pause buttons... this should *not* be the AUT window
36
this.buttonWindow = window;
37
this.currentWindow = this.topWindow;
38
this.currentWindowName = null;
39
this.allowNativeXpath = true;
40
this.xpathLibrary = this.defaultXpathLibrary = 'ajaxslt' // change to "javascript-xpath" for the newer, faster engine
42
// We need to know this in advance, in case the frame closes unexpectedly
43
this.isSubFrameSelected = false;
45
this.altKeyDown = false;
46
this.controlKeyDown = false;
47
this.shiftKeyDown = false;
48
this.metaKeyDown = false;
50
this.modalDialogTest = null;
51
this.recordedAlerts = new Array();
52
this.recordedConfirmations = new Array();
53
this.recordedPrompts = new Array();
54
this.openedWindows = {};
55
this.nextConfirmResult = true;
56
this.nextPromptResult = '';
57
this.newPageLoaded = false;
58
this.pageLoadError = null;
60
this.shouldHighlightLocatedElement = false;
62
this.uniqueId = "seleniumMarker" + new Date().getTime();
63
this.pollingForLoad = new Object();
64
this.permDeniedCount = new Object();
65
this.windowPollers = new Array();
66
// DGF for backwards compatibility
67
this.browserbot = this;
71
objectExtend(this, PageBot.prototype);
72
this._registerAllLocatorFunctions();
74
this.recordPageLoad = function(elementOrWindow) {
75
LOG.debug("Page load detected");
77
if (elementOrWindow.location && elementOrWindow.location.href) {
78
LOG.debug("Page load location=" + elementOrWindow.location.href);
79
} else if (elementOrWindow.contentWindow && elementOrWindow.contentWindow.location && elementOrWindow.contentWindow.location.href) {
80
LOG.debug("Page load location=" + elementOrWindow.contentWindow.location.href);
82
LOG.debug("Page load location unknown, current window location=" + this.getCurrentWindow(true).location);
85
LOG.error("Caught an exception attempting to log location; this should get noticed soon!");
87
self.pageLoadError = e;
90
self.newPageLoaded = true;
93
this.isNewPageLoaded = function() {
94
if (this.pageLoadError) {
95
LOG.error("isNewPageLoaded found an old pageLoadError");
96
var e = this.pageLoadError;
97
this.pageLoadError = null;
100
return self.newPageLoaded;
105
// DGF PageBot exists for backwards compatibility with old user-extensions
106
var PageBot = function(){};
108
BrowserBot.createForWindow = function(window, proxyInjectionMode) {
110
LOG.debug('createForWindow');
111
LOG.debug("browserName: " + browserVersion.name);
112
LOG.debug("userAgent: " + navigator.userAgent);
113
if (browserVersion.isIE) {
114
browserbot = new IEBrowserBot(window);
116
else if (browserVersion.isKonqueror) {
117
browserbot = new KonquerorBrowserBot(window);
119
else if (browserVersion.isOpera) {
120
browserbot = new OperaBrowserBot(window);
122
else if (browserVersion.isSafari) {
123
browserbot = new SafariBrowserBot(window);
126
// Use mozilla by default
127
browserbot = new MozillaBrowserBot(window);
129
// getCurrentWindow has the side effect of modifying it to handle page loads etc
130
browserbot.proxyInjectionMode = proxyInjectionMode;
131
browserbot.getCurrentWindow(); // for modifyWindow side effect. This is not a transparent style
135
// todo: rename? This doesn't actually "do" anything.
136
BrowserBot.prototype.doModalDialogTest = function(test) {
137
this.modalDialogTest = test;
140
BrowserBot.prototype.cancelNextConfirmation = function(result) {
141
this.nextConfirmResult = result;
144
BrowserBot.prototype.setNextPromptResult = function(result) {
145
this.nextPromptResult = result;
148
BrowserBot.prototype.hasAlerts = function() {
149
return (this.recordedAlerts.length > 0);
152
BrowserBot.prototype.relayBotToRC = function(s) {
153
// DGF need to do this funny trick to see if we're in PI mode, because
154
// "this" might be the window, rather than the browserbot (e.g. during window.alert)
155
var piMode = this.proxyInjectionMode;
157
if (typeof(selenium) != "undefined") {
158
piMode = selenium.browserbot && selenium.browserbot.proxyInjectionMode;
162
this.relayToRC("selenium." + s);
166
BrowserBot.prototype.relayToRC = function(name) {
167
var object = eval(name);
168
var s = 'state:' + serializeObject(name, object) + "\n";
169
sendToRC(s,"state=true");
172
BrowserBot.prototype.resetPopups = function() {
173
this.recordedAlerts = [];
174
this.recordedConfirmations = [];
175
this.recordedPrompts = [];
178
BrowserBot.prototype.getNextAlert = function() {
179
var t = this.recordedAlerts.shift();
181
t = t.replace(/\n/g, " "); // because Selenese loses \n's when retrieving text from HTML table
183
this.relayBotToRC("browserbot.recordedAlerts");
187
BrowserBot.prototype.hasConfirmations = function() {
188
return (this.recordedConfirmations.length > 0);
191
BrowserBot.prototype.getNextConfirmation = function() {
192
var t = this.recordedConfirmations.shift();
193
this.relayBotToRC("browserbot.recordedConfirmations");
197
BrowserBot.prototype.hasPrompts = function() {
198
return (this.recordedPrompts.length > 0);
201
BrowserBot.prototype.getNextPrompt = function() {
202
var t = this.recordedPrompts.shift();
203
this.relayBotToRC("browserbot.recordedPrompts");
207
/* Fire a mouse event in a browser-compatible manner */
209
BrowserBot.prototype.triggerMouseEvent = function(element, eventType, canBubble, clientX, clientY, button) {
210
clientX = clientX ? clientX : 0;
211
clientY = clientY ? clientY : 0;
213
LOG.debug("triggerMouseEvent assumes setting screenX and screenY to 0 is ok");
217
canBubble = (typeof(canBubble) == undefined) ? true : canBubble;
218
if (element.fireEvent && element.ownerDocument && element.ownerDocument.createEventObject) { //IE
219
var evt = createEventObject(element, this.controlKeyDown, this.altKeyDown, this.shiftKeyDown, this.metaKeyDown);
221
evt.button = button ? button : 1; // default will be the left mouse click ( http://www.javascriptkit.com/jsref/event.shtml )
222
evt.relatedTarget = null;
223
if (!screenX && !screenY && !clientX && !clientY && !this.controlKeyDown && !this.altKeyDown && !this.shiftKeyDown && !this.metaKeyDown) {
224
element.fireEvent('on' + eventType);
227
evt.screenX = screenX;
228
evt.screenY = screenY;
229
evt.clientX = clientX;
230
evt.clientY = clientY;
232
// when we go this route, window.event is never set to contain the event we have just created.
233
// ideally we could just slide it in as follows in the try-block below, but this normally
234
// doesn't work. This is why I try to avoid this code path, which is only required if we need to
235
// set attributes on the event (e.g., clientX).
240
// getting an "Object does not support this action or property" error. Save the event away
241
// for future reference.
242
// TODO: is there a way to update window.event?
244
// work around for http://jira.openqa.org/browse/SEL-280 -- make the event available somewhere:
245
selenium.browserbot.getCurrentWindow().selenium_event = evt;
247
element.fireEvent('on' + eventType, evt);
251
var evt = document.createEvent('MouseEvents');
252
if (evt.initMouseEvent)
254
// see http://developer.mozilla.org/en/docs/DOM:event.button and
255
// http://developer.mozilla.org/en/docs/DOM:event.initMouseEvent for button ternary logic logic
257
evt.initMouseEvent(eventType, canBubble, true, document.defaultView, 1, screenX, screenY, clientX, clientY,
258
this.controlKeyDown, this.altKeyDown, this.shiftKeyDown, this.metaKeyDown, button ? button : 0, null);
261
LOG.warn("element doesn't have initMouseEvent; firing an event which should -- but doesn't -- have other mouse-event related attributes here, as well as controlKeyDown, altKeyDown, shiftKeyDown, metaKeyDown");
262
evt.initEvent(eventType, canBubble, true);
264
evt.shiftKey = this.shiftKeyDown;
265
evt.metaKey = this.metaKeyDown;
266
evt.altKey = this.altKeyDown;
267
evt.ctrlKey = this.controlKeyDown;
273
element.dispatchEvent(evt);
277
BrowserBot.prototype._windowClosed = function(win) {
279
if (c == null) return true;
283
BrowserBot.prototype._modifyWindow = function(win) {
284
// In proxyInjectionMode, have to suppress LOG calls in _modifyWindow to avoid an infinite loop
285
if (this._windowClosed(win)) {
286
if (!this.proxyInjectionMode) {
287
LOG.error("modifyWindow: Window was closed!");
291
if (!this.proxyInjectionMode) {
292
LOG.debug('modifyWindow ' + this.uniqueId + ":" + win[this.uniqueId]);
294
if (!win[this.uniqueId]) {
295
win[this.uniqueId] = 1;
296
this.modifyWindowToRecordPopUpDialogs(win, this);
298
// In proxyInjection mode, we have our own mechanism for detecting page loads
299
if (!this.proxyInjectionMode) {
300
this.modifySeparateTestWindowToDetectPageLoads(win);
302
if (win.frames && win.frames.length && win.frames.length > 0) {
303
for (var i = 0; i < win.frames.length; i++) {
305
this._modifyWindow(win.frames[i]);
306
} catch (e) {} // we're just trying to be opportunistic; don't worry if this doesn't work out
312
BrowserBot.prototype.selectWindow = function(target) {
313
if (!target || target == "null") {
314
this._selectTopWindow();
317
var result = target.match(/^([a-zA-Z]+)=(.*)/);
320
this._selectWindowByName(target);
323
this._selectWindowByTitle(target);
327
locatorType = result[1];
328
locatorValue = result[2];
329
if (locatorType == "title") {
330
this._selectWindowByTitle(locatorValue);
332
// TODO separate name and var into separate functions
333
else if (locatorType == "name") {
334
this._selectWindowByName(locatorValue);
335
} else if (locatorType == "var") {
336
this._selectWindowByName(locatorValue);
338
throw new SeleniumError("Window locator not recognized: " + locatorType);
342
BrowserBot.prototype._selectTopWindow = function() {
343
this.currentWindowName = null;
344
this.currentWindow = this.topWindow;
345
this.topFrame = this.topWindow;
346
this.isSubFrameSelected = false;
349
BrowserBot.prototype._selectWindowByName = function(target) {
350
this.currentWindow = this.getWindowByName(target, false);
351
this.topFrame = this.currentWindow;
352
this.currentWindowName = target;
353
this.isSubFrameSelected = false;
356
BrowserBot.prototype._selectWindowByTitle = function(target) {
357
var windowName = this.getWindowNameByTitle(target);
359
this._selectTopWindow();
361
this._selectWindowByName(windowName);
365
BrowserBot.prototype.selectFrame = function(target) {
366
if (target.indexOf("index=") == 0) {
367
target = target.substr(6);
368
var frame = this.getCurrentWindow().frames[target];
370
throw new SeleniumError("Not found: frames["+index+"]");
372
if (!frame.document) {
373
throw new SeleniumError("frames["+index+"] is not a frame");
375
this.currentWindow = frame;
376
this.isSubFrameSelected = true;
378
else if (target == "relative=up" || target == "relative=parent") {
379
this.currentWindow = this.getCurrentWindow().parent;
380
this.isSubFrameSelected = (this._getFrameElement(this.currentWindow) != null);
381
} else if (target == "relative=top") {
382
this.currentWindow = this.topFrame;
383
this.isSubFrameSelected = false;
385
var frame = this.findElement(target);
387
throw new SeleniumError("Not found: " + target);
389
// now, did they give us a frame or a frame ELEMENT?
391
if (frame.contentWindow) {
392
// this must be a frame element
393
if (browserVersion.isHTA) {
394
// stupid HTA bug; can't get in the front door
395
target = frame.contentWindow.name;
397
this.currentWindow = frame.contentWindow;
398
this.isSubFrameSelected = true;
401
} else if (frame.document && frame.location) {
402
// must be an actual window frame
403
this.currentWindow = frame;
404
this.isSubFrameSelected = true;
409
// neither, let's loop through the frame names
410
var win = this.getCurrentWindow();
412
if (win && win.frames && win.frames.length) {
413
for (var i = 0; i < win.frames.length; i++) {
414
if (win.frames[i].name == target) {
415
this.currentWindow = win.frames[i];
416
this.isSubFrameSelected = true;
423
throw new SeleniumError("Not a frame: " + target);
427
// modifies the window
428
this.getCurrentWindow();
431
BrowserBot.prototype.doesThisFrameMatchFrameExpression = function(currentFrameString, target) {
433
if (target.indexOf("dom=") == 0) {
434
target = target.substr(4);
436
} else if (target.indexOf("index=") == 0) {
437
target = "frames[" + target.substr(6) + "]";
442
eval("t=" + currentFrameString + "." + target);
445
var autWindow = this.browserbot.getCurrentWindow();
448
if (t.window == autWindow) {
451
if (t.window.uniqueId == autWindow.uniqueId) {
455
} catch (permDenied) {
456
// DGF if the windows are incomparable, they're probably not the same...
463
eval("currentFrame=" + currentFrameString);
464
if (target == "relative=up") {
465
if (currentFrame.window.parent == autWindow) {
470
if (target == "relative=top") {
471
if (currentFrame.window.top == autWindow) {
476
if (currentFrame.window == autWindow.parent) {
477
if (autWindow.name == target) {
481
var element = this.findElement(target, currentFrame.window);
482
if (element.contentWindow == autWindow) {
490
BrowserBot.prototype.openLocation = function(target) {
491
// We're moving to a new page - clear the current one
492
var win = this.getCurrentWindow();
493
LOG.debug("openLocation newPageLoaded = false");
494
this.newPageLoaded = false;
496
this.setOpenLocation(win, target);
499
BrowserBot.prototype.openWindow = function(url, windowID) {
501
url = absolutify(url, this.baseUrl);
503
if (browserVersion.isHTA) {
504
// in HTA mode, calling .open on the window interprets the url relative to that window
505
// we need to absolute-ize the URL to make it consistent
506
var child = this.getCurrentWindow().open(url, windowID);
507
selenium.browserbot.openedWindows[windowID] = child;
509
this.getCurrentWindow().open(url, windowID);
513
BrowserBot.prototype.setIFrameLocation = function(iframe, location) {
514
iframe.src = location;
517
BrowserBot.prototype.setOpenLocation = function(win, loc) {
518
loc = absolutify(loc, this.baseUrl);
519
if (browserVersion.isHTA) {
520
var oldHref = win.location.href;
521
win.location.href = loc;
524
marker = this.isPollingForLoad(win);
525
if (marker && win.location[marker]) {
526
win.location[marker] = false;
528
} catch (e) {} // DGF don't know why, but this often fails
530
win.location.href = loc;
534
BrowserBot.prototype.getCurrentPage = function() {
538
BrowserBot.prototype.modifyWindowToRecordPopUpDialogs = function(windowToModify, browserBot) {
541
windowToModify.seleniumAlert = windowToModify.alert;
543
windowToModify.alert = function(alert) {
544
browserBot.recordedAlerts.push(alert);
545
self.relayBotToRC.call(self, "browserbot.recordedAlerts");
548
windowToModify.confirm = function(message) {
549
browserBot.recordedConfirmations.push(message);
550
var result = browserBot.nextConfirmResult;
551
browserBot.nextConfirmResult = true;
552
self.relayBotToRC.call(self, "browserbot.recordedConfirmations");
556
windowToModify.prompt = function(message) {
557
browserBot.recordedPrompts.push(message);
558
var result = !browserBot.nextConfirmResult ? null : browserBot.nextPromptResult;
559
browserBot.nextConfirmResult = true;
560
browserBot.nextPromptResult = '';
561
self.relayBotToRC.call(self, "browserbot.recordedPrompts");
565
// Keep a reference to all popup windows by name
566
// note that in IE the "windowName" argument must be a valid javascript identifier, it seems.
567
var originalOpen = windowToModify.open;
568
var originalOpenReference;
569
if (browserVersion.isHTA) {
570
originalOpenReference = 'selenium_originalOpen' + new Date().getTime();
571
windowToModify[originalOpenReference] = windowToModify.open;
574
var isHTA = browserVersion.isHTA;
576
var newOpen = function(url, windowName, windowFeatures, replaceFlag) {
577
var myOriginalOpen = originalOpen;
579
myOriginalOpen = this[originalOpenReference];
581
if (windowName == "" || windowName == "_blank") {
582
windowName = "selenium_blank" + Math.round(100000 * Math.random());
583
LOG.warn("Opening window '_blank', which is not a real window name. Randomizing target to be: " + windowName);
585
var openedWindow = myOriginalOpen(url, windowName, windowFeatures, replaceFlag);
586
LOG.debug("window.open call intercepted; window ID (which you can use with selectWindow()) is \"" + windowName + "\"");
587
if (windowName!=null) {
588
openedWindow["seleniumWindowName"] = windowName;
590
selenium.browserbot.openedWindows[windowName] = openedWindow;
594
if (browserVersion.isHTA) {
595
originalOpenReference = 'selenium_originalOpen' + new Date().getTime();
596
newOpenReference = 'selenium_newOpen' + new Date().getTime();
597
var setOriginalRef = "this['" + originalOpenReference + "'] = this.open;";
599
if (windowToModify.eval) {
600
windowToModify.eval(setOriginalRef);
601
windowToModify.open = newOpen;
603
// DGF why can't I eval here? Seems like I'm querying the window at a bad time, maybe?
604
setOriginalRef += "this.open = this['" + newOpenReference + "'];";
605
windowToModify[newOpenReference] = newOpen;
606
windowToModify.setTimeout(setOriginalRef, 0);
609
windowToModify.open = newOpen;
614
* Call the supplied function when a the current page unloads and a new one loads.
615
* This is done by polling continuously until the document changes and is fully loaded.
617
BrowserBot.prototype.modifySeparateTestWindowToDetectPageLoads = function(windowObject) {
618
// Since the unload event doesn't fire in Safari 1.3, we start polling immediately
620
LOG.warn("modifySeparateTestWindowToDetectPageLoads: no windowObject!");
623
if (this._windowClosed(windowObject)) {
624
LOG.info("modifySeparateTestWindowToDetectPageLoads: windowObject was closed");
627
var oldMarker = this.isPollingForLoad(windowObject);
629
LOG.debug("modifySeparateTestWindowToDetectPageLoads: already polling this window: " + oldMarker);
633
var marker = 'selenium' + new Date().getTime();
634
LOG.debug("Starting pollForLoad (" + marker + "): " + windowObject.location);
635
this.pollingForLoad[marker] = true;
636
// if this is a frame, add a load listener, otherwise, attach a poller
637
var frameElement = this._getFrameElement(windowObject);
638
// DGF HTA mode can't attach load listeners to subframes (yuk!)
639
var htaSubFrame = this._isHTASubFrame(windowObject);
640
if (frameElement && !htaSubFrame) {
641
LOG.debug("modifySeparateTestWindowToDetectPageLoads: this window is a frame; attaching a load listener");
642
addLoadListener(frameElement, this.recordPageLoad);
643
frameElement[marker] = true;
644
frameElement["frame"+this.uniqueId] = marker;
645
LOG.debug("dgf this.uniqueId="+this.uniqueId);
646
LOG.debug("dgf marker="+marker);
647
LOG.debug("dgf frameElement['frame'+this.uniqueId]="+frameElement['frame'+this.uniqueId]);
648
frameElement[this.uniqueId] = marker;
649
LOG.debug("dgf frameElement[this.uniqueId]="+frameElement[this.uniqueId]);
651
windowObject.location[marker] = true;
652
windowObject[this.uniqueId] = marker;
653
this.pollForLoad(this.recordPageLoad, windowObject, windowObject.document, windowObject.location, windowObject.location.href, marker);
657
BrowserBot.prototype._isHTASubFrame = function(win) {
658
if (!browserVersion.isHTA) return false;
659
// DGF this is wrong! what if "win" isn't the selected window?
660
return this.isSubFrameSelected;
663
BrowserBot.prototype._getFrameElement = function(win) {
664
var frameElement = null;
667
frameElement = win.frameElement;
672
// on IE, checking frameElement in a pop-up results in a "No such interface supported" exception
673
// but it might have a frame element anyway!
674
var parentContainsIdenticallyNamedFrame = false;
676
parentContainsIdenticallyNamedFrame = win.parent.frames[win.name];
677
} catch (e) {} // this may fail if access is denied to the parent; in that case, assume it's not a pop-up
679
if (parentContainsIdenticallyNamedFrame) {
680
// it can't be a coincidence that the parent has a frame with the same name as myself!
683
result = parentContainsIdenticallyNamedFrame.frameElement;
687
} catch (e) {} // it was worth a try! _getFrameElementsByName is often slow
688
result = this._getFrameElementByName(win.name, win.parent.document, win);
692
LOG.debug("_getFrameElement: frameElement="+frameElement);
694
LOG.debug("frameElement.name="+frameElement.name);
699
BrowserBot.prototype._getFrameElementByName = function(name, doc, win) {
703
frames = doc.getElementsByTagName("iframe");
704
for (i = 0; i < frames.length; i++) {
706
if (frame.name === name) {
710
frames = doc.getElementsByTagName("frame");
711
for (i = 0; i < frames.length; i++) {
713
if (frame.name === name) {
717
// DGF weird; we only call this function when we know the doc contains the frame
718
LOG.warn("_getFrameElementByName couldn't find a frame or iframe; checking every element for the name " + name);
719
return BrowserBot.prototype.locateElementByName(win.name, win.parent.document);
724
* Set up a polling timer that will keep checking the readyState of the document until it's complete.
725
* Since we might call this before the original page is unloaded, we first check to see that the current location
726
* or href is different from the original one.
728
BrowserBot.prototype.pollForLoad = function(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker) {
729
LOG.debug("pollForLoad original (" + marker + "): " + originalHref);
731
if (this._windowClosed(windowObject)) {
732
LOG.debug("pollForLoad WINDOW CLOSED (" + marker + ")");
733
delete this.pollingForLoad[marker];
737
var isSamePage = this._isSamePage(windowObject, originalDocument, originalLocation, originalHref, marker);
738
var rs = this.getReadyState(windowObject, windowObject.document);
740
if (!isSamePage && rs == 'complete') {
741
var currentHref = windowObject.location.href;
742
LOG.debug("pollForLoad FINISHED (" + marker + "): " + rs + " (" + currentHref + ")");
743
delete this.pollingForLoad[marker];
744
this._modifyWindow(windowObject);
745
var newMarker = this.isPollingForLoad(windowObject);
747
LOG.debug("modifyWindow didn't start new poller: " + newMarker);
748
this.modifySeparateTestWindowToDetectPageLoads(windowObject);
750
newMarker = this.isPollingForLoad(windowObject);
751
var currentlySelectedWindow;
752
var currentlySelectedWindowMarker;
753
currentlySelectedWindow =this.getCurrentWindow(true);
754
currentlySelectedWindowMarker = currentlySelectedWindow[this.uniqueId];
756
LOG.debug("pollForLoad (" + marker + ") restarting " + newMarker);
757
if (/(TestRunner-splash|Blank)\.html\?start=true$/.test(currentHref)) {
758
LOG.debug("pollForLoad Oh, it's just the starting page. Never mind!");
759
} else if (currentlySelectedWindowMarker == newMarker) {
760
loadFunction(currentlySelectedWindow);
762
LOG.debug("pollForLoad page load detected in non-current window; ignoring (currentlySelected="+currentlySelectedWindowMarker+", detection in "+newMarker+")");
766
LOG.debug("pollForLoad continue (" + marker + "): " + currentHref);
767
this.reschedulePoller(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker);
769
LOG.debug("Exception during pollForLoad; this should get noticed soon (" + e.message + ")!");
770
//DGF this is supposed to get logged later; log it at debug just in case
772
this.pageLoadError = e;
776
BrowserBot.prototype._isSamePage = function(windowObject, originalDocument, originalLocation, originalHref, marker) {
777
var currentDocument = windowObject.document;
778
var currentLocation = windowObject.location;
779
var currentHref = currentLocation.href
781
var sameDoc = this._isSameDocument(originalDocument, currentDocument);
783
var sameLoc = (originalLocation === currentLocation);
785
// hash marks don't meant the page has loaded, so we need to strip them off if they exist...
786
var currentHash = currentHref.indexOf('#');
787
if (currentHash > 0) {
788
currentHref = currentHref.substring(0, currentHash);
790
var originalHash = originalHref.indexOf('#');
791
if (originalHash > 0) {
792
originalHref = originalHref.substring(0, originalHash);
794
LOG.debug("_isSamePage: currentHref: " + currentHref);
795
LOG.debug("_isSamePage: originalHref: " + originalHref);
797
var sameHref = (originalHref === currentHref);
798
var markedLoc = currentLocation[marker];
800
if (browserVersion.isKonqueror || browserVersion.isSafari) {
801
// the mark disappears too early on these browsers
805
// since this is some _very_ important logic, especially for PI and multiWindow mode, we should log all these out
806
LOG.debug("_isSamePage: sameDoc: " + sameDoc);
807
LOG.debug("_isSamePage: sameLoc: " + sameLoc);
808
LOG.debug("_isSamePage: sameHref: " + sameHref);
809
LOG.debug("_isSamePage: markedLoc: " + markedLoc);
811
return sameDoc && sameLoc && sameHref && markedLoc
814
BrowserBot.prototype._isSameDocument = function(originalDocument, currentDocument) {
815
return originalDocument === currentDocument;
819
BrowserBot.prototype.getReadyState = function(windowObject, currentDocument) {
820
var rs = currentDocument.readyState;
822
if ((this.buttonWindow!=null && this.buttonWindow.document.readyState == null) // not proxy injection mode (and therefore buttonWindow isn't null)
823
|| (top.document.readyState == null)) { // proxy injection mode (and therefore everything's in the top window, but buttonWindow doesn't exist)
824
// uh oh! we're probably on Firefox with no readyState extension installed!
825
// We'll have to just take a guess as to when the document is loaded; this guess
826
// will never be perfect. :-(
827
if (typeof currentDocument.getElementsByTagName != 'undefined'
828
&& typeof currentDocument.getElementById != 'undefined'
829
&& ( currentDocument.getElementsByTagName('body')[0] != null
830
|| currentDocument.body != null )) {
831
if (windowObject.frameElement && windowObject.location.href == "about:blank" && windowObject.frameElement.src != "about:blank") {
832
LOG.info("getReadyState not loaded, frame location was about:blank, but frame src = " + windowObject.frameElement.src);
835
LOG.debug("getReadyState = windowObject.frames.length = " + windowObject.frames.length);
836
for (var i = 0; i < windowObject.frames.length; i++) {
837
LOG.debug("i = " + i);
838
if (this.getReadyState(windowObject.frames[i], windowObject.frames[i].document) != 'complete') {
839
LOG.debug("getReadyState aha! the nested frame " + windowObject.frames[i].name + " wasn't ready!");
846
LOG.debug("pollForLoad readyState was null and DOM appeared to not be ready yet");
850
else if (rs == "loading" && browserVersion.isIE) {
851
LOG.debug("pageUnloading = true!!!!");
852
this.pageUnloading = true;
854
LOG.debug("getReadyState returning " + rs);
858
/** This function isn't used normally, but was the way we used to schedule pollers:
859
asynchronously executed autonomous units. This is deprecated, but remains here
860
for future reference.
862
BrowserBot.prototype.XXXreschedulePoller = function(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker) {
864
window.setTimeout(function() {
865
self.pollForLoad(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker);
869
/** This function isn't used normally, but is useful for debugging asynchronous pollers
870
* To enable it, rename it to "reschedulePoller", so it will override the
871
* existing reschedulePoller function
873
BrowserBot.prototype.XXXreschedulePoller = function(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker) {
874
var doc = this.buttonWindow.document;
875
var button = doc.createElement("button");
876
var buttonName = doc.createTextNode(marker + " - " + windowObject.name);
877
button.appendChild(buttonName);
878
var tools = doc.getElementById("tools");
880
button.onclick = function() {
881
tools.removeChild(button);
882
self.pollForLoad(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker);
884
tools.appendChild(button);
885
window.setTimeout(button.onclick, 500);
888
BrowserBot.prototype.reschedulePoller = function(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker) {
890
var pollerFunction = function() {
891
self.pollForLoad(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker);
893
this.windowPollers.push(pollerFunction);
896
BrowserBot.prototype.runScheduledPollers = function() {
897
LOG.debug("runScheduledPollers");
898
var oldPollers = this.windowPollers;
899
this.windowPollers = new Array();
900
for (var i = 0; i < oldPollers.length; i++) {
901
oldPollers[i].call();
903
LOG.debug("runScheduledPollers DONE");
906
BrowserBot.prototype.isPollingForLoad = function(win) {
908
var frameElement = this._getFrameElement(win);
909
var htaSubFrame = this._isHTASubFrame(win);
910
if (frameElement && !htaSubFrame) {
911
marker = frameElement["frame"+this.uniqueId];
913
marker = win[this.uniqueId];
916
LOG.debug("isPollingForLoad false, missing uniqueId " + this.uniqueId + ": " + marker);
919
if (!this.pollingForLoad[marker]) {
920
LOG.debug("isPollingForLoad false, this.pollingForLoad[" + marker + "]: " + this.pollingForLoad[marker]);
926
BrowserBot.prototype.getWindowByName = function(windowName, doNotModify) {
927
LOG.debug("getWindowByName(" + windowName + ")");
928
// First look in the map of opened windows
929
var targetWindow = this.openedWindows[windowName];
931
targetWindow = this.topWindow[windowName];
933
if (!targetWindow && windowName == "_blank") {
934
for (var winName in this.openedWindows) {
935
// _blank can match selenium_blank*, if it looks like it's OK (valid href, not closed)
936
if (/^selenium_blank/.test(winName)) {
937
targetWindow = this.openedWindows[winName];
940
if (!this._windowClosed(targetWindow)) {
941
ok = targetWindow.location.href;
949
throw new SeleniumError("Window does not exist. If this looks like a Selenium bug, make sure to read http://selenium-core.openqa.org/reference.html#openWindow for potential workarounds.");
951
if (browserVersion.isHTA) {
953
targetWindow.location.href;
955
targetWindow = window.open("", targetWindow.name);
956
this.openedWindows[targetWindow.name] = targetWindow;
960
this._modifyWindow(targetWindow);
966
* Find a window name from the window title.
968
BrowserBot.prototype.getWindowNameByTitle = function(windowTitle) {
969
LOG.debug("getWindowNameByTitle(" + windowTitle + ")");
971
// First look in the map of opened windows and iterate them
972
for (var windowName in this.openedWindows) {
973
var targetWindow = this.openedWindows[windowName];
975
// If the target window's title is our title
977
// TODO implement Pattern Matching here
978
if (!this._windowClosed(targetWindow) &&
979
targetWindow.document.title == windowTitle) {
983
// You'll often get Permission Denied errors here in IE
984
// eh, if we can't read this window's title,
985
// it's probably not available to us right now anyway
990
if (this.topWindow.document.title == windowTitle) {
993
} catch (e) {} // IE Perm denied
995
throw new SeleniumError("Could not find window with title " + windowTitle);
998
BrowserBot.prototype.getCurrentWindow = function(doNotModify) {
999
if (this.proxyInjectionMode) {
1002
var testWindow = this.currentWindow;
1004
this._modifyWindow(testWindow);
1005
LOG.debug("getCurrentWindow newPageLoaded = false");
1006
this.newPageLoaded = false;
1008
testWindow = this._handleClosedSubFrame(testWindow, doNotModify);
1013
* Offer a method the end-user can reliably use to retrieve the current window.
1014
* This should work even for windows with an XPCNativeWrapper. Returns the
1015
* current window object.
1017
BrowserBot.prototype.getUserWindow = function() {
1018
var userWindow = this.getCurrentWindow(true);
1020
if (userWindow.wrappedJSObject) {
1021
userWindow = userWindow.wrappedJSObject;
1027
BrowserBot.prototype._handleClosedSubFrame = function(testWindow, doNotModify) {
1028
if (this.proxyInjectionMode) {
1032
if (this.isSubFrameSelected) {
1034
if (testWindow.parent && testWindow.parent.frames && testWindow.parent.frames.length) {
1035
for (var i = 0; i < testWindow.parent.frames.length; i++) {
1036
if (testWindow.parent.frames[i] == testWindow) {
1043
LOG.warn("Current subframe appears to have closed; selecting top frame");
1044
this.selectFrame("relative=top");
1045
return this.getCurrentWindow(doNotModify);
1047
} else if (this._windowClosed(testWindow)) {
1048
var closedError = new SeleniumError("Current window or frame is closed!");
1049
closedError.windowClosed = true;
1055
BrowserBot.prototype.highlight = function (element, force) {
1056
if (force || this.shouldHighlightLocatedElement) {
1059
} catch (e) {} // DGF element highlighting is low-priority and possibly dangerous
1064
BrowserBot.prototype.setShouldHighlightElement = function (shouldHighlight) {
1065
this.shouldHighlightLocatedElement = shouldHighlight;
1068
/*****************************************************************/
1069
/* BROWSER-SPECIFIC FUNCTIONS ONLY AFTER THIS LINE */
1072
BrowserBot.prototype._registerAllLocatorFunctions = function() {
1073
// TODO - don't do this in the constructor - only needed once ever
1074
this.locationStrategies = {};
1075
for (var functionName in this) {
1076
var result = /^locateElementBy([A-Z].+)$/.exec(functionName);
1077
if (result != null) {
1078
var locatorFunction = this[functionName];
1079
if (typeof(locatorFunction) != 'function') {
1082
// Use a specified prefix in preference to one generated from
1083
// the function name
1084
var locatorPrefix = locatorFunction.prefix || result[1].toLowerCase();
1085
this.locationStrategies[locatorPrefix] = locatorFunction;
1090
* Find a locator based on a prefix.
1092
this.findElementBy = function(locatorType, locator, inDocument, inWindow) {
1093
var locatorFunction = this.locationStrategies[locatorType];
1094
if (! locatorFunction) {
1095
throw new SeleniumError("Unrecognised locator type: '" + locatorType + "'");
1097
return locatorFunction.call(this, locator, inDocument, inWindow);
1101
* The implicit locator, that is used when no prefix is supplied.
1103
this.locationStrategies['implicit'] = function(locator, inDocument, inWindow) {
1104
if (locator.startsWith('//')) {
1105
return this.locateElementByXPath(locator, inDocument, inWindow);
1107
if (locator.startsWith('document.')) {
1108
return this.locateElementByDomTraversal(locator, inDocument, inWindow);
1110
return this.locateElementByIdentifier(locator, inDocument, inWindow);
1114
BrowserBot.prototype.getDocument = function() {
1115
return this.getCurrentWindow().document;
1118
BrowserBot.prototype.getTitle = function() {
1119
var t = this.getDocument().title;
1120
if (typeof(t) == "string") {
1126
BrowserBot.prototype.getCookieByName = function(cookieName, doc) {
1127
if (!doc) doc = this.getDocument();
1128
var ck = doc.cookie;
1129
if (!ck) return null;
1130
var ckPairs = ck.split(/;/);
1131
for (var i = 0; i < ckPairs.length; i++) {
1132
var ckPair = ckPairs[i].trim();
1133
var ckNameValue = ckPair.split(/=/);
1134
var ckName = decodeURIComponent(ckNameValue[0]);
1135
if (ckName === cookieName) {
1136
return decodeURIComponent(ckNameValue[1]);
1142
BrowserBot.prototype.getAllCookieNames = function(doc) {
1143
if (!doc) doc = this.getDocument();
1144
var ck = doc.cookie;
1146
var cookieNames = [];
1147
var ckPairs = ck.split(/;/);
1148
for (var i = 0; i < ckPairs.length; i++) {
1149
var ckPair = ckPairs[i].trim();
1150
var ckNameValue = ckPair.split(/=/);
1151
var ckName = decodeURIComponent(ckNameValue[0]);
1152
cookieNames.push(ckName);
1157
BrowserBot.prototype.deleteCookie = function(cookieName, domain, path, doc) {
1158
if (!doc) doc = this.getDocument();
1159
var expireDateInMilliseconds = (new Date()).getTime() + (-1 * 1000);
1160
var cookie = cookieName + "=deleted; ";
1162
cookie += "path=" + path + "; ";
1165
cookie += "domain=" + domain + "; ";
1167
cookie += "expires=" + new Date(expireDateInMilliseconds).toGMTString();
1168
LOG.debug("Setting cookie to: " + cookie);
1169
doc.cookie = cookie;
1172
/** Try to delete cookie, return false if it didn't work */
1173
BrowserBot.prototype._maybeDeleteCookie = function(cookieName, domain, path, doc) {
1174
this.deleteCookie(cookieName, domain, path, doc);
1175
return (!this.getCookieByName(cookieName, doc));
1179
BrowserBot.prototype._recursivelyDeleteCookieDomains = function(cookieName, domain, path, doc) {
1180
var deleted = this._maybeDeleteCookie(cookieName, domain, path, doc);
1181
if (deleted) return true;
1182
var dotIndex = domain.indexOf(".");
1183
if (dotIndex == 0) {
1184
return this._recursivelyDeleteCookieDomains(cookieName, domain.substring(1), path, doc);
1185
} else if (dotIndex != -1) {
1186
return this._recursivelyDeleteCookieDomains(cookieName, domain.substring(dotIndex), path, doc);
1188
// No more dots; try just not passing in a domain at all
1189
return this._maybeDeleteCookie(cookieName, null, path, doc);
1193
BrowserBot.prototype._recursivelyDeleteCookie = function(cookieName, domain, path, doc) {
1194
var slashIndex = path.lastIndexOf("/");
1195
var finalIndex = path.length-1;
1196
if (slashIndex == finalIndex) {
1199
if (slashIndex != -1) {
1200
deleted = this._recursivelyDeleteCookie(cookieName, domain, path.substring(0, slashIndex+1), doc);
1201
if (deleted) return true;
1203
return this._recursivelyDeleteCookieDomains(cookieName, domain, path, doc);
1206
BrowserBot.prototype.recursivelyDeleteCookie = function(cookieName, domain, path, win) {
1207
if (!win) win = this.getCurrentWindow();
1208
var doc = win.document;
1210
domain = doc.domain;
1213
path = win.location.pathname;
1215
var deleted = this._recursivelyDeleteCookie(cookieName, "." + domain, path, doc);
1216
if (deleted) return;
1217
// Finally try a null path (Try it last because it's uncommon)
1218
deleted = this._recursivelyDeleteCookieDomains(cookieName, "." + domain, null, doc);
1219
if (deleted) return;
1220
throw new SeleniumError("Couldn't delete cookie " + cookieName);
1224
* Finds an element recursively in frames and nested frames
1225
* in the specified document, using various lookup protocols
1227
BrowserBot.prototype.findElementRecursive = function(locatorType, locatorString, inDocument, inWindow) {
1229
var element = this.findElementBy(locatorType, locatorString, inDocument, inWindow);
1230
if (element != null) {
1234
for (var i = 0; i < inWindow.frames.length; i++) {
1235
// On some browsers, the document object is undefined for third-party
1236
// frames. Make sure the document is valid before continuing.
1237
if (inWindow.frames[i].document) {
1238
element = this.findElementRecursive(locatorType, locatorString, inWindow.frames[i].document, inWindow.frames[i]);
1240
if (element != null) {
1248
* Finds an element on the current page, using various lookup protocols
1250
BrowserBot.prototype.findElementOrNull = function(locator, win) {
1251
locator = parse_locator(locator);
1254
win = this.getCurrentWindow();
1256
var element = this.findElementRecursive(locator.type, locator.string, win.document, win);
1258
if (element != null) {
1259
return this.browserbot.highlight(element);
1262
// Element was not found by any locator function.
1266
BrowserBot.prototype.findElement = function(locator, win) {
1267
var element = this.findElementOrNull(locator, win);
1268
if (element == null) throw new SeleniumError("Element " + locator + " not found");
1273
* In non-IE browsers, getElementById() does not search by name. Instead, we
1274
* we search separately by id and name.
1276
BrowserBot.prototype.locateElementByIdentifier = function(identifier, inDocument, inWindow) {
1277
return BrowserBot.prototype.locateElementById(identifier, inDocument, inWindow)
1278
|| BrowserBot.prototype.locateElementByName(identifier, inDocument, inWindow)
1283
* Find the element with id - can't rely on getElementById, coz it returns by name as well in IE..
1285
BrowserBot.prototype.locateElementById = function(identifier, inDocument, inWindow) {
1286
var element = inDocument.getElementById(identifier);
1287
if (element && element.getAttribute('id') === identifier) {
1290
else if (browserVersion.isIE || browserVersion.isOpera) {
1292
var xpath = '/descendant::*[@id=' + identifier.quoteForXPath() + ']';
1293
return BrowserBot.prototype
1294
.locateElementByXPath(xpath, inDocument, inWindow);
1302
* Find an element by name, refined by (optional) element-filter
1305
BrowserBot.prototype.locateElementByName = function(locator, document, inWindow) {
1306
var elements = document.getElementsByTagName("*");
1308
var filters = locator.split(' ');
1309
filters[0] = 'name=' + filters[0];
1311
while (filters.length) {
1312
var filter = filters.shift();
1313
elements = this.selectElements(filter, elements, 'value');
1316
if (elements.length > 0) {
1323
* Finds an element using by evaluating the specfied string.
1325
BrowserBot.prototype.locateElementByDomTraversal = function(domTraversal, document, window) {
1327
var browserbot = this.browserbot;
1330
element = eval(domTraversal);
1341
BrowserBot.prototype.locateElementByDomTraversal.prefix = "dom";
1344
* Finds an element identified by the xpath expression. Expressions _must_
1347
BrowserBot.prototype.locateElementByXPath = function(xpath, inDocument, inWindow) {
1348
var results = eval_xpath(xpath, inDocument, {
1349
returnOnFirstMatch : true,
1350
ignoreAttributesWithoutValue: this.ignoreAttributesWithoutValue,
1351
allowNativeXpath : this.allowNativeXpath,
1352
xpathLibrary : this.xpathLibrary,
1353
namespaceResolver : this._namespaceResolver
1355
return (results.length > 0) ? results[0] : null;
1358
BrowserBot.prototype._namespaceResolver = function(prefix) {
1359
if (prefix == 'html' || prefix == 'xhtml' || prefix == 'x') {
1360
return 'http://www.w3.org/1999/xhtml';
1361
} else if (prefix == 'mathml') {
1362
return 'http://www.w3.org/1998/Math/MathML';
1364
throw new Error("Unknown namespace: " + prefix + ".");
1369
* Returns the number of xpath results.
1371
BrowserBot.prototype.evaluateXPathCount = function(xpath, inDocument) {
1372
var results = eval_xpath(xpath, inDocument, {
1373
ignoreAttributesWithoutValue: this.ignoreAttributesWithoutValue,
1374
allowNativeXpath : this.allowNativeXpath,
1375
xpathLibrary : this.xpathLibrary,
1376
namespaceResolver : this._namespaceResolver
1378
return results.length;
1382
* Finds a link element with text matching the expression supplied. Expressions must
1383
* begin with "link:".
1385
BrowserBot.prototype.locateElementByLinkText = function(linkText, inDocument, inWindow) {
1386
var links = inDocument.getElementsByTagName('a');
1387
for (var i = 0; i < links.length; i++) {
1388
var element = links[i];
1389
if (PatternMatcher.matches(linkText, getText(element))) {
1395
BrowserBot.prototype.locateElementByLinkText.prefix = "link";
1398
* Returns an attribute based on an attribute locator. This is made up of an element locator
1399
* suffixed with @attribute-name.
1401
BrowserBot.prototype.findAttribute = function(locator) {
1402
// Split into locator + attributeName
1403
var attributePos = locator.lastIndexOf("@");
1404
var elementLocator = locator.slice(0, attributePos);
1405
var attributeName = locator.slice(attributePos + 1);
1407
// Find the element.
1408
var element = this.findElement(elementLocator);
1410
// Handle missing "class" attribute in IE.
1411
if (browserVersion.isIE && attributeName == "class") {
1412
attributeName = "className";
1415
// Get the attribute value.
1416
var attributeValue = element.getAttribute(attributeName);
1418
// IE returns an object for the "style" attribute
1419
if (attributeName == 'style' && typeof(attributeValue) != 'string') {
1420
attributeValue = attributeValue.cssText;
1423
return attributeValue ? attributeValue.toString() : null;
1427
* Select the specified option and trigger the relevant events of the element.
1429
BrowserBot.prototype.selectOption = function(element, optionToSelect) {
1430
triggerEvent(element, 'focus', false);
1431
var changed = false;
1432
for (var i = 0; i < element.options.length; i++) {
1433
var option = element.options[i];
1434
if (option.selected && option != optionToSelect) {
1435
option.selected = false;
1438
else if (!option.selected && option == optionToSelect) {
1439
option.selected = true;
1445
triggerEvent(element, 'change', true);
1450
* Select the specified option and trigger the relevant events of the element.
1452
BrowserBot.prototype.addSelection = function(element, option) {
1453
this.checkMultiselect(element);
1454
triggerEvent(element, 'focus', false);
1455
if (!option.selected) {
1456
option.selected = true;
1457
triggerEvent(element, 'change', true);
1462
* Select the specified option and trigger the relevant events of the element.
1464
BrowserBot.prototype.removeSelection = function(element, option) {
1465
this.checkMultiselect(element);
1466
triggerEvent(element, 'focus', false);
1467
if (option.selected) {
1468
option.selected = false;
1469
triggerEvent(element, 'change', true);
1473
BrowserBot.prototype.checkMultiselect = function(element) {
1474
if (!element.multiple)
1476
throw new SeleniumError("Not a multi-select");
1481
BrowserBot.prototype.replaceText = function(element, stringValue) {
1482
triggerEvent(element, 'focus', false);
1483
triggerEvent(element, 'select', true);
1484
var maxLengthAttr = element.getAttribute("maxLength");
1485
var actualValue = stringValue;
1486
if (maxLengthAttr != null) {
1487
var maxLength = parseInt(maxLengthAttr);
1488
if (stringValue.length > maxLength) {
1489
actualValue = stringValue.substr(0, maxLength);
1493
if (getTagName(element) == "body") {
1494
if (element.ownerDocument && element.ownerDocument.designMode) {
1495
var designMode = new String(element.ownerDocument.designMode).toLowerCase();
1496
if (designMode = "on") {
1497
// this must be a rich text control!
1498
element.innerHTML = actualValue;
1502
element.value = actualValue;
1504
// DGF this used to be skipped in chrome URLs, but no longer. Is xpcnativewrappers to blame?
1506
triggerEvent(element, 'change', true);
1510
BrowserBot.prototype.submit = function(formElement) {
1511
var actuallySubmit = true;
1512
this._modifyElementTarget(formElement);
1513
if (formElement.onsubmit) {
1514
if (browserVersion.isHTA) {
1515
// run the code in the correct window so alerts are handled correctly even in HTA mode
1516
var win = this.browserbot.getCurrentWindow();
1517
var now = new Date().getTime();
1518
var marker = 'marker' + now;
1519
win[marker] = formElement;
1520
win.setTimeout("var actuallySubmit = "+marker+".onsubmit();" +
1521
"if (actuallySubmit) { " +
1522
marker+".submit(); " +
1523
"if ("+marker+".target && !/^_/.test("+marker+".target)) {"+
1524
"window.open('', "+marker+".target);"+
1528
// pause for up to 2s while this command runs
1529
var terminationCondition = function () {
1530
return !win[marker];
1532
return Selenium.decorateFunctionWithTimeout(terminationCondition, 2000);
1534
actuallySubmit = formElement.onsubmit();
1535
if (actuallySubmit) {
1536
formElement.submit();
1537
if (formElement.target && !/^_/.test(formElement.target)) {
1538
this.browserbot.openWindow('', formElement.target);
1543
formElement.submit();
1547
BrowserBot.prototype.clickElement = function(element, clientX, clientY) {
1548
this._fireEventOnElement("click", element, clientX, clientY);
1551
BrowserBot.prototype.doubleClickElement = function(element, clientX, clientY) {
1552
this._fireEventOnElement("dblclick", element, clientX, clientY);
1555
// The contextmenu event is fired when the user right-clicks to open the context menu
1556
BrowserBot.prototype.contextMenuOnElement = function(element, clientX, clientY) {
1557
this._fireEventOnElement("contextmenu", element, clientX, clientY);
1560
BrowserBot.prototype._modifyElementTarget = function(element) {
1561
if (element.target) {
1562
if (element.target == "_blank" || /^selenium_blank/.test(element.target) ) {
1563
var tagName = getTagName(element);
1564
if (tagName == "a" || tagName == "form") {
1565
var newTarget = "selenium_blank" + Math.round(100000 * Math.random());
1566
LOG.warn("Link has target '_blank', which is not supported in Selenium! Randomizing target to be: " + newTarget);
1567
this.browserbot.openWindow('', newTarget);
1568
element.target = newTarget;
1575
BrowserBot.prototype._handleClickingImagesInsideLinks = function(targetWindow, element) {
1576
var itrElement = element;
1577
while (itrElement != null) {
1578
if (itrElement.href) {
1579
targetWindow.location.href = itrElement.href;
1582
itrElement = itrElement.parentNode;
1586
BrowserBot.prototype._getTargetWindow = function(element) {
1587
var targetWindow = element.ownerDocument.defaultView;
1588
if (element.target) {
1589
targetWindow = this._getFrameFromGlobal(element.target);
1591
return targetWindow;
1594
BrowserBot.prototype._getFrameFromGlobal = function(target) {
1596
if (target == "_self") {
1597
return this.getCurrentWindow();
1599
if (target == "_top") {
1600
return this.topFrame;
1601
} else if (target == "_parent") {
1602
return this.getCurrentWindow().parent;
1603
} else if (target == "_blank") {
1604
// TODO should this set cleverer window defaults?
1605
return this.getCurrentWindow().open('', '_blank');
1607
var frameElement = this.findElementBy("implicit", target, this.topFrame.document, this.topFrame);
1609
return frameElement.contentWindow;
1611
var win = this.getWindowByName(target);
1612
if (win) return win;
1613
return this.getCurrentWindow().open('', target);
1617
BrowserBot.prototype.bodyText = function() {
1618
if (!this.getDocument().body) {
1619
throw new SeleniumError("Couldn't access document.body. Is this HTML page fully loaded?");
1621
return getText(this.getDocument().body);
1624
BrowserBot.prototype.getAllButtons = function() {
1625
var elements = this.getDocument().getElementsByTagName('input');
1628
for (var i = 0; i < elements.length; i++) {
1629
if (elements[i].type == 'button' || elements[i].type == 'submit' || elements[i].type == 'reset') {
1630
result.push(elements[i].id);
1638
BrowserBot.prototype.getAllFields = function() {
1639
var elements = this.getDocument().getElementsByTagName('input');
1642
for (var i = 0; i < elements.length; i++) {
1643
if (elements[i].type == 'text') {
1644
result.push(elements[i].id);
1651
BrowserBot.prototype.getAllLinks = function() {
1652
var elements = this.getDocument().getElementsByTagName('a');
1655
for (var i = 0; i < elements.length; i++) {
1656
result.push(elements[i].id);
1662
function isDefined(value) {
1663
return typeof(value) != undefined;
1666
BrowserBot.prototype.goBack = function() {
1667
this.getCurrentWindow().history.back();
1670
BrowserBot.prototype.goForward = function() {
1671
this.getCurrentWindow().history.forward();
1674
BrowserBot.prototype.close = function() {
1675
if (browserVersion.isIE) {
1676
// fix "do you want to close this window" warning in IE
1677
// You can only close windows that you have opened.
1678
// So, let's "open" it.
1680
this.topFrame.name=new Date().getTime();
1681
window.open("", this.topFrame.name, "");
1682
this.topFrame.close();
1686
if (browserVersion.isChrome || browserVersion.isSafari || browserVersion.isOpera) {
1687
this.topFrame.close();
1689
this.getCurrentWindow().eval("window.top.close();");
1693
BrowserBot.prototype.refresh = function() {
1694
this.getCurrentWindow().location.reload(true);
1698
* Refine a list of elements using a filter.
1700
BrowserBot.prototype.selectElementsBy = function(filterType, filter, elements) {
1701
var filterFunction = BrowserBot.filterFunctions[filterType];
1702
if (! filterFunction) {
1703
throw new SeleniumError("Unrecognised element-filter type: '" + filterType + "'");
1706
return filterFunction(filter, elements);
1709
BrowserBot.filterFunctions = {};
1711
BrowserBot.filterFunctions.name = function(name, elements) {
1712
var selectedElements = [];
1713
for (var i = 0; i < elements.length; i++) {
1714
if (elements[i].name === name) {
1715
selectedElements.push(elements[i]);
1718
return selectedElements;
1721
BrowserBot.filterFunctions.value = function(value, elements) {
1722
var selectedElements = [];
1723
for (var i = 0; i < elements.length; i++) {
1724
if (elements[i].value === value) {
1725
selectedElements.push(elements[i]);
1728
return selectedElements;
1731
BrowserBot.filterFunctions.index = function(index, elements) {
1732
index = Number(index);
1733
if (isNaN(index) || index < 0) {
1734
throw new SeleniumError("Illegal Index: " + index);
1736
if (elements.length <= index) {
1737
throw new SeleniumError("Index out of range: " + index);
1739
return [elements[index]];
1742
BrowserBot.prototype.selectElements = function(filterExpr, elements, defaultFilterType) {
1744
var filterType = (defaultFilterType || 'value');
1746
// If there is a filter prefix, use the specified strategy
1747
var result = filterExpr.match(/^([A-Za-z]+)=(.+)/);
1749
filterType = result[1].toLowerCase();
1750
filterExpr = result[2];
1753
return this.selectElementsBy(filterType, filterExpr, elements);
1757
* Find an element by class
1759
BrowserBot.prototype.locateElementByClass = function(locator, document) {
1760
return elementFindFirstMatchingChild(document,
1762
return element.className == locator
1768
* Find an element by alt
1770
BrowserBot.prototype.locateElementByAlt = function(locator, document) {
1771
return elementFindFirstMatchingChild(document,
1773
return element.alt == locator
1779
* Find an element by css selector
1781
BrowserBot.prototype.locateElementByCss = function(locator, document) {
1782
var elements = eval_css(locator, document);
1783
if (elements.length != 0)
1789
* This function is responsible for mapping a UI specifier string to an element
1790
* on the page, and returning it. If no element is found, null is returned.
1791
* Returning null on failure to locate the element is part of the undocumented
1792
* API for locator strategies.
1794
BrowserBot.prototype.locateElementByUIElement = function(locator, inDocument) {
1795
// offset locators are delimited by "->", which is much simpler than the
1796
// previous scheme involving detecting the close-paren.
1797
var locators = locator.split(/->/, 2);
1799
var locatedElement = null;
1800
var pageElements = UIMap.getInstance()
1801
.getPageElements(locators[0], inDocument);
1803
if (locators.length > 1) {
1804
for (var i = 0; i < pageElements.length; ++i) {
1805
var locatedElements = eval_locator(locators[1], inDocument,
1807
if (locatedElements.length) {
1808
locatedElement = locatedElements[0];
1813
else if (pageElements.length) {
1814
locatedElement = pageElements[0];
1817
return locatedElement;
1820
BrowserBot.prototype.locateElementByUIElement.prefix = 'ui';
1822
// define a function used to compare the result of a close UI element
1823
// match with the actual interacted element. If they are close enough
1824
// according to the heuristic, consider them a match.
1826
* A heuristic function for comparing a node with a target node. Typically the
1827
* node is specified in a UI element definition, while the target node is
1828
* returned by the recorder as the leaf element which had some event enacted
1829
* upon it. This particular heuristic covers the case where the anchor element
1830
* contains other inline tags, such as "em" or "img".
1832
* @param node the node being compared to the target node
1833
* @param target the target node
1834
* @return true if node equals target, or if node is a link
1835
* element and target is its descendant, or if node has
1836
* an onclick attribute and target is its descendant.
1839
BrowserBot.prototype.locateElementByUIElement.is_fuzzy_match = function(node, target) {
1843
((node.nodeName == 'A' || node.onclick) && is_ancestor(node, target))
1852
/*****************************************************************/
1853
/* BROWSER-SPECIFIC FUNCTIONS ONLY AFTER THIS LINE */
1855
function MozillaBrowserBot(frame) {
1856
BrowserBot.call(this, frame);
1858
objectExtend(MozillaBrowserBot.prototype, BrowserBot.prototype);
1860
function KonquerorBrowserBot(frame) {
1861
BrowserBot.call(this, frame);
1863
objectExtend(KonquerorBrowserBot.prototype, BrowserBot.prototype);
1865
KonquerorBrowserBot.prototype.setIFrameLocation = function(iframe, location) {
1866
// Window doesn't fire onload event when setting src to the current value,
1867
// so we set it to blank first.
1868
iframe.src = "about:blank";
1869
iframe.src = location;
1872
KonquerorBrowserBot.prototype.setOpenLocation = function(win, loc) {
1873
// Window doesn't fire onload event when setting src to the current value,
1874
// so we just refresh in that case instead.
1875
loc = absolutify(loc, this.baseUrl);
1876
loc = canonicalize(loc);
1877
var startUrl = win.location.href;
1878
if ("about:blank" != win.location.href) {
1879
var startLoc = parseUrl(win.location.href);
1880
startLoc.hash = null;
1881
var startUrl = reassembleLocation(startLoc);
1883
LOG.debug("startUrl="+startUrl);
1884
LOG.debug("win.location.href="+win.location.href);
1885
LOG.debug("loc="+loc);
1886
if (startUrl == loc) {
1887
LOG.debug("opening exact same location");
1890
LOG.debug("locations differ");
1891
win.location.href = loc;
1893
// force the current polling thread to detect a page load
1894
var marker = this.isPollingForLoad(win);
1896
delete win.location[marker];
1900
KonquerorBrowserBot.prototype._isSameDocument = function(originalDocument, currentDocument) {
1901
// under Konqueror, there may be this case:
1902
// originalDocument and currentDocument are different objects
1903
// while their location are same.
1904
if (originalDocument) {
1905
return originalDocument.location == currentDocument.location
1907
return originalDocument === currentDocument;
1911
function SafariBrowserBot(frame) {
1912
BrowserBot.call(this, frame);
1914
objectExtend(SafariBrowserBot.prototype, BrowserBot.prototype);
1916
SafariBrowserBot.prototype.setIFrameLocation = KonquerorBrowserBot.prototype.setIFrameLocation;
1917
SafariBrowserBot.prototype.setOpenLocation = KonquerorBrowserBot.prototype.setOpenLocation;
1920
function OperaBrowserBot(frame) {
1921
BrowserBot.call(this, frame);
1923
objectExtend(OperaBrowserBot.prototype, BrowserBot.prototype);
1924
OperaBrowserBot.prototype.setIFrameLocation = function(iframe, location) {
1925
if (iframe.src == location) {
1926
iframe.src = location + '?reload';
1928
iframe.src = location;
1932
function IEBrowserBot(frame) {
1933
BrowserBot.call(this, frame);
1935
objectExtend(IEBrowserBot.prototype, BrowserBot.prototype);
1937
IEBrowserBot.prototype._handleClosedSubFrame = function(testWindow, doNotModify) {
1938
if (this.proxyInjectionMode) {
1943
testWindow.location.href;
1944
this.permDenied = 0;
1948
if (this._windowClosed(testWindow) || this.permDenied > 4) {
1949
if (this.isSubFrameSelected) {
1950
LOG.warn("Current subframe appears to have closed; selecting top frame");
1951
this.selectFrame("relative=top");
1952
return this.getCurrentWindow(doNotModify);
1954
var closedError = new SeleniumError("Current window or frame is closed!");
1955
closedError.windowClosed = true;
1962
IEBrowserBot.prototype.modifyWindowToRecordPopUpDialogs = function(windowToModify, browserBot) {
1963
BrowserBot.prototype.modifyWindowToRecordPopUpDialogs(windowToModify, browserBot);
1965
// we will call the previous version of this method from within our own interception
1966
oldShowModalDialog = windowToModify.showModalDialog;
1968
windowToModify.showModalDialog = function(url, args, features) {
1969
// Get relative directory to where TestRunner.html lives
1970
// A risky assumption is that the user's TestRunner is named TestRunner.html
1971
var doc_location = document.location.toString();
1972
var end_of_base_ref = doc_location.indexOf('TestRunner.html');
1973
var base_ref = doc_location.substring(0, end_of_base_ref);
1974
var runInterval = '';
1976
// Only set run interval if options is defined
1977
if (typeof(window.runOptions) != 'undefined') {
1978
runInterval = "&runInterval=" + runOptions.runInterval;
1981
var testRunnerURL = "TestRunner.html?auto=true&singletest="
1982
+ escape(browserBot.modalDialogTest)
1986
var fullURL = base_ref + testRunnerURL;
1987
browserBot.modalDialogTest = null;
1989
// If using proxy injection mode
1990
if (this.proxyInjectionMode) {
1991
var sessionId = runOptions.getSessionId();
1992
if (sessionId == undefined) {
1993
sessionId = injectedSessionId;
1995
if (sessionId != undefined) {
1996
LOG.debug("Invoking showModalDialog and injecting URL " + fullURL);
2000
var returnValue = oldShowModalDialog(fullURL, args, features);
2005
IEBrowserBot.prototype.modifySeparateTestWindowToDetectPageLoads = function(windowObject) {
2006
this.pageUnloading = false;
2008
var pageUnloadDetector = function() {
2009
self.pageUnloading = true;
2011
windowObject.attachEvent("onbeforeunload", pageUnloadDetector);
2012
BrowserBot.prototype.modifySeparateTestWindowToDetectPageLoads.call(this, windowObject);
2015
IEBrowserBot.prototype.pollForLoad = function(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker) {
2016
LOG.debug("IEBrowserBot.pollForLoad: " + marker);
2017
if (!this.permDeniedCount[marker]) this.permDeniedCount[marker] = 0;
2018
BrowserBot.prototype.pollForLoad.call(this, loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker);
2019
if (this.pageLoadError) {
2020
if (this.pageUnloading) {
2022
LOG.debug("pollForLoad UNLOADING (" + marker + "): caught exception while firing events on unloading page: " + this.pageLoadError.message);
2023
this.reschedulePoller(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker);
2024
this.pageLoadError = null;
2026
} else if (((this.pageLoadError.message == "Permission denied") || (/^Access is denied/.test(this.pageLoadError.message)))
2027
&& this.permDeniedCount[marker]++ < 8) {
2028
if (this.permDeniedCount[marker] > 4) {
2029
var canAccessThisWindow;
2030
var canAccessCurrentlySelectedWindow;
2032
windowObject.location.href;
2033
canAccessThisWindow = true;
2036
this.getCurrentWindow(true).location.href;
2037
canAccessCurrentlySelectedWindow = true;
2039
if (canAccessCurrentlySelectedWindow & !canAccessThisWindow) {
2040
LOG.debug("pollForLoad (" + marker + ") ABORTING: " + this.pageLoadError.message + " (" + this.permDeniedCount[marker] + "), but the currently selected window is fine");
2041
// returning without rescheduling
2042
this.pageLoadError = null;
2048
LOG.debug("pollForLoad (" + marker + "): " + this.pageLoadError.message + " (" + this.permDeniedCount[marker] + "), waiting to see if it goes away");
2049
this.reschedulePoller(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker);
2050
this.pageLoadError = null;
2053
//handy for debugging!
2054
//throw this.pageLoadError;
2058
IEBrowserBot.prototype._windowClosed = function(win) {
2061
// frame windows claim to be non-closed when their parents are closed
2062
// but you can't access their document objects in that case
2067
if (de.message == "Permission denied") {
2068
// the window is probably unloading, which means it's probably not closed yet
2071
else if (/^Access is denied/.test(de.message)) {
2072
// rare variation on "Permission denied"?
2073
LOG.debug("IEBrowserBot.windowClosed: got " + de.message + " (this.pageUnloading=" + this.pageUnloading + "); assuming window is unloading, probably not closed yet");
2076
// this is probably one of those frame window situations
2077
LOG.debug("IEBrowserBot.windowClosed: couldn't read win.document, assume closed: " + de.message + " (this.pageUnloading=" + this.pageUnloading + ")");
2083
LOG.debug("IEBrowserBot.windowClosed: win.closed was null, assuming closed");
2088
LOG.debug("IEBrowserBot._windowClosed: Got an exception trying to read win.closed; we'll have to take a guess!");
2090
if (browserVersion.isHTA) {
2091
if (e.message == "Permission denied") {
2092
// the window is probably unloading, which means it's not closed yet
2095
// there's a good chance that we've lost contact with the window object if it is closed
2099
// the window is probably unloading, which means it's not closed yet
2106
* In IE, getElementById() also searches by name - this is an optimisation for IE.
2108
IEBrowserBot.prototype.locateElementByIdentifer = function(identifier, inDocument, inWindow) {
2109
return inDocument.getElementById(identifier);
2112
SafariBrowserBot.prototype.modifyWindowToRecordPopUpDialogs = function(windowToModify, browserBot) {
2113
BrowserBot.prototype.modifyWindowToRecordPopUpDialogs(windowToModify, browserBot);
2115
var originalOpen = windowToModify.open;
2117
* Safari seems to be broken, so that when we manually trigger the onclick method
2118
* of a button/href, any window.open calls aren't resolved relative to the app location.
2119
* So here we replace the open() method with one that does resolve the url correctly.
2121
windowToModify.open = function(url, windowName, windowFeatures, replaceFlag) {
2123
if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("/")) {
2124
return originalOpen(url, windowName, windowFeatures, replaceFlag);
2127
// Reduce the current path to the directory
2128
var currentPath = windowToModify.location.pathname || "/";
2129
currentPath = currentPath.replace(/\/[^\/]*$/, "/");
2131
// Remove any leading "./" from the new url.
2132
url = url.replace(/^\.\//, "");
2134
newUrl = currentPath + url;
2136
var openedWindow = originalOpen(newUrl, windowName, windowFeatures, replaceFlag);
2137
LOG.debug("window.open call intercepted; window ID (which you can use with selectWindow()) is \"" + windowName + "\"");
2138
if (windowName!=null) {
2139
openedWindow["seleniumWindowName"] = windowName;
2141
return openedWindow;
2145
MozillaBrowserBot.prototype._fireEventOnElement = function(eventType, element, clientX, clientY) {
2146
var win = this.getCurrentWindow();
2147
triggerEvent(element, 'focus', false);
2149
// Add an event listener that detects if the default action has been prevented.
2150
// (This is caused by a javascript onclick handler returning false)
2151
// we capture the whole event, rather than the getPreventDefault() state at the time,
2152
// because we need to let the entire event bubbling and capturing to go through
2153
// before making a decision on whether we should force the href
2154
var savedEvent = null;
2156
element.addEventListener(eventType, function(evt) {
2160
this._modifyElementTarget(element);
2162
// Trigger the event.
2163
this.browserbot.triggerMouseEvent(element, eventType, true, clientX, clientY);
2165
if (this._windowClosed(win)) {
2169
// Perform the link action if preventDefault was set.
2170
// In chrome URL, the link action is already executed by triggerMouseEvent.
2171
if (!browserVersion.isChrome && savedEvent != null && !savedEvent.getPreventDefault()) {
2172
var targetWindow = this.browserbot._getTargetWindow(element);
2174
targetWindow.location.href = element.href;
2176
this.browserbot._handleClickingImagesInsideLinks(targetWindow, element);
2183
OperaBrowserBot.prototype._fireEventOnElement = function(eventType, element, clientX, clientY) {
2184
var win = this.getCurrentWindow();
2185
triggerEvent(element, 'focus', false);
2187
this._modifyElementTarget(element);
2189
// Trigger the click event.
2190
this.browserbot.triggerMouseEvent(element, eventType, true, clientX, clientY);
2192
if (this._windowClosed(win)) {
2199
KonquerorBrowserBot.prototype._fireEventOnElement = function(eventType, element, clientX, clientY) {
2200
var win = this.getCurrentWindow();
2201
triggerEvent(element, 'focus', false);
2203
this._modifyElementTarget(element);
2205
if (element[eventType]) {
2206
element[eventType]();
2209
this.browserbot.triggerMouseEvent(element, eventType, true, clientX, clientY);
2212
if (this._windowClosed(win)) {
2218
SafariBrowserBot.prototype._fireEventOnElement = function(eventType, element, clientX, clientY) {
2219
triggerEvent(element, 'focus', false);
2220
var wasChecked = element.checked;
2222
this._modifyElementTarget(element);
2224
// For form element it is simple.
2225
if (element[eventType]) {
2226
element[eventType]();
2228
// For links and other elements, event emulation is required.
2230
var targetWindow = this.browserbot._getTargetWindow(element);
2231
// todo: deal with anchors?
2232
this.browserbot.triggerMouseEvent(element, eventType, true, clientX, clientY);
2238
SafariBrowserBot.prototype.refresh = function() {
2239
var win = this.getCurrentWindow();
2240
if (win.location.hash) {
2241
// DGF Safari refuses to refresh when there's a hash symbol in the URL
2242
win.location.hash = "";
2243
var actuallyReload = function() {
2244
win.location.reload(true);
2246
window.setTimeout(actuallyReload, 1);
2248
win.location.reload(true);
2252
IEBrowserBot.prototype._fireEventOnElement = function(eventType, element, clientX, clientY) {
2253
var win = this.getCurrentWindow();
2254
triggerEvent(element, 'focus', false);
2256
var wasChecked = element.checked;
2258
// Set a flag that records if the page will unload - this isn't always accurate, because
2259
// <a href="javascript:alert('foo'):"> triggers the onbeforeunload event, even thought the page won't unload
2260
var pageUnloading = false;
2261
var pageUnloadDetector = function() {
2262
pageUnloading = true;
2264
win.attachEvent("onbeforeunload", pageUnloadDetector);
2265
this._modifyElementTarget(element);
2266
if (element[eventType]) {
2267
element[eventType]();
2270
this.browserbot.triggerMouseEvent(element, eventType, true, clientX, clientY);
2274
// If the page is going to unload - still attempt to fire any subsequent events.
2275
// However, we can't guarantee that the page won't unload half way through, so we need to handle exceptions.
2277
win.detachEvent("onbeforeunload", pageUnloadDetector);
2279
if (this._windowClosed(win)) {
2283
// Onchange event is not triggered automatically in IE.
2284
if (isDefined(element.checked) && wasChecked != element.checked) {
2285
triggerEvent(element, 'change', true);
2290
// If the page is unloading, we may get a "Permission denied" or "Unspecified error".
2291
// Just ignore it, because the document may have unloaded.
2292
if (pageUnloading) {
2293
LOG.logHook = function() {
2295
LOG.warn("Caught exception when firing events on unloading page: " + e.message);
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.
19
* This script provides the Javascript API to drive the test application contained within
22
* Add support for more events (keyboard and mouse)
23
* Allow to switch "user-entry" mode from mouse-based to keyboard-based, firing different
24
* events in different modes.
27
// The window to which the commands will be sent. For example, to click on a
28
// popup window, first select that window, and then do a normal click command.
29
var BrowserBot = function(topLevelApplicationWindow) {
30
this.topWindow = topLevelApplicationWindow;
31
this.topFrame = this.topWindow;
32
this.baseUrl=window.location.href;
34
// the buttonWindow is the Selenium window
35
// it contains the Run/Pause buttons... this should *not* be the AUT window
36
this.buttonWindow = window;
37
this.currentWindow = this.topWindow;
38
this.currentWindowName = null;
39
this.allowNativeXpath = true;
40
this.xpathLibrary = this.defaultXpathLibrary = 'ajaxslt' // change to "javascript-xpath" for the newer, faster engine
42
// We need to know this in advance, in case the frame closes unexpectedly
43
this.isSubFrameSelected = false;
45
this.altKeyDown = false;
46
this.controlKeyDown = false;
47
this.shiftKeyDown = false;
48
this.metaKeyDown = false;
50
this.modalDialogTest = null;
51
this.recordedAlerts = new Array();
52
this.recordedConfirmations = new Array();
53
this.recordedPrompts = new Array();
54
this.openedWindows = {};
55
this.nextConfirmResult = true;
56
this.nextPromptResult = '';
57
this.newPageLoaded = false;
58
this.pageLoadError = null;
60
this.shouldHighlightLocatedElement = false;
62
this.uniqueId = "seleniumMarker" + new Date().getTime();
63
this.pollingForLoad = new Object();
64
this.permDeniedCount = new Object();
65
this.windowPollers = new Array();
66
// DGF for backwards compatibility
67
this.browserbot = this;
71
objectExtend(this, PageBot.prototype);
72
this._registerAllLocatorFunctions();
74
this.recordPageLoad = function(elementOrWindow) {
75
LOG.debug("Page load detected");
77
if (elementOrWindow.location && elementOrWindow.location.href) {
78
LOG.debug("Page load location=" + elementOrWindow.location.href);
79
} else if (elementOrWindow.contentWindow && elementOrWindow.contentWindow.location && elementOrWindow.contentWindow.location.href) {
80
LOG.debug("Page load location=" + elementOrWindow.contentWindow.location.href);
82
LOG.debug("Page load location unknown, current window location=" + this.getCurrentWindow(true).location);
85
LOG.error("Caught an exception attempting to log location; this should get noticed soon!");
87
self.pageLoadError = e;
90
self.newPageLoaded = true;
93
this.isNewPageLoaded = function() {
94
if (this.pageLoadError) {
95
LOG.error("isNewPageLoaded found an old pageLoadError");
96
var e = this.pageLoadError;
97
this.pageLoadError = null;
100
return self.newPageLoaded;
105
// DGF PageBot exists for backwards compatibility with old user-extensions
106
var PageBot = function(){};
108
BrowserBot.createForWindow = function(window, proxyInjectionMode) {
110
LOG.debug('createForWindow');
111
LOG.debug("browserName: " + browserVersion.name);
112
LOG.debug("userAgent: " + navigator.userAgent);
113
if (browserVersion.isIE) {
114
browserbot = new IEBrowserBot(window);
116
else if (browserVersion.isKonqueror) {
117
browserbot = new KonquerorBrowserBot(window);
119
else if (browserVersion.isOpera) {
120
browserbot = new OperaBrowserBot(window);
122
else if (browserVersion.isSafari) {
123
browserbot = new SafariBrowserBot(window);
126
// Use mozilla by default
127
browserbot = new MozillaBrowserBot(window);
129
// getCurrentWindow has the side effect of modifying it to handle page loads etc
130
browserbot.proxyInjectionMode = proxyInjectionMode;
131
browserbot.getCurrentWindow(); // for modifyWindow side effect. This is not a transparent style
135
// todo: rename? This doesn't actually "do" anything.
136
BrowserBot.prototype.doModalDialogTest = function(test) {
137
this.modalDialogTest = test;
140
BrowserBot.prototype.cancelNextConfirmation = function(result) {
141
this.nextConfirmResult = result;
144
BrowserBot.prototype.setNextPromptResult = function(result) {
145
this.nextPromptResult = result;
148
BrowserBot.prototype.hasAlerts = function() {
149
return (this.recordedAlerts.length > 0);
152
BrowserBot.prototype.relayBotToRC = function(s) {
153
// DGF need to do this funny trick to see if we're in PI mode, because
154
// "this" might be the window, rather than the browserbot (e.g. during window.alert)
155
var piMode = this.proxyInjectionMode;
157
if (typeof(selenium) != "undefined") {
158
piMode = selenium.browserbot && selenium.browserbot.proxyInjectionMode;
162
this.relayToRC("selenium." + s);
166
BrowserBot.prototype.relayToRC = function(name) {
167
var object = eval(name);
168
var s = 'state:' + serializeObject(name, object) + "\n";
169
sendToRC(s,"state=true");
172
BrowserBot.prototype.resetPopups = function() {
173
this.recordedAlerts = [];
174
this.recordedConfirmations = [];
175
this.recordedPrompts = [];
178
BrowserBot.prototype.getNextAlert = function() {
179
var t = this.recordedAlerts.shift();
181
t = t.replace(/\n/g, " "); // because Selenese loses \n's when retrieving text from HTML table
183
this.relayBotToRC("browserbot.recordedAlerts");
187
BrowserBot.prototype.hasConfirmations = function() {
188
return (this.recordedConfirmations.length > 0);
191
BrowserBot.prototype.getNextConfirmation = function() {
192
var t = this.recordedConfirmations.shift();
193
this.relayBotToRC("browserbot.recordedConfirmations");
197
BrowserBot.prototype.hasPrompts = function() {
198
return (this.recordedPrompts.length > 0);
201
BrowserBot.prototype.getNextPrompt = function() {
202
var t = this.recordedPrompts.shift();
203
this.relayBotToRC("browserbot.recordedPrompts");
207
/* Fire a mouse event in a browser-compatible manner */
209
BrowserBot.prototype.triggerMouseEvent = function(element, eventType, canBubble, clientX, clientY, button) {
210
clientX = clientX ? clientX : 0;
211
clientY = clientY ? clientY : 0;
213
LOG.debug("triggerMouseEvent assumes setting screenX and screenY to 0 is ok");
217
canBubble = (typeof(canBubble) == undefined) ? true : canBubble;
218
if (element.fireEvent && element.ownerDocument && element.ownerDocument.createEventObject) { //IE
219
var evt = createEventObject(element, this.controlKeyDown, this.altKeyDown, this.shiftKeyDown, this.metaKeyDown);
221
evt.button = button ? button : 1; // default will be the left mouse click ( http://www.javascriptkit.com/jsref/event.shtml )
222
evt.relatedTarget = null;
223
if (!screenX && !screenY && !clientX && !clientY && !this.controlKeyDown && !this.altKeyDown && !this.shiftKeyDown && !this.metaKeyDown) {
224
element.fireEvent('on' + eventType);
227
evt.screenX = screenX;
228
evt.screenY = screenY;
229
evt.clientX = clientX;
230
evt.clientY = clientY;
232
// when we go this route, window.event is never set to contain the event we have just created.
233
// ideally we could just slide it in as follows in the try-block below, but this normally
234
// doesn't work. This is why I try to avoid this code path, which is only required if we need to
235
// set attributes on the event (e.g., clientX).
240
// getting an "Object does not support this action or property" error. Save the event away
241
// for future reference.
242
// TODO: is there a way to update window.event?
244
// work around for http://jira.openqa.org/browse/SEL-280 -- make the event available somewhere:
245
selenium.browserbot.getCurrentWindow().selenium_event = evt;
247
element.fireEvent('on' + eventType, evt);
251
var evt = document.createEvent('MouseEvents');
252
if (evt.initMouseEvent)
254
// see http://developer.mozilla.org/en/docs/DOM:event.button and
255
// http://developer.mozilla.org/en/docs/DOM:event.initMouseEvent for button ternary logic logic
257
evt.initMouseEvent(eventType, canBubble, true, document.defaultView, 1, screenX, screenY, clientX, clientY,
258
this.controlKeyDown, this.altKeyDown, this.shiftKeyDown, this.metaKeyDown, button ? button : 0, null);
261
LOG.warn("element doesn't have initMouseEvent; firing an event which should -- but doesn't -- have other mouse-event related attributes here, as well as controlKeyDown, altKeyDown, shiftKeyDown, metaKeyDown");
262
evt.initEvent(eventType, canBubble, true);
264
evt.shiftKey = this.shiftKeyDown;
265
evt.metaKey = this.metaKeyDown;
266
evt.altKey = this.altKeyDown;
267
evt.ctrlKey = this.controlKeyDown;
273
element.dispatchEvent(evt);
277
BrowserBot.prototype._windowClosed = function(win) {
279
if (c == null) return true;
283
BrowserBot.prototype._modifyWindow = function(win) {
284
// In proxyInjectionMode, have to suppress LOG calls in _modifyWindow to avoid an infinite loop
285
if (this._windowClosed(win)) {
286
if (!this.proxyInjectionMode) {
287
LOG.error("modifyWindow: Window was closed!");
291
if (!this.proxyInjectionMode) {
292
LOG.debug('modifyWindow ' + this.uniqueId + ":" + win[this.uniqueId]);
294
if (!win[this.uniqueId]) {
295
win[this.uniqueId] = 1;
296
this.modifyWindowToRecordPopUpDialogs(win, this);
298
// In proxyInjection mode, we have our own mechanism for detecting page loads
299
if (!this.proxyInjectionMode) {
300
this.modifySeparateTestWindowToDetectPageLoads(win);
302
if (win.frames && win.frames.length && win.frames.length > 0) {
303
for (var i = 0; i < win.frames.length; i++) {
305
this._modifyWindow(win.frames[i]);
306
} catch (e) {} // we're just trying to be opportunistic; don't worry if this doesn't work out
312
BrowserBot.prototype.selectWindow = function(target) {
313
if (!target || target == "null") {
314
this._selectTopWindow();
317
var result = target.match(/^([a-zA-Z]+)=(.*)/);
319
this._selectWindowByWindowId(target);
322
locatorType = result[1];
323
locatorValue = result[2];
324
if (locatorType == "title") {
325
this._selectWindowByTitle(locatorValue);
327
// TODO separate name and var into separate functions
328
else if (locatorType == "name") {
329
this._selectWindowByName(locatorValue);
330
} else if (locatorType == "var") {
331
this._selectWindowByName(locatorValue);
333
throw new SeleniumError("Window locator not recognized: " + locatorType);
337
BrowserBot.prototype.selectPopUp = function(windowId) {
338
if (! windowId || windowId == 'null') {
339
this._selectFirstNonTopWindow();
342
this._selectWindowByWindowId(windowId);
346
BrowserBot.prototype._selectTopWindow = function() {
347
this.currentWindowName = null;
348
this.currentWindow = this.topWindow;
349
this.topFrame = this.topWindow;
350
this.isSubFrameSelected = false;
353
BrowserBot.prototype._selectWindowByWindowId = function(windowId) {
355
this._selectWindowByName(windowId);
358
this._selectWindowByTitle(windowId);
362
BrowserBot.prototype._selectWindowByName = function(target) {
363
this.currentWindow = this.getWindowByName(target, false);
364
this.topFrame = this.currentWindow;
365
this.currentWindowName = target;
366
this.isSubFrameSelected = false;
369
BrowserBot.prototype._selectWindowByTitle = function(target) {
370
var windowName = this.getWindowNameByTitle(target);
372
this._selectTopWindow();
374
this._selectWindowByName(windowName);
378
BrowserBot.prototype._selectFirstNonTopWindow = function() {
379
var names = this.getNonTopWindowNames();
381
this._selectWindowByName(names[0]);
385
BrowserBot.prototype.selectFrame = function(target) {
386
if (target.indexOf("index=") == 0) {
387
target = target.substr(6);
388
var frame = this.getCurrentWindow().frames[target];
390
throw new SeleniumError("Not found: frames["+target+"]");
392
if (!frame.document) {
393
throw new SeleniumError("frames["+target+"] is not a frame");
395
this.currentWindow = frame;
396
this.isSubFrameSelected = true;
398
else if (target == "relative=up" || target == "relative=parent") {
399
this.currentWindow = this.getCurrentWindow().parent;
400
this.isSubFrameSelected = (this._getFrameElement(this.currentWindow) != null);
401
} else if (target == "relative=top") {
402
this.currentWindow = this.topFrame;
403
this.isSubFrameSelected = false;
405
var frame = this.findElement(target);
407
throw new SeleniumError("Not found: " + target);
409
// now, did they give us a frame or a frame ELEMENT?
411
if (frame.contentWindow) {
412
// this must be a frame element
413
if (browserVersion.isHTA) {
414
// stupid HTA bug; can't get in the front door
415
target = frame.contentWindow.name;
417
this.currentWindow = frame.contentWindow;
418
this.isSubFrameSelected = true;
421
} else if (frame.document && frame.location) {
422
// must be an actual window frame
423
this.currentWindow = frame;
424
this.isSubFrameSelected = true;
429
// neither, let's loop through the frame names
430
var win = this.getCurrentWindow();
432
if (win && win.frames && win.frames.length) {
433
for (var i = 0; i < win.frames.length; i++) {
434
if (win.frames[i].name == target) {
435
this.currentWindow = win.frames[i];
436
this.isSubFrameSelected = true;
443
throw new SeleniumError("Not a frame: " + target);
447
// modifies the window
448
this.getCurrentWindow();
451
BrowserBot.prototype.doesThisFrameMatchFrameExpression = function(currentFrameString, target) {
453
if (target.indexOf("dom=") == 0) {
454
target = target.substr(4);
456
} else if (target.indexOf("index=") == 0) {
457
target = "frames[" + target.substr(6) + "]";
462
eval("t=" + currentFrameString + "." + target);
465
var autWindow = this.browserbot.getCurrentWindow();
468
if (t.window == autWindow) {
471
if (t.window.uniqueId == autWindow.uniqueId) {
475
} catch (permDenied) {
476
// DGF if the windows are incomparable, they're probably not the same...
483
eval("currentFrame=" + currentFrameString);
484
if (target == "relative=up") {
485
if (currentFrame.window.parent == autWindow) {
490
if (target == "relative=top") {
491
if (currentFrame.window.top == autWindow) {
496
if (currentFrame.window == autWindow.parent) {
497
if (autWindow.name == target) {
501
var element = this.findElement(target, currentFrame.window);
502
if (element.contentWindow == autWindow) {
510
BrowserBot.prototype.openLocation = function(target) {
511
// We're moving to a new page - clear the current one
512
var win = this.getCurrentWindow();
513
LOG.debug("openLocation newPageLoaded = false");
514
this.newPageLoaded = false;
516
this.setOpenLocation(win, target);
519
BrowserBot.prototype.openWindow = function(url, windowID) {
521
url = absolutify(url, this.baseUrl);
523
if (browserVersion.isHTA) {
524
// in HTA mode, calling .open on the window interprets the url relative to that window
525
// we need to absolute-ize the URL to make it consistent
526
var child = this.getCurrentWindow().open(url, windowID);
527
selenium.browserbot.openedWindows[windowID] = child;
529
this.getCurrentWindow().open(url, windowID);
533
BrowserBot.prototype.setIFrameLocation = function(iframe, location) {
534
iframe.src = location;
537
BrowserBot.prototype.setOpenLocation = function(win, loc) {
538
loc = absolutify(loc, this.baseUrl);
539
if (browserVersion.isHTA) {
540
var oldHref = win.location.href;
541
win.location.href = loc;
544
marker = this.isPollingForLoad(win);
545
if (marker && win.location[marker]) {
546
win.location[marker] = false;
548
} catch (e) {} // DGF don't know why, but this often fails
550
win.location.href = loc;
554
BrowserBot.prototype.getCurrentPage = function() {
558
BrowserBot.prototype.modifyWindowToRecordPopUpDialogs = function(windowToModify, browserBot) {
561
windowToModify.seleniumAlert = windowToModify.alert;
563
windowToModify.alert = function(alert) {
564
browserBot.recordedAlerts.push(alert);
565
self.relayBotToRC.call(self, "browserbot.recordedAlerts");
568
windowToModify.confirm = function(message) {
569
browserBot.recordedConfirmations.push(message);
570
var result = browserBot.nextConfirmResult;
571
browserBot.nextConfirmResult = true;
572
self.relayBotToRC.call(self, "browserbot.recordedConfirmations");
576
windowToModify.prompt = function(message) {
577
browserBot.recordedPrompts.push(message);
578
var result = !browserBot.nextConfirmResult ? null : browserBot.nextPromptResult;
579
browserBot.nextConfirmResult = true;
580
browserBot.nextPromptResult = '';
581
self.relayBotToRC.call(self, "browserbot.recordedPrompts");
585
// Keep a reference to all popup windows by name
586
// note that in IE the "windowName" argument must be a valid javascript identifier, it seems.
587
var originalOpen = windowToModify.open;
588
var originalOpenReference;
589
if (browserVersion.isHTA) {
590
originalOpenReference = 'selenium_originalOpen' + new Date().getTime();
591
windowToModify[originalOpenReference] = windowToModify.open;
594
var isHTA = browserVersion.isHTA;
596
var newOpen = function(url, windowName, windowFeatures, replaceFlag) {
597
var myOriginalOpen = originalOpen;
599
myOriginalOpen = this[originalOpenReference];
601
if (windowName == "" || windowName == "_blank") {
602
windowName = "selenium_blank" + Math.round(100000 * Math.random());
603
LOG.warn("Opening window '_blank', which is not a real window name. Randomizing target to be: " + windowName);
605
var openedWindow = myOriginalOpen(url, windowName, windowFeatures, replaceFlag);
606
LOG.debug("window.open call intercepted; window ID (which you can use with selectWindow()) is \"" + windowName + "\"");
607
if (windowName!=null) {
608
openedWindow["seleniumWindowName"] = windowName;
610
selenium.browserbot.openedWindows[windowName] = openedWindow;
614
if (browserVersion.isHTA) {
615
originalOpenReference = 'selenium_originalOpen' + new Date().getTime();
616
newOpenReference = 'selenium_newOpen' + new Date().getTime();
617
var setOriginalRef = "this['" + originalOpenReference + "'] = this.open;";
619
if (windowToModify.eval) {
620
windowToModify.eval(setOriginalRef);
621
windowToModify.open = newOpen;
623
// DGF why can't I eval here? Seems like I'm querying the window at a bad time, maybe?
624
setOriginalRef += "this.open = this['" + newOpenReference + "'];";
625
windowToModify[newOpenReference] = newOpen;
626
windowToModify.setTimeout(setOriginalRef, 0);
629
windowToModify.open = newOpen;
634
* Call the supplied function when a the current page unloads and a new one loads.
635
* This is done by polling continuously until the document changes and is fully loaded.
637
BrowserBot.prototype.modifySeparateTestWindowToDetectPageLoads = function(windowObject) {
638
// Since the unload event doesn't fire in Safari 1.3, we start polling immediately
640
LOG.warn("modifySeparateTestWindowToDetectPageLoads: no windowObject!");
643
if (this._windowClosed(windowObject)) {
644
LOG.info("modifySeparateTestWindowToDetectPageLoads: windowObject was closed");
647
var oldMarker = this.isPollingForLoad(windowObject);
649
LOG.debug("modifySeparateTestWindowToDetectPageLoads: already polling this window: " + oldMarker);
653
var marker = 'selenium' + new Date().getTime();
654
LOG.debug("Starting pollForLoad (" + marker + "): " + windowObject.location);
655
this.pollingForLoad[marker] = true;
656
// if this is a frame, add a load listener, otherwise, attach a poller
657
var frameElement = this._getFrameElement(windowObject);
658
// DGF HTA mode can't attach load listeners to subframes (yuk!)
659
var htaSubFrame = this._isHTASubFrame(windowObject);
660
if (frameElement && !htaSubFrame) {
661
LOG.debug("modifySeparateTestWindowToDetectPageLoads: this window is a frame; attaching a load listener");
662
addLoadListener(frameElement, this.recordPageLoad);
663
frameElement[marker] = true;
664
frameElement["frame"+this.uniqueId] = marker;
665
LOG.debug("dgf this.uniqueId="+this.uniqueId);
666
LOG.debug("dgf marker="+marker);
667
LOG.debug("dgf frameElement['frame'+this.uniqueId]="+frameElement['frame'+this.uniqueId]);
668
frameElement[this.uniqueId] = marker;
669
LOG.debug("dgf frameElement[this.uniqueId]="+frameElement[this.uniqueId]);
671
windowObject.location[marker] = true;
672
windowObject[this.uniqueId] = marker;
673
this.pollForLoad(this.recordPageLoad, windowObject, windowObject.document, windowObject.location, windowObject.location.href, marker);
677
BrowserBot.prototype._isHTASubFrame = function(win) {
678
if (!browserVersion.isHTA) return false;
679
// DGF this is wrong! what if "win" isn't the selected window?
680
return this.isSubFrameSelected;
683
BrowserBot.prototype._getFrameElement = function(win) {
684
var frameElement = null;
687
frameElement = win.frameElement;
692
// on IE, checking frameElement in a pop-up results in a "No such interface supported" exception
693
// but it might have a frame element anyway!
694
var parentContainsIdenticallyNamedFrame = false;
696
parentContainsIdenticallyNamedFrame = win.parent.frames[win.name];
697
} catch (e) {} // this may fail if access is denied to the parent; in that case, assume it's not a pop-up
699
if (parentContainsIdenticallyNamedFrame) {
700
// it can't be a coincidence that the parent has a frame with the same name as myself!
703
result = parentContainsIdenticallyNamedFrame.frameElement;
707
} catch (e) {} // it was worth a try! _getFrameElementsByName is often slow
708
result = this._getFrameElementByName(win.name, win.parent.document, win);
712
LOG.debug("_getFrameElement: frameElement="+frameElement);
714
LOG.debug("frameElement.name="+frameElement.name);
719
BrowserBot.prototype._getFrameElementByName = function(name, doc, win) {
723
frames = doc.getElementsByTagName("iframe");
724
for (i = 0; i < frames.length; i++) {
726
if (frame.name === name) {
730
frames = doc.getElementsByTagName("frame");
731
for (i = 0; i < frames.length; i++) {
733
if (frame.name === name) {
737
// DGF weird; we only call this function when we know the doc contains the frame
738
LOG.warn("_getFrameElementByName couldn't find a frame or iframe; checking every element for the name " + name);
739
return BrowserBot.prototype.locateElementByName(win.name, win.parent.document);
744
* Set up a polling timer that will keep checking the readyState of the document until it's complete.
745
* Since we might call this before the original page is unloaded, we first check to see that the current location
746
* or href is different from the original one.
748
BrowserBot.prototype.pollForLoad = function(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker) {
749
LOG.debug("pollForLoad original (" + marker + "): " + originalHref);
751
if (this._windowClosed(windowObject)) {
752
LOG.debug("pollForLoad WINDOW CLOSED (" + marker + ")");
753
delete this.pollingForLoad[marker];
757
var isSamePage = this._isSamePage(windowObject, originalDocument, originalLocation, originalHref, marker);
758
var rs = this.getReadyState(windowObject, windowObject.document);
760
if (!isSamePage && rs == 'complete') {
761
var currentHref = windowObject.location.href;
762
LOG.debug("pollForLoad FINISHED (" + marker + "): " + rs + " (" + currentHref + ")");
763
delete this.pollingForLoad[marker];
764
this._modifyWindow(windowObject);
765
var newMarker = this.isPollingForLoad(windowObject);
767
LOG.debug("modifyWindow didn't start new poller: " + newMarker);
768
this.modifySeparateTestWindowToDetectPageLoads(windowObject);
770
newMarker = this.isPollingForLoad(windowObject);
771
var currentlySelectedWindow;
772
var currentlySelectedWindowMarker;
773
currentlySelectedWindow =this.getCurrentWindow(true);
774
currentlySelectedWindowMarker = currentlySelectedWindow[this.uniqueId];
776
LOG.debug("pollForLoad (" + marker + ") restarting " + newMarker);
777
if (/(TestRunner-splash|Blank)\.html\?start=true$/.test(currentHref)) {
778
LOG.debug("pollForLoad Oh, it's just the starting page. Never mind!");
779
} else if (currentlySelectedWindowMarker == newMarker) {
780
loadFunction(currentlySelectedWindow);
782
LOG.debug("pollForLoad page load detected in non-current window; ignoring (currentlySelected="+currentlySelectedWindowMarker+", detection in "+newMarker+")");
786
LOG.debug("pollForLoad continue (" + marker + "): " + currentHref);
787
this.reschedulePoller(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker);
789
LOG.debug("Exception during pollForLoad; this should get noticed soon (" + e.message + ")!");
790
//DGF this is supposed to get logged later; log it at debug just in case
792
this.pageLoadError = e;
796
BrowserBot.prototype._isSamePage = function(windowObject, originalDocument, originalLocation, originalHref, marker) {
797
var currentDocument = windowObject.document;
798
var currentLocation = windowObject.location;
799
var currentHref = currentLocation.href
801
var sameDoc = this._isSameDocument(originalDocument, currentDocument);
803
var sameLoc = (originalLocation === currentLocation);
805
// hash marks don't meant the page has loaded, so we need to strip them off if they exist...
806
var currentHash = currentHref.indexOf('#');
807
if (currentHash > 0) {
808
currentHref = currentHref.substring(0, currentHash);
810
var originalHash = originalHref.indexOf('#');
811
if (originalHash > 0) {
812
originalHref = originalHref.substring(0, originalHash);
814
LOG.debug("_isSamePage: currentHref: " + currentHref);
815
LOG.debug("_isSamePage: originalHref: " + originalHref);
817
var sameHref = (originalHref === currentHref);
818
var markedLoc = currentLocation[marker];
820
if (browserVersion.isKonqueror || browserVersion.isSafari) {
821
// the mark disappears too early on these browsers
825
// since this is some _very_ important logic, especially for PI and multiWindow mode, we should log all these out
826
LOG.debug("_isSamePage: sameDoc: " + sameDoc);
827
LOG.debug("_isSamePage: sameLoc: " + sameLoc);
828
LOG.debug("_isSamePage: sameHref: " + sameHref);
829
LOG.debug("_isSamePage: markedLoc: " + markedLoc);
831
return sameDoc && sameLoc && sameHref && markedLoc
834
BrowserBot.prototype._isSameDocument = function(originalDocument, currentDocument) {
835
return originalDocument === currentDocument;
839
BrowserBot.prototype.getReadyState = function(windowObject, currentDocument) {
840
var rs = currentDocument.readyState;
842
if ((this.buttonWindow!=null && this.buttonWindow.document.readyState == null) // not proxy injection mode (and therefore buttonWindow isn't null)
843
|| (top.document.readyState == null)) { // proxy injection mode (and therefore everything's in the top window, but buttonWindow doesn't exist)
844
// uh oh! we're probably on Firefox with no readyState extension installed!
845
// We'll have to just take a guess as to when the document is loaded; this guess
846
// will never be perfect. :-(
847
if (typeof currentDocument.getElementsByTagName != 'undefined'
848
&& typeof currentDocument.getElementById != 'undefined'
849
&& ( currentDocument.getElementsByTagName('body')[0] != null
850
|| currentDocument.body != null )) {
851
if (windowObject.frameElement && windowObject.location.href == "about:blank" && windowObject.frameElement.src != "about:blank") {
852
LOG.info("getReadyState not loaded, frame location was about:blank, but frame src = " + windowObject.frameElement.src);
855
LOG.debug("getReadyState = windowObject.frames.length = " + windowObject.frames.length);
856
for (var i = 0; i < windowObject.frames.length; i++) {
857
LOG.debug("i = " + i);
858
if (this.getReadyState(windowObject.frames[i], windowObject.frames[i].document) != 'complete') {
859
LOG.debug("getReadyState aha! the nested frame " + windowObject.frames[i].name + " wasn't ready!");
866
LOG.debug("pollForLoad readyState was null and DOM appeared to not be ready yet");
870
else if (rs == "loading" && browserVersion.isIE) {
871
LOG.debug("pageUnloading = true!!!!");
872
this.pageUnloading = true;
874
LOG.debug("getReadyState returning " + rs);
878
/** This function isn't used normally, but was the way we used to schedule pollers:
879
asynchronously executed autonomous units. This is deprecated, but remains here
880
for future reference.
882
BrowserBot.prototype.XXXreschedulePoller = function(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker) {
884
window.setTimeout(function() {
885
self.pollForLoad(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker);
889
/** This function isn't used normally, but is useful for debugging asynchronous pollers
890
* To enable it, rename it to "reschedulePoller", so it will override the
891
* existing reschedulePoller function
893
BrowserBot.prototype.XXXreschedulePoller = function(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker) {
894
var doc = this.buttonWindow.document;
895
var button = doc.createElement("button");
896
var buttonName = doc.createTextNode(marker + " - " + windowObject.name);
897
button.appendChild(buttonName);
898
var tools = doc.getElementById("tools");
900
button.onclick = function() {
901
tools.removeChild(button);
902
self.pollForLoad(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker);
904
tools.appendChild(button);
905
window.setTimeout(button.onclick, 500);
908
BrowserBot.prototype.reschedulePoller = function(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker) {
910
var pollerFunction = function() {
911
self.pollForLoad(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker);
913
this.windowPollers.push(pollerFunction);
916
BrowserBot.prototype.runScheduledPollers = function() {
917
LOG.debug("runScheduledPollers");
918
var oldPollers = this.windowPollers;
919
this.windowPollers = new Array();
920
for (var i = 0; i < oldPollers.length; i++) {
921
oldPollers[i].call();
923
LOG.debug("runScheduledPollers DONE");
926
BrowserBot.prototype.isPollingForLoad = function(win) {
928
var frameElement = this._getFrameElement(win);
929
var htaSubFrame = this._isHTASubFrame(win);
930
if (frameElement && !htaSubFrame) {
931
marker = frameElement["frame"+this.uniqueId];
933
marker = win[this.uniqueId];
936
LOG.debug("isPollingForLoad false, missing uniqueId " + this.uniqueId + ": " + marker);
939
if (!this.pollingForLoad[marker]) {
940
LOG.debug("isPollingForLoad false, this.pollingForLoad[" + marker + "]: " + this.pollingForLoad[marker]);
946
BrowserBot.prototype.getWindowByName = function(windowName, doNotModify) {
947
LOG.debug("getWindowByName(" + windowName + ")");
948
// First look in the map of opened windows
949
var targetWindow = this.openedWindows[windowName];
951
targetWindow = this.topWindow[windowName];
953
if (!targetWindow && windowName == "_blank") {
954
for (var winName in this.openedWindows) {
955
// _blank can match selenium_blank*, if it looks like it's OK (valid href, not closed)
956
if (/^selenium_blank/.test(winName)) {
957
targetWindow = this.openedWindows[winName];
960
if (!this._windowClosed(targetWindow)) {
961
ok = targetWindow.location.href;
969
throw new SeleniumError("Window does not exist. If this looks like a Selenium bug, make sure to read http://selenium-core.openqa.org/reference.html#openWindow for potential workarounds.");
971
if (browserVersion.isHTA) {
973
targetWindow.location.href;
975
targetWindow = window.open("", targetWindow.name);
976
this.openedWindows[targetWindow.name] = targetWindow;
980
this._modifyWindow(targetWindow);
986
* Find a window name from the window title.
988
BrowserBot.prototype.getWindowNameByTitle = function(windowTitle) {
989
LOG.debug("getWindowNameByTitle(" + windowTitle + ")");
991
// First look in the map of opened windows and iterate them
992
for (var windowName in this.openedWindows) {
993
var targetWindow = this.openedWindows[windowName];
995
// If the target window's title is our title
997
// TODO implement Pattern Matching here
998
if (!this._windowClosed(targetWindow) &&
999
targetWindow.document.title == windowTitle) {
1003
// You'll often get Permission Denied errors here in IE
1004
// eh, if we can't read this window's title,
1005
// it's probably not available to us right now anyway
1010
if (this.topWindow.document.title == windowTitle) {
1013
} catch (e) {} // IE Perm denied
1015
throw new SeleniumError("Could not find window with title " + windowTitle);
1018
BrowserBot.prototype.getNonTopWindowNames = function() {
1019
var nonTopWindowNames = [];
1021
for (var windowName in this.openedWindows) {
1022
var win = this.openedWindows[windowName];
1023
if (! this._windowClosed(win) && win != this.topWindow) {
1024
nonTopWindowNames.push(windowName);
1028
return nonTopWindowNames;
1031
BrowserBot.prototype.getCurrentWindow = function(doNotModify) {
1032
if (this.proxyInjectionMode) {
1035
var testWindow = this.currentWindow;
1037
this._modifyWindow(testWindow);
1038
LOG.debug("getCurrentWindow newPageLoaded = false");
1039
this.newPageLoaded = false;
1041
testWindow = this._handleClosedSubFrame(testWindow, doNotModify);
1046
* Offer a method the end-user can reliably use to retrieve the current window.
1047
* This should work even for windows with an XPCNativeWrapper. Returns the
1048
* current window object.
1050
BrowserBot.prototype.getUserWindow = function() {
1051
var userWindow = this.getCurrentWindow(true);
1053
if (userWindow.wrappedJSObject) {
1054
userWindow = userWindow.wrappedJSObject;
1060
BrowserBot.prototype._handleClosedSubFrame = function(testWindow, doNotModify) {
1061
if (this.proxyInjectionMode) {
1065
if (this.isSubFrameSelected) {
1067
if (testWindow.parent && testWindow.parent.frames && testWindow.parent.frames.length) {
1068
for (var i = 0; i < testWindow.parent.frames.length; i++) {
1069
if (testWindow.parent.frames[i] == testWindow) {
1076
LOG.warn("Current subframe appears to have closed; selecting top frame");
1077
this.selectFrame("relative=top");
1078
return this.getCurrentWindow(doNotModify);
1080
} else if (this._windowClosed(testWindow)) {
1081
var closedError = new SeleniumError("Current window or frame is closed!");
1082
closedError.windowClosed = true;
1088
BrowserBot.prototype.highlight = function (element, force) {
1089
if (force || this.shouldHighlightLocatedElement) {
1092
} catch (e) {} // DGF element highlighting is low-priority and possibly dangerous
1097
BrowserBot.prototype.setShouldHighlightElement = function (shouldHighlight) {
1098
this.shouldHighlightLocatedElement = shouldHighlight;
1101
/*****************************************************************/
1102
/* BROWSER-SPECIFIC FUNCTIONS ONLY AFTER THIS LINE */
1105
BrowserBot.prototype._registerAllLocatorFunctions = function() {
1106
// TODO - don't do this in the constructor - only needed once ever
1107
this.locationStrategies = {};
1108
for (var functionName in this) {
1109
var result = /^locateElementBy([A-Z].+)$/.exec(functionName);
1110
if (result != null) {
1111
var locatorFunction = this[functionName];
1112
if (typeof(locatorFunction) != 'function') {
1115
// Use a specified prefix in preference to one generated from
1116
// the function name
1117
var locatorPrefix = locatorFunction.prefix || result[1].toLowerCase();
1118
this.locationStrategies[locatorPrefix] = locatorFunction;
1123
* Find a locator based on a prefix.
1125
this.findElementBy = function(locatorType, locator, inDocument, inWindow) {
1126
var locatorFunction = this.locationStrategies[locatorType];
1127
if (! locatorFunction) {
1128
throw new SeleniumError("Unrecognised locator type: '" + locatorType + "'");
1130
return locatorFunction.call(this, locator, inDocument, inWindow);
1134
* The implicit locator, that is used when no prefix is supplied.
1136
this.locationStrategies['implicit'] = function(locator, inDocument, inWindow) {
1137
if (locator.startsWith('//')) {
1138
return this.locateElementByXPath(locator, inDocument, inWindow);
1140
if (locator.startsWith('document.')) {
1141
return this.locateElementByDomTraversal(locator, inDocument, inWindow);
1143
return this.locateElementByIdentifier(locator, inDocument, inWindow);
1147
BrowserBot.prototype.getDocument = function() {
1148
return this.getCurrentWindow().document;
1151
BrowserBot.prototype.getTitle = function() {
1152
var t = this.getDocument().title;
1153
if (typeof(t) == "string") {
1159
BrowserBot.prototype.getCookieByName = function(cookieName, doc) {
1160
if (!doc) doc = this.getDocument();
1161
var ck = doc.cookie;
1162
if (!ck) return null;
1163
var ckPairs = ck.split(/;/);
1164
for (var i = 0; i < ckPairs.length; i++) {
1165
var ckPair = ckPairs[i].trim();
1166
var ckNameValue = ckPair.split(/=/);
1167
var ckName = decodeURIComponent(ckNameValue[0]);
1168
if (ckName === cookieName) {
1169
return decodeURIComponent(ckNameValue[1]);
1175
BrowserBot.prototype.getAllCookieNames = function(doc) {
1176
if (!doc) doc = this.getDocument();
1177
var ck = doc.cookie;
1179
var cookieNames = [];
1180
var ckPairs = ck.split(/;/);
1181
for (var i = 0; i < ckPairs.length; i++) {
1182
var ckPair = ckPairs[i].trim();
1183
var ckNameValue = ckPair.split(/=/);
1184
var ckName = decodeURIComponent(ckNameValue[0]);
1185
cookieNames.push(ckName);
1190
BrowserBot.prototype.deleteCookie = function(cookieName, domain, path, doc) {
1191
if (!doc) doc = this.getDocument();
1192
var expireDateInMilliseconds = (new Date()).getTime() + (-1 * 1000);
1193
var cookie = cookieName + "=deleted; ";
1195
cookie += "path=" + path + "; ";
1198
cookie += "domain=" + domain + "; ";
1200
cookie += "expires=" + new Date(expireDateInMilliseconds).toGMTString();
1201
LOG.debug("Setting cookie to: " + cookie);
1202
doc.cookie = cookie;
1205
/** Try to delete cookie, return false if it didn't work */
1206
BrowserBot.prototype._maybeDeleteCookie = function(cookieName, domain, path, doc) {
1207
this.deleteCookie(cookieName, domain, path, doc);
1208
return (!this.getCookieByName(cookieName, doc));
1212
BrowserBot.prototype._recursivelyDeleteCookieDomains = function(cookieName, domain, path, doc) {
1213
var deleted = this._maybeDeleteCookie(cookieName, domain, path, doc);
1214
if (deleted) return true;
1215
var dotIndex = domain.indexOf(".");
1216
if (dotIndex == 0) {
1217
return this._recursivelyDeleteCookieDomains(cookieName, domain.substring(1), path, doc);
1218
} else if (dotIndex != -1) {
1219
return this._recursivelyDeleteCookieDomains(cookieName, domain.substring(dotIndex), path, doc);
1221
// No more dots; try just not passing in a domain at all
1222
return this._maybeDeleteCookie(cookieName, null, path, doc);
1226
BrowserBot.prototype._recursivelyDeleteCookie = function(cookieName, domain, path, doc) {
1227
var slashIndex = path.lastIndexOf("/");
1228
var finalIndex = path.length-1;
1229
if (slashIndex == finalIndex) {
1232
if (slashIndex != -1) {
1233
deleted = this._recursivelyDeleteCookie(cookieName, domain, path.substring(0, slashIndex+1), doc);
1234
if (deleted) return true;
1236
return this._recursivelyDeleteCookieDomains(cookieName, domain, path, doc);
1239
BrowserBot.prototype.recursivelyDeleteCookie = function(cookieName, domain, path, win) {
1240
if (!win) win = this.getCurrentWindow();
1241
var doc = win.document;
1243
domain = doc.domain;
1246
path = win.location.pathname;
1248
var deleted = this._recursivelyDeleteCookie(cookieName, "." + domain, path, doc);
1249
if (deleted) return;
1250
// Finally try a null path (Try it last because it's uncommon)
1251
deleted = this._recursivelyDeleteCookieDomains(cookieName, "." + domain, null, doc);
1252
if (deleted) return;
1253
throw new SeleniumError("Couldn't delete cookie " + cookieName);
1257
* Finds an element recursively in frames and nested frames
1258
* in the specified document, using various lookup protocols
1260
BrowserBot.prototype.findElementRecursive = function(locatorType, locatorString, inDocument, inWindow) {
1262
var element = this.findElementBy(locatorType, locatorString, inDocument, inWindow);
1263
if (element != null) {
1267
for (var i = 0; i < inWindow.frames.length; i++) {
1268
// On some browsers, the document object is undefined for third-party
1269
// frames. Make sure the document is valid before continuing.
1270
if (inWindow.frames[i].document) {
1271
element = this.findElementRecursive(locatorType, locatorString, inWindow.frames[i].document, inWindow.frames[i]);
1273
if (element != null) {
1281
* Finds an element on the current page, using various lookup protocols
1283
BrowserBot.prototype.findElementOrNull = function(locator, win) {
1284
locator = parse_locator(locator);
1287
win = this.getCurrentWindow();
1289
var element = this.findElementRecursive(locator.type, locator.string, win.document, win);
1291
if (element != null) {
1292
return this.browserbot.highlight(element);
1295
// Element was not found by any locator function.
1299
BrowserBot.prototype.findElement = function(locator, win) {
1300
var element = this.findElementOrNull(locator, win);
1301
if (element == null) throw new SeleniumError("Element " + locator + " not found");
1306
* In non-IE browsers, getElementById() does not search by name. Instead, we
1307
* we search separately by id and name.
1309
BrowserBot.prototype.locateElementByIdentifier = function(identifier, inDocument, inWindow) {
1310
return BrowserBot.prototype.locateElementById(identifier, inDocument, inWindow)
1311
|| BrowserBot.prototype.locateElementByName(identifier, inDocument, inWindow)
1316
* Find the element with id - can't rely on getElementById, coz it returns by name as well in IE..
1318
BrowserBot.prototype.locateElementById = function(identifier, inDocument, inWindow) {
1319
var element = inDocument.getElementById(identifier);
1320
if (element && element.getAttribute('id') === identifier) {
1323
else if (browserVersion.isIE || browserVersion.isOpera) {
1325
var xpath = '/descendant::*[@id=' + identifier.quoteForXPath() + ']';
1326
return BrowserBot.prototype
1327
.locateElementByXPath(xpath, inDocument, inWindow);
1335
* Find an element by name, refined by (optional) element-filter
1338
BrowserBot.prototype.locateElementByName = function(locator, document, inWindow) {
1339
var elements = document.getElementsByTagName("*");
1341
var filters = locator.split(' ');
1342
filters[0] = 'name=' + filters[0];
1344
while (filters.length) {
1345
var filter = filters.shift();
1346
elements = this.selectElements(filter, elements, 'value');
1349
if (elements.length > 0) {
1356
* Finds an element using by evaluating the specfied string.
1358
BrowserBot.prototype.locateElementByDomTraversal = function(domTraversal, document, window) {
1360
var browserbot = this.browserbot;
1363
element = eval(domTraversal);
1374
BrowserBot.prototype.locateElementByDomTraversal.prefix = "dom";
1377
* Finds an element identified by the xpath expression. Expressions _must_
1380
BrowserBot.prototype.locateElementByXPath = function(xpath, inDocument, inWindow) {
1381
var results = eval_xpath(xpath, inDocument, {
1382
returnOnFirstMatch : true,
1383
ignoreAttributesWithoutValue: this.ignoreAttributesWithoutValue,
1384
allowNativeXpath : this.allowNativeXpath,
1385
xpathLibrary : this.xpathLibrary,
1386
namespaceResolver : this._namespaceResolver
1388
return (results.length > 0) ? results[0] : null;
1391
BrowserBot.prototype._namespaceResolver = function(prefix) {
1392
if (prefix == 'html' || prefix == 'xhtml' || prefix == 'x') {
1393
return 'http://www.w3.org/1999/xhtml';
1394
} else if (prefix == 'mathml') {
1395
return 'http://www.w3.org/1998/Math/MathML';
1397
throw new Error("Unknown namespace: " + prefix + ".");
1402
* Returns the number of xpath results.
1404
BrowserBot.prototype.evaluateXPathCount = function(xpath, inDocument) {
1405
var results = eval_xpath(xpath, inDocument, {
1406
ignoreAttributesWithoutValue: this.ignoreAttributesWithoutValue,
1407
allowNativeXpath : this.allowNativeXpath,
1408
xpathLibrary : this.xpathLibrary,
1409
namespaceResolver : this._namespaceResolver
1411
return results.length;
1415
* Finds a link element with text matching the expression supplied. Expressions must
1416
* begin with "link:".
1418
BrowserBot.prototype.locateElementByLinkText = function(linkText, inDocument, inWindow) {
1419
var links = inDocument.getElementsByTagName('a');
1420
for (var i = 0; i < links.length; i++) {
1421
var element = links[i];
1422
if (PatternMatcher.matches(linkText, getText(element))) {
1428
BrowserBot.prototype.locateElementByLinkText.prefix = "link";
1431
* Returns an attribute based on an attribute locator. This is made up of an element locator
1432
* suffixed with @attribute-name.
1434
BrowserBot.prototype.findAttribute = function(locator) {
1435
// Split into locator + attributeName
1436
var attributePos = locator.lastIndexOf("@");
1437
var elementLocator = locator.slice(0, attributePos);
1438
var attributeName = locator.slice(attributePos + 1);
1440
// Find the element.
1441
var element = this.findElement(elementLocator);
1443
// Handle missing "class" attribute in IE.
1444
if (browserVersion.isIE && attributeName == "class") {
1445
attributeName = "className";
1448
// Get the attribute value.
1449
var attributeValue = element.getAttribute(attributeName);
1451
// IE returns an object for the "style" attribute
1452
if (attributeName == 'style' && typeof(attributeValue) != 'string') {
1453
attributeValue = attributeValue.cssText;
1456
return attributeValue ? attributeValue.toString() : null;
1460
* Select the specified option and trigger the relevant events of the element.
1462
BrowserBot.prototype.selectOption = function(element, optionToSelect) {
1463
triggerEvent(element, 'focus', false);
1464
var changed = false;
1465
for (var i = 0; i < element.options.length; i++) {
1466
var option = element.options[i];
1467
if (option.selected && option != optionToSelect) {
1468
option.selected = false;
1471
else if (!option.selected && option == optionToSelect) {
1472
option.selected = true;
1478
triggerEvent(element, 'change', true);
1483
* Select the specified option and trigger the relevant events of the element.
1485
BrowserBot.prototype.addSelection = function(element, option) {
1486
this.checkMultiselect(element);
1487
triggerEvent(element, 'focus', false);
1488
if (!option.selected) {
1489
option.selected = true;
1490
triggerEvent(element, 'change', true);
1495
* Select the specified option and trigger the relevant events of the element.
1497
BrowserBot.prototype.removeSelection = function(element, option) {
1498
this.checkMultiselect(element);
1499
triggerEvent(element, 'focus', false);
1500
if (option.selected) {
1501
option.selected = false;
1502
triggerEvent(element, 'change', true);
1506
BrowserBot.prototype.checkMultiselect = function(element) {
1507
if (!element.multiple)
1509
throw new SeleniumError("Not a multi-select");
1514
BrowserBot.prototype.replaceText = function(element, stringValue) {
1515
triggerEvent(element, 'focus', false);
1516
triggerEvent(element, 'select', true);
1517
var maxLengthAttr = element.getAttribute("maxLength");
1518
var actualValue = stringValue;
1519
if (maxLengthAttr != null) {
1520
var maxLength = parseInt(maxLengthAttr);
1521
if (stringValue.length > maxLength) {
1522
actualValue = stringValue.substr(0, maxLength);
1526
if (getTagName(element) == "body") {
1527
if (element.ownerDocument && element.ownerDocument.designMode) {
1528
var designMode = new String(element.ownerDocument.designMode).toLowerCase();
1529
if (designMode = "on") {
1530
// this must be a rich text control!
1531
element.innerHTML = actualValue;
1535
element.value = actualValue;
1537
// DGF this used to be skipped in chrome URLs, but no longer. Is xpcnativewrappers to blame?
1539
triggerEvent(element, 'change', true);
1543
BrowserBot.prototype.submit = function(formElement) {
1544
var actuallySubmit = true;
1545
this._modifyElementTarget(formElement);
1546
if (formElement.onsubmit) {
1547
if (browserVersion.isHTA) {
1548
// run the code in the correct window so alerts are handled correctly even in HTA mode
1549
var win = this.browserbot.getCurrentWindow();
1550
var now = new Date().getTime();
1551
var marker = 'marker' + now;
1552
win[marker] = formElement;
1553
win.setTimeout("var actuallySubmit = "+marker+".onsubmit();" +
1554
"if (actuallySubmit) { " +
1555
marker+".submit(); " +
1556
"if ("+marker+".target && !/^_/.test("+marker+".target)) {"+
1557
"window.open('', "+marker+".target);"+
1561
// pause for up to 2s while this command runs
1562
var terminationCondition = function () {
1563
return !win[marker];
1565
return Selenium.decorateFunctionWithTimeout(terminationCondition, 2000);
1567
actuallySubmit = formElement.onsubmit();
1568
if (actuallySubmit) {
1569
formElement.submit();
1570
if (formElement.target && !/^_/.test(formElement.target)) {
1571
this.browserbot.openWindow('', formElement.target);
1576
formElement.submit();
1580
BrowserBot.prototype.clickElement = function(element, clientX, clientY) {
1581
this._fireEventOnElement("click", element, clientX, clientY);
1584
BrowserBot.prototype.doubleClickElement = function(element, clientX, clientY) {
1585
this._fireEventOnElement("dblclick", element, clientX, clientY);
1588
// The contextmenu event is fired when the user right-clicks to open the context menu
1589
BrowserBot.prototype.contextMenuOnElement = function(element, clientX, clientY) {
1590
this._fireEventOnElement("contextmenu", element, clientX, clientY);
1593
BrowserBot.prototype._modifyElementTarget = function(element) {
1594
if (element.target) {
1595
if (element.target == "_blank" || /^selenium_blank/.test(element.target) ) {
1596
var tagName = getTagName(element);
1597
if (tagName == "a" || tagName == "form") {
1598
var newTarget = "selenium_blank" + Math.round(100000 * Math.random());
1599
LOG.warn("Link has target '_blank', which is not supported in Selenium! Randomizing target to be: " + newTarget);
1600
this.browserbot.openWindow('', newTarget);
1601
element.target = newTarget;
1608
BrowserBot.prototype._handleClickingImagesInsideLinks = function(targetWindow, element) {
1609
var itrElement = element;
1610
while (itrElement != null) {
1611
if (itrElement.href) {
1612
targetWindow.location.href = itrElement.href;
1615
itrElement = itrElement.parentNode;
1619
BrowserBot.prototype._getTargetWindow = function(element) {
1620
var targetWindow = element.ownerDocument.defaultView;
1621
if (element.target) {
1622
targetWindow = this._getFrameFromGlobal(element.target);
1624
return targetWindow;
1627
BrowserBot.prototype._getFrameFromGlobal = function(target) {
1629
if (target == "_self") {
1630
return this.getCurrentWindow();
1632
if (target == "_top") {
1633
return this.topFrame;
1634
} else if (target == "_parent") {
1635
return this.getCurrentWindow().parent;
1636
} else if (target == "_blank") {
1637
// TODO should this set cleverer window defaults?
1638
return this.getCurrentWindow().open('', '_blank');
1640
var frameElement = this.findElementBy("implicit", target, this.topFrame.document, this.topFrame);
1642
return frameElement.contentWindow;
1644
var win = this.getWindowByName(target);
1645
if (win) return win;
1646
return this.getCurrentWindow().open('', target);
1650
BrowserBot.prototype.bodyText = function() {
1651
if (!this.getDocument().body) {
1652
throw new SeleniumError("Couldn't access document.body. Is this HTML page fully loaded?");
1654
return getText(this.getDocument().body);
1657
BrowserBot.prototype.getAllButtons = function() {
1658
var elements = this.getDocument().getElementsByTagName('input');
1661
for (var i = 0; i < elements.length; i++) {
1662
if (elements[i].type == 'button' || elements[i].type == 'submit' || elements[i].type == 'reset') {
1663
result.push(elements[i].id);
1671
BrowserBot.prototype.getAllFields = function() {
1672
var elements = this.getDocument().getElementsByTagName('input');
1675
for (var i = 0; i < elements.length; i++) {
1676
if (elements[i].type == 'text') {
1677
result.push(elements[i].id);
1684
BrowserBot.prototype.getAllLinks = function() {
1685
var elements = this.getDocument().getElementsByTagName('a');
1688
for (var i = 0; i < elements.length; i++) {
1689
result.push(elements[i].id);
1695
function isDefined(value) {
1696
return typeof(value) != undefined;
1699
BrowserBot.prototype.goBack = function() {
1700
this.getCurrentWindow().history.back();
1703
BrowserBot.prototype.goForward = function() {
1704
this.getCurrentWindow().history.forward();
1707
BrowserBot.prototype.close = function() {
1708
if (browserVersion.isIE) {
1709
// fix "do you want to close this window" warning in IE
1710
// You can only close windows that you have opened.
1711
// So, let's "open" it.
1713
this.topFrame.name=new Date().getTime();
1714
window.open("", this.topFrame.name, "");
1715
this.topFrame.close();
1719
if (browserVersion.isChrome || browserVersion.isSafari || browserVersion.isOpera) {
1720
this.topFrame.close();
1722
this.getCurrentWindow().eval("window.top.close();");
1726
BrowserBot.prototype.refresh = function() {
1727
this.getCurrentWindow().location.reload(true);
1731
* Refine a list of elements using a filter.
1733
BrowserBot.prototype.selectElementsBy = function(filterType, filter, elements) {
1734
var filterFunction = BrowserBot.filterFunctions[filterType];
1735
if (! filterFunction) {
1736
throw new SeleniumError("Unrecognised element-filter type: '" + filterType + "'");
1739
return filterFunction(filter, elements);
1742
BrowserBot.filterFunctions = {};
1744
BrowserBot.filterFunctions.name = function(name, elements) {
1745
var selectedElements = [];
1746
for (var i = 0; i < elements.length; i++) {
1747
if (elements[i].name === name) {
1748
selectedElements.push(elements[i]);
1751
return selectedElements;
1754
BrowserBot.filterFunctions.value = function(value, elements) {
1755
var selectedElements = [];
1756
for (var i = 0; i < elements.length; i++) {
1757
if (elements[i].value === value) {
1758
selectedElements.push(elements[i]);
1761
return selectedElements;
1764
BrowserBot.filterFunctions.index = function(index, elements) {
1765
index = Number(index);
1766
if (isNaN(index) || index < 0) {
1767
throw new SeleniumError("Illegal Index: " + index);
1769
if (elements.length <= index) {
1770
throw new SeleniumError("Index out of range: " + index);
1772
return [elements[index]];
1775
BrowserBot.prototype.selectElements = function(filterExpr, elements, defaultFilterType) {
1777
var filterType = (defaultFilterType || 'value');
1779
// If there is a filter prefix, use the specified strategy
1780
var result = filterExpr.match(/^([A-Za-z]+)=(.+)/);
1782
filterType = result[1].toLowerCase();
1783
filterExpr = result[2];
1786
return this.selectElementsBy(filterType, filterExpr, elements);
1790
* Find an element by class
1792
BrowserBot.prototype.locateElementByClass = function(locator, document) {
1793
return elementFindFirstMatchingChild(document,
1795
return element.className == locator
1801
* Find an element by alt
1803
BrowserBot.prototype.locateElementByAlt = function(locator, document) {
1804
return elementFindFirstMatchingChild(document,
1806
return element.alt == locator
1812
* Find an element by css selector
1814
BrowserBot.prototype.locateElementByCss = function(locator, document) {
1815
var elements = eval_css(locator, document);
1816
if (elements.length != 0)
1822
* This function is responsible for mapping a UI specifier string to an element
1823
* on the page, and returning it. If no element is found, null is returned.
1824
* Returning null on failure to locate the element is part of the undocumented
1825
* API for locator strategies.
1827
BrowserBot.prototype.locateElementByUIElement = function(locator, inDocument) {
1828
// offset locators are delimited by "->", which is much simpler than the
1829
// previous scheme involving detecting the close-paren.
1830
var locators = locator.split(/->/, 2);
1832
var locatedElement = null;
1833
var pageElements = UIMap.getInstance()
1834
.getPageElements(locators[0], inDocument);
1836
if (locators.length > 1) {
1837
for (var i = 0; i < pageElements.length; ++i) {
1838
var locatedElements = eval_locator(locators[1], inDocument,
1840
if (locatedElements.length) {
1841
locatedElement = locatedElements[0];
1846
else if (pageElements.length) {
1847
locatedElement = pageElements[0];
1850
return locatedElement;
1853
BrowserBot.prototype.locateElementByUIElement.prefix = 'ui';
1855
// define a function used to compare the result of a close UI element
1856
// match with the actual interacted element. If they are close enough
1857
// according to the heuristic, consider them a match.
1859
* A heuristic function for comparing a node with a target node. Typically the
1860
* node is specified in a UI element definition, while the target node is
1861
* returned by the recorder as the leaf element which had some event enacted
1862
* upon it. This particular heuristic covers the case where the anchor element
1863
* contains other inline tags, such as "em" or "img".
1865
* @param node the node being compared to the target node
1866
* @param target the target node
1867
* @return true if node equals target, or if node is a link
1868
* element and target is its descendant, or if node has
1869
* an onclick attribute and target is its descendant.
1872
BrowserBot.prototype.locateElementByUIElement.is_fuzzy_match = function(node, target) {
1876
((node.nodeName == 'A' || node.onclick) && is_ancestor(node, target))
1885
/*****************************************************************/
1886
/* BROWSER-SPECIFIC FUNCTIONS ONLY AFTER THIS LINE */
1888
function MozillaBrowserBot(frame) {
1889
BrowserBot.call(this, frame);
1891
objectExtend(MozillaBrowserBot.prototype, BrowserBot.prototype);
1893
function KonquerorBrowserBot(frame) {
1894
BrowserBot.call(this, frame);
1896
objectExtend(KonquerorBrowserBot.prototype, BrowserBot.prototype);
1898
KonquerorBrowserBot.prototype.setIFrameLocation = function(iframe, location) {
1899
// Window doesn't fire onload event when setting src to the current value,
1900
// so we set it to blank first.
1901
iframe.src = "about:blank";
1902
iframe.src = location;
1905
KonquerorBrowserBot.prototype.setOpenLocation = function(win, loc) {
1906
// Window doesn't fire onload event when setting src to the current value,
1907
// so we just refresh in that case instead.
1908
loc = absolutify(loc, this.baseUrl);
1909
loc = canonicalize(loc);
1910
var startUrl = win.location.href;
1911
if ("about:blank" != win.location.href) {
1912
var startLoc = parseUrl(win.location.href);
1913
startLoc.hash = null;
1914
var startUrl = reassembleLocation(startLoc);
1916
LOG.debug("startUrl="+startUrl);
1917
LOG.debug("win.location.href="+win.location.href);
1918
LOG.debug("loc="+loc);
1919
if (startUrl == loc) {
1920
LOG.debug("opening exact same location");
1923
LOG.debug("locations differ");
1924
win.location.href = loc;
1926
// force the current polling thread to detect a page load
1927
var marker = this.isPollingForLoad(win);
1929
delete win.location[marker];
1933
KonquerorBrowserBot.prototype._isSameDocument = function(originalDocument, currentDocument) {
1934
// under Konqueror, there may be this case:
1935
// originalDocument and currentDocument are different objects
1936
// while their location are same.
1937
if (originalDocument) {
1938
return originalDocument.location == currentDocument.location
1940
return originalDocument === currentDocument;
1944
function SafariBrowserBot(frame) {
1945
BrowserBot.call(this, frame);
1947
objectExtend(SafariBrowserBot.prototype, BrowserBot.prototype);
1949
SafariBrowserBot.prototype.setIFrameLocation = KonquerorBrowserBot.prototype.setIFrameLocation;
1950
SafariBrowserBot.prototype.setOpenLocation = KonquerorBrowserBot.prototype.setOpenLocation;
1953
function OperaBrowserBot(frame) {
1954
BrowserBot.call(this, frame);
1956
objectExtend(OperaBrowserBot.prototype, BrowserBot.prototype);
1957
OperaBrowserBot.prototype.setIFrameLocation = function(iframe, location) {
1958
if (iframe.src == location) {
1959
iframe.src = location + '?reload';
1961
iframe.src = location;
1965
function IEBrowserBot(frame) {
1966
BrowserBot.call(this, frame);
1968
objectExtend(IEBrowserBot.prototype, BrowserBot.prototype);
1970
IEBrowserBot.prototype._handleClosedSubFrame = function(testWindow, doNotModify) {
1971
if (this.proxyInjectionMode) {
1976
testWindow.location.href;
1977
this.permDenied = 0;
1981
if (this._windowClosed(testWindow) || this.permDenied > 4) {
1982
if (this.isSubFrameSelected) {
1983
LOG.warn("Current subframe appears to have closed; selecting top frame");
1984
this.selectFrame("relative=top");
1985
return this.getCurrentWindow(doNotModify);
1987
var closedError = new SeleniumError("Current window or frame is closed!");
1988
closedError.windowClosed = true;
1995
IEBrowserBot.prototype.modifyWindowToRecordPopUpDialogs = function(windowToModify, browserBot) {
1996
BrowserBot.prototype.modifyWindowToRecordPopUpDialogs(windowToModify, browserBot);
1998
// we will call the previous version of this method from within our own interception
1999
oldShowModalDialog = windowToModify.showModalDialog;
2001
windowToModify.showModalDialog = function(url, args, features) {
2002
// Get relative directory to where TestRunner.html lives
2003
// A risky assumption is that the user's TestRunner is named TestRunner.html
2004
var doc_location = document.location.toString();
2005
var end_of_base_ref = doc_location.indexOf('TestRunner.html');
2006
var base_ref = doc_location.substring(0, end_of_base_ref);
2007
var runInterval = '';
2009
// Only set run interval if options is defined
2010
if (typeof(window.runOptions) != 'undefined') {
2011
runInterval = "&runInterval=" + runOptions.runInterval;
2014
var testRunnerURL = "TestRunner.html?auto=true&singletest="
2015
+ escape(browserBot.modalDialogTest)
2019
var fullURL = base_ref + testRunnerURL;
2020
browserBot.modalDialogTest = null;
2022
// If using proxy injection mode
2023
if (this.proxyInjectionMode) {
2024
var sessionId = runOptions.getSessionId();
2025
if (sessionId == undefined) {
2026
sessionId = injectedSessionId;
2028
if (sessionId != undefined) {
2029
LOG.debug("Invoking showModalDialog and injecting URL " + fullURL);
2033
var returnValue = oldShowModalDialog(fullURL, args, features);
2038
IEBrowserBot.prototype.modifySeparateTestWindowToDetectPageLoads = function(windowObject) {
2039
this.pageUnloading = false;
2041
var pageUnloadDetector = function() {
2042
self.pageUnloading = true;
2044
windowObject.attachEvent("onbeforeunload", pageUnloadDetector);
2045
BrowserBot.prototype.modifySeparateTestWindowToDetectPageLoads.call(this, windowObject);
2048
IEBrowserBot.prototype.pollForLoad = function(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker) {
2049
LOG.debug("IEBrowserBot.pollForLoad: " + marker);
2050
if (!this.permDeniedCount[marker]) this.permDeniedCount[marker] = 0;
2051
BrowserBot.prototype.pollForLoad.call(this, loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker);
2052
if (this.pageLoadError) {
2053
if (this.pageUnloading) {
2055
LOG.debug("pollForLoad UNLOADING (" + marker + "): caught exception while firing events on unloading page: " + this.pageLoadError.message);
2056
this.reschedulePoller(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker);
2057
this.pageLoadError = null;
2059
} else if (((this.pageLoadError.message == "Permission denied") || (/^Access is denied/.test(this.pageLoadError.message)))
2060
&& this.permDeniedCount[marker]++ < 8) {
2061
if (this.permDeniedCount[marker] > 4) {
2062
var canAccessThisWindow;
2063
var canAccessCurrentlySelectedWindow;
2065
windowObject.location.href;
2066
canAccessThisWindow = true;
2069
this.getCurrentWindow(true).location.href;
2070
canAccessCurrentlySelectedWindow = true;
2072
if (canAccessCurrentlySelectedWindow & !canAccessThisWindow) {
2073
LOG.debug("pollForLoad (" + marker + ") ABORTING: " + this.pageLoadError.message + " (" + this.permDeniedCount[marker] + "), but the currently selected window is fine");
2074
// returning without rescheduling
2075
this.pageLoadError = null;
2081
LOG.debug("pollForLoad (" + marker + "): " + this.pageLoadError.message + " (" + this.permDeniedCount[marker] + "), waiting to see if it goes away");
2082
this.reschedulePoller(loadFunction, windowObject, originalDocument, originalLocation, originalHref, marker);
2083
this.pageLoadError = null;
2086
//handy for debugging!
2087
//throw this.pageLoadError;
2091
IEBrowserBot.prototype._windowClosed = function(win) {
2094
// frame windows claim to be non-closed when their parents are closed
2095
// but you can't access their document objects in that case
2100
if (de.message == "Permission denied") {
2101
// the window is probably unloading, which means it's probably not closed yet
2104
else if (/^Access is denied/.test(de.message)) {
2105
// rare variation on "Permission denied"?
2106
LOG.debug("IEBrowserBot.windowClosed: got " + de.message + " (this.pageUnloading=" + this.pageUnloading + "); assuming window is unloading, probably not closed yet");
2109
// this is probably one of those frame window situations
2110
LOG.debug("IEBrowserBot.windowClosed: couldn't read win.document, assume closed: " + de.message + " (this.pageUnloading=" + this.pageUnloading + ")");
2116
LOG.debug("IEBrowserBot.windowClosed: win.closed was null, assuming closed");
2121
LOG.debug("IEBrowserBot._windowClosed: Got an exception trying to read win.closed; we'll have to take a guess!");
2123
if (browserVersion.isHTA) {
2124
if (e.message == "Permission denied") {
2125
// the window is probably unloading, which means it's not closed yet
2128
// there's a good chance that we've lost contact with the window object if it is closed
2132
// the window is probably unloading, which means it's not closed yet
2139
* In IE, getElementById() also searches by name - this is an optimisation for IE.
2141
IEBrowserBot.prototype.locateElementByIdentifer = function(identifier, inDocument, inWindow) {
2142
return inDocument.getElementById(identifier);
2145
SafariBrowserBot.prototype.modifyWindowToRecordPopUpDialogs = function(windowToModify, browserBot) {
2146
BrowserBot.prototype.modifyWindowToRecordPopUpDialogs(windowToModify, browserBot);
2148
var originalOpen = windowToModify.open;
2150
* Safari seems to be broken, so that when we manually trigger the onclick method
2151
* of a button/href, any window.open calls aren't resolved relative to the app location.
2152
* So here we replace the open() method with one that does resolve the url correctly.
2154
windowToModify.open = function(url, windowName, windowFeatures, replaceFlag) {
2156
if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("/")) {
2157
return originalOpen(url, windowName, windowFeatures, replaceFlag);
2160
// Reduce the current path to the directory
2161
var currentPath = windowToModify.location.pathname || "/";
2162
currentPath = currentPath.replace(/\/[^\/]*$/, "/");
2164
// Remove any leading "./" from the new url.
2165
url = url.replace(/^\.\//, "");
2167
newUrl = currentPath + url;
2169
var openedWindow = originalOpen(newUrl, windowName, windowFeatures, replaceFlag);
2170
LOG.debug("window.open call intercepted; window ID (which you can use with selectWindow()) is \"" + windowName + "\"");
2171
if (windowName!=null) {
2172
openedWindow["seleniumWindowName"] = windowName;
2174
return openedWindow;
2178
MozillaBrowserBot.prototype._fireEventOnElement = function(eventType, element, clientX, clientY) {
2179
var win = this.getCurrentWindow();
2180
triggerEvent(element, 'focus', false);
2182
// Add an event listener that detects if the default action has been prevented.
2183
// (This is caused by a javascript onclick handler returning false)
2184
// we capture the whole event, rather than the getPreventDefault() state at the time,
2185
// because we need to let the entire event bubbling and capturing to go through
2186
// before making a decision on whether we should force the href
2187
var savedEvent = null;
2189
element.addEventListener(eventType, function(evt) {
2193
this._modifyElementTarget(element);
2195
// Trigger the event.
2196
this.browserbot.triggerMouseEvent(element, eventType, true, clientX, clientY);
2198
if (this._windowClosed(win)) {
2202
// Perform the link action if preventDefault was set.
2203
// In chrome URL, the link action is already executed by triggerMouseEvent.
2204
if (!browserVersion.isChrome && savedEvent != null && !savedEvent.getPreventDefault()) {
2205
var targetWindow = this.browserbot._getTargetWindow(element);
2207
targetWindow.location.href = element.href;
2209
this.browserbot._handleClickingImagesInsideLinks(targetWindow, element);
2216
OperaBrowserBot.prototype._fireEventOnElement = function(eventType, element, clientX, clientY) {
2217
var win = this.getCurrentWindow();
2218
triggerEvent(element, 'focus', false);
2220
this._modifyElementTarget(element);
2222
// Trigger the click event.
2223
this.browserbot.triggerMouseEvent(element, eventType, true, clientX, clientY);
2225
if (this._windowClosed(win)) {
2232
KonquerorBrowserBot.prototype._fireEventOnElement = function(eventType, element, clientX, clientY) {
2233
var win = this.getCurrentWindow();
2234
triggerEvent(element, 'focus', false);
2236
this._modifyElementTarget(element);
2238
if (element[eventType]) {
2239
element[eventType]();
2242
this.browserbot.triggerMouseEvent(element, eventType, true, clientX, clientY);
2245
if (this._windowClosed(win)) {
2251
SafariBrowserBot.prototype._fireEventOnElement = function(eventType, element, clientX, clientY) {
2252
triggerEvent(element, 'focus', false);
2253
var wasChecked = element.checked;
2255
this._modifyElementTarget(element);
2257
// For form element it is simple.
2258
if (element[eventType]) {
2259
element[eventType]();
2261
// For links and other elements, event emulation is required.
2263
var targetWindow = this.browserbot._getTargetWindow(element);
2264
// todo: deal with anchors?
2265
this.browserbot.triggerMouseEvent(element, eventType, true, clientX, clientY);
2271
SafariBrowserBot.prototype.refresh = function() {
2272
var win = this.getCurrentWindow();
2273
if (win.location.hash) {
2274
// DGF Safari refuses to refresh when there's a hash symbol in the URL
2275
win.location.hash = "";
2276
var actuallyReload = function() {
2277
win.location.reload(true);
2279
window.setTimeout(actuallyReload, 1);
2281
win.location.reload(true);
2285
IEBrowserBot.prototype._fireEventOnElement = function(eventType, element, clientX, clientY) {
2286
var win = this.getCurrentWindow();
2287
triggerEvent(element, 'focus', false);
2289
var wasChecked = element.checked;
2291
// Set a flag that records if the page will unload - this isn't always accurate, because
2292
// <a href="javascript:alert('foo'):"> triggers the onbeforeunload event, even thought the page won't unload
2293
var pageUnloading = false;
2294
var pageUnloadDetector = function() {
2295
pageUnloading = true;
2297
win.attachEvent("onbeforeunload", pageUnloadDetector);
2298
this._modifyElementTarget(element);
2299
if (element[eventType]) {
2300
element[eventType]();
2303
this.browserbot.triggerMouseEvent(element, eventType, true, clientX, clientY);
2307
// If the page is going to unload - still attempt to fire any subsequent events.
2308
// However, we can't guarantee that the page won't unload half way through, so we need to handle exceptions.
2310
win.detachEvent("onbeforeunload", pageUnloadDetector);
2312
if (this._windowClosed(win)) {
2316
// Onchange event is not triggered automatically in IE.
2317
if (isDefined(element.checked) && wasChecked != element.checked) {
2318
triggerEvent(element, 'change', true);
2323
// If the page is unloading, we may get a "Permission denied" or "Unspecified error".
2324
// Just ignore it, because the document may have unloaded.
2325
if (pageUnloading) {
2326
LOG.logHook = function() {
2328
LOG.warn("Caught exception when firing events on unloading page: " + e.message);