3
我想要做的就是硒的「clickAt()」方法正在做什麼。它不同於selenium和普通的javascript命令「click()」。JavaScript代碼背後的硒「clickAt()」方法?
因爲「clickAt」正在爲正常的「點擊」 - 方法不是時的ajax測試工作。
比方說,我想做一個clickAt,在與ID的元素:身份識別碼
我怎樣才能做到這一點?
我想要做的就是硒的「clickAt()」方法正在做什麼。它不同於selenium和普通的javascript命令「click()」。JavaScript代碼背後的硒「clickAt()」方法?
因爲「clickAt」正在爲正常的「點擊」 - 方法不是時的ajax測試工作。
比方說,我想做一個clickAt,在與ID的元素:身份識別碼
我怎樣才能做到這一點?
我想你正在尋找的是這樣的:
/* Fire a mouse event in a browser-compatible manner */
BrowserBot.prototype.triggerMouseEvent = function(element, eventType, canBubble, clientX, clientY, button) {
clientX = clientX ? clientX : 0;
clientY = clientY ? clientY : 0;
LOG.debug("triggerMouseEvent assumes setting screenX and screenY to 0 is ok");
var screenX = 0;
var screenY = 0;
canBubble = (typeof(canBubble) == undefined) ? true : canBubble;
var evt;
if (element.fireEvent && element.ownerDocument && element.ownerDocument.createEventObject) { //IE
evt = createEventObject(element, this.controlKeyDown, this.altKeyDown, this.shiftKeyDown, this.metaKeyDown);
evt.detail = 0;
evt.button = button ? button : 1; // default will be the left mouse click (http://www.javascriptkit.com/jsref/event.shtml)
evt.relatedTarget = null;
if (!screenX && !screenY && !clientX && !clientY && !this.controlKeyDown && !this.altKeyDown && !this.shiftKeyDown && !this.metaKeyDown) {
element.fireEvent('on' + eventType);
}
else {
evt.screenX = screenX;
evt.screenY = screenY;
evt.clientX = clientX;
evt.clientY = clientY;
// when we go this route, window.event is never set to contain the event we have just created.
// ideally we could just slide it in as follows in the try-block below, but this normally
// doesn't work. This is why I try to avoid this code path, which is only required if we need to
// set attributes on the event (e.g., clientX).
try {
window.event = evt;
}
catch(e) {
// getting an "Object does not support this action or property" error. Save the event away
// for future reference.
// TODO: is there a way to update window.event?
// work around for http://jira.openqa.org/browse/SEL-280 -- make the event available somewhere:
selenium.browserbot.getCurrentWindow().selenium_event = evt;
}
element.fireEvent('on' + eventType, evt);
}
}
else {
evt = document.createEvent('MouseEvents');
if (evt.initMouseEvent)
{
// see http://developer.mozilla.org/en/docs/DOM:event.button and
// http://developer.mozilla.org/en/docs/DOM:event.initMouseEvent for button ternary logic logic
//Safari
evt.initMouseEvent(eventType, canBubble, true, document.defaultView, 1, screenX, screenY, clientX, clientY,
this.controlKeyDown, this.altKeyDown, this.shiftKeyDown, this.metaKeyDown, button ? button : 0, null);
}
else {
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");
evt.initEvent(eventType, canBubble, true);
evt.shiftKey = this.shiftKeyDown;
evt.metaKey = this.metaKeyDown;
evt.altKey = this.altKeyDown;
evt.ctrlKey = this.controlKeyDown;
if(button)
{
evt.button = button;
}
}
element.dispatchEvent(evt);
}
};
這來自於硒源代碼,請訪問:
http://selenium.googlecode.com/svn/trunk/javascript/selenium-core/scripts/selenium-browserbot.js