1
1
mirror of https://github.com/ariya/phantomjs.git synced 2024-09-11 12:55:33 +03:00

Support contextmenu events using webpage.sendEvent('contextmenu')

Phantomjs issue
https://github.com/ariya/phantomjs/issues/11429

Fixes #11429 by allowing to send a "contextmenu" event. Intentionally
does not map a "click" event using "right" button into contextmenu event
because this would be inconsistent with how browsers work.

Related issue in Ghostdriver:
https://github.com/detro/ghostdriver/issues/125
This commit is contained in:
Artur Signell 2016-01-07 18:47:33 +02:00 committed by Vitaly Slobodin
parent cab28ccbd4
commit 20d673bf20
2 changed files with 56 additions and 0 deletions

View File

@ -33,6 +33,7 @@
#include <math.h>
#include <QApplication>
#include <QContextMenuEvent>
#include <QDesktopServices>
#include <QDateTime>
#include <QDir>
@ -1504,6 +1505,29 @@ void WebPage::sendEvent(const QString& type, const QVariant& arg1, const QVarian
return;
}
// context click
if (type == "contextmenu") {
QContextMenuEvent::Reason reason = QContextMenuEvent::Mouse;
// Gather coordinates
if (arg1.isValid() && arg2.isValid()) {
m_mousePos.setX(arg1.toInt());
m_mousePos.setY(arg2.toInt());
}
// Prepare the context menu event
qDebug() << "Context Menu Event:" << eventType << "(" << reason << "," << m_mousePos << ")";
QContextMenuEvent* event = new QContextMenuEvent(reason, m_mousePos, QCursor::pos(), keyboardModifiers);
// Post and process events
// Send the context menu event directly to QWebPage::swallowContextMenuEvent which forwards it to JS
// If we fire the event using postEvent to m_customWebPage, it will end up in QWebPagePrivate::contextMenuEvent,
// which will not forward it to JS at all
m_customWebPage->swallowContextMenuEvent(event);
return;
}
// mouse click events: Qt doesn't provide this as a separate events,
// so we compose it with a mousedown/mouseup sequence
// mouse doubleclick events: It is not enough to simply send a

View File

@ -0,0 +1,32 @@
test(function () {
var page = require('webpage').create();
page.evaluate(function() {
window.addEventListener('contextmenu', function(event) {
window.loggedEvent = window.loggedEvent || {};
window.loggedEvent.contextmenu = event;
}, false);
});
page.sendEvent('contextmenu', 42, 217);
var event = page.evaluate(function() {
return window.loggedEvent;
});
assert_equals(event.contextmenu.clientX, 42);
assert_equals(event.contextmenu.clientY, 217);
// click with modifier key
page.evaluate(function() {
window.addEventListener('contextmenu', function(event) {
window.loggedEvent = window.loggedEvent || {};
window.loggedEvent.contextmenu = event;
}, false);
});
page.sendEvent('contextmenu', 100, 100, 'left', page.event.modifier.shift);
var event = page.evaluate(function() {
return window.loggedEvent.contextmenu;
});
assert_is_true(event.shiftKey);
}, "context click events");