mirror of https://github.com/vitalif/phantomjs
Improve the API for mouse events.
It now looks like in this example: page.sendEvent('mousedown', 42, 217); Other available events are 'mouseup', 'mousemove', and 'click'. They modelled after the DOM events. Unit tests are included. http://code.google.com/p/phantomjs/issues/detail?id=2341.3
parent
df93935d52
commit
4544b129c9
|
@ -16,6 +16,7 @@ Please see also http://code.google.com/p/phantomjs/wiki/ReleaseNotes.
|
|||
|
||||
* Added Filesystem API, based on CommonJS Filesystem draft specs (issue 129)
|
||||
* Added support for persistent cookies (issue 91)
|
||||
* Added event handling, currently only for mouse events (issue 234)
|
||||
* Added page scroll position (issue 162)
|
||||
* Added basic HTTP authentication support (issue 45)
|
||||
* Added callback for page initialization (issue 143)
|
||||
|
|
|
@ -522,36 +522,37 @@ void WebPage::_appendScriptElement(const QString &scriptUrl) {
|
|||
m_mainFrame->evaluateJavaScript( QString(JS_APPEND_SCRIPT_ELEMENT).arg(scriptUrl) );
|
||||
}
|
||||
|
||||
void WebPage::click( int x, int y )
|
||||
void WebPage::sendEvent(const QString &type, const QVariant &arg1, const QVariant &arg2)
|
||||
{
|
||||
mouseMoveTo(x,y);
|
||||
mouseDown();
|
||||
mouseUp();
|
||||
}
|
||||
if (type == "mousedown" || type == "mouseup" || type == "mousemove") {
|
||||
QMouseEvent::Type eventType = QEvent::None;
|
||||
Qt::MouseButton button = Qt::LeftButton;
|
||||
Qt::MouseButtons buttons = Qt::LeftButton;
|
||||
|
||||
void WebPage::mouseDown()
|
||||
{
|
||||
// qDebug() << "EventSender::mouseDown " << m_mousePos.x() << " " << m_mousePos.y();
|
||||
QMouseEvent* event = new QMouseEvent(QEvent::MouseButtonPress, m_mousePos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
|
||||
QApplication::postEvent( m_webPage, event);
|
||||
QApplication::processEvents();
|
||||
}
|
||||
if (type == "mousedown")
|
||||
eventType = QEvent::MouseButtonPress;
|
||||
if (type == "mouseup")
|
||||
eventType = QEvent::MouseButtonRelease;
|
||||
if (type == "mousemove") {
|
||||
eventType = QEvent::MouseMove;
|
||||
button = Qt::NoButton;
|
||||
buttons = Qt::NoButton;
|
||||
}
|
||||
Q_ASSERT(eventType != QEvent::None);
|
||||
|
||||
void WebPage::mouseUp()
|
||||
{
|
||||
// qDebug() << "EventSender::mouseUp " << m_mousePos.x() << " " << m_mousePos.y();
|
||||
QMouseEvent* event = new QMouseEvent(QEvent::MouseButtonRelease, m_mousePos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
|
||||
QApplication::postEvent(m_webPage, event);
|
||||
QApplication::processEvents();
|
||||
}
|
||||
int x = arg1.toInt();
|
||||
int y = arg2.toInt();
|
||||
QMouseEvent *event = new QMouseEvent(eventType, QPoint(x, y), button, buttons, Qt::NoModifier);
|
||||
QApplication::postEvent(m_webPage, event);
|
||||
QApplication::processEvents();
|
||||
return;
|
||||
}
|
||||
|
||||
void WebPage::mouseMoveTo(int x, int y)
|
||||
{
|
||||
// qDebug() << "EventSender::mouseMoveTo " << x << " " << y;
|
||||
m_mousePos = QPoint(x, y);
|
||||
QMouseEvent* event = new QMouseEvent(QEvent::MouseMove, m_mousePos, Qt::NoButton, Qt::NoButton, Qt::NoModifier);
|
||||
QApplication::postEvent(m_webPage, event);
|
||||
QApplication::processEvents();
|
||||
if (type == "click") {
|
||||
sendEvent("mousedown", arg1, arg2);
|
||||
sendEvent("mouseup", arg1, arg2);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
#include "webpage.moc"
|
||||
|
|
|
@ -81,10 +81,7 @@ public slots:
|
|||
bool injectJs(const QString &jsFilePath);
|
||||
void _appendScriptElement(const QString &scriptUrl);
|
||||
void uploadFile(const QString &selector, const QString &fileName);
|
||||
void click( int x, int y );
|
||||
void mouseDown();
|
||||
void mouseUp();
|
||||
void mouseMoveTo(int x, int y);
|
||||
void sendEvent(const QString &type, const QVariant &arg1 = QVariant(), const QVariant &arg2 = QVariant());
|
||||
|
||||
signals:
|
||||
void initialized();
|
||||
|
|
|
@ -65,7 +65,6 @@ describe("WebPage object", function() {
|
|||
expect(page.viewportSize.width).toEqual(400);
|
||||
});
|
||||
|
||||
expectHasFunction(page, 'click');
|
||||
expectHasFunction(page, 'deleteLater');
|
||||
expectHasFunction(page, 'destroyed');
|
||||
expectHasFunction(page, 'evaluate');
|
||||
|
@ -75,13 +74,109 @@ describe("WebPage object", function() {
|
|||
expectHasFunction(page, 'javaScriptConsoleMessageSent');
|
||||
expectHasFunction(page, 'loadFinished');
|
||||
expectHasFunction(page, 'loadStarted');
|
||||
expectHasFunction(page, 'mouseDown');
|
||||
expectHasFunction(page, 'mouseMoveTo');
|
||||
expectHasFunction(page, 'mouseUp');
|
||||
expectHasFunction(page, 'openUrl');
|
||||
expectHasFunction(page, 'release');
|
||||
expectHasFunction(page, 'render');
|
||||
expectHasFunction(page, 'resourceReceived');
|
||||
expectHasFunction(page, 'resourceRequested');
|
||||
expectHasFunction(page, 'uploadFile');
|
||||
|
||||
expectHasFunction(page, 'sendEvent');
|
||||
|
||||
it("should handle mousedown event", function() {
|
||||
runs(function() {
|
||||
page.evaluate(function() {
|
||||
window.addEventListener('mousedown', function(event) {
|
||||
window.loggedEvent = window.loggedEvent || {};
|
||||
window.loggedEvent.mousedown = event;
|
||||
}, false);
|
||||
});
|
||||
page.sendEvent('mousedown', 42, 217);
|
||||
});
|
||||
|
||||
waits(50);
|
||||
|
||||
runs(function() {
|
||||
var event = page.evaluate(function() {
|
||||
return window.loggedEvent.mousedown;
|
||||
});
|
||||
expect(event.clientX).toEqual(42);
|
||||
expect(event.clientY).toEqual(217);
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle mouseup event", function() {
|
||||
runs(function() {
|
||||
page.evaluate(function() {
|
||||
window.addEventListener('mouseup', function(event) {
|
||||
window.loggedEvent = window.loggedEvent || {};
|
||||
window.loggedEvent.mouseup = event;
|
||||
}, false);
|
||||
});
|
||||
page.sendEvent('mouseup', 14, 3);
|
||||
});
|
||||
|
||||
waits(50);
|
||||
|
||||
runs(function() {
|
||||
var event = page.evaluate(function() {
|
||||
return window.loggedEvent.mouseup;
|
||||
});
|
||||
expect(event.clientX).toEqual(14);
|
||||
expect(event.clientY).toEqual(3);
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle mousemove event", function() {
|
||||
runs(function() {
|
||||
page.evaluate(function() {
|
||||
window.addEventListener('mousemove', function(event) {
|
||||
window.loggedEvent = window.loggedEvent || {};
|
||||
window.loggedEvent.mousemove = event;
|
||||
}, false);
|
||||
});
|
||||
page.sendEvent('mousemove', 14, 7);
|
||||
});
|
||||
|
||||
waits(50);
|
||||
|
||||
runs(function() {
|
||||
var event = page.evaluate(function() {
|
||||
return window.loggedEvent.mousemove;
|
||||
});
|
||||
expect(event.clientX).toEqual(14);
|
||||
expect(event.clientY).toEqual(7);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it("should handle click event", function() {
|
||||
runs(function() {
|
||||
page.evaluate(function() {
|
||||
window.addEventListener('mousedown', function(event) {
|
||||
window.loggedEvent = window.loggedEvent || {};
|
||||
window.loggedEvent.mousedown = event;
|
||||
}, false);
|
||||
window.addEventListener('mouseup', function(event) {
|
||||
window.loggedEvent = window.loggedEvent || {};
|
||||
window.loggedEvent.mouseup = event;
|
||||
}, false);
|
||||
});
|
||||
page.sendEvent('click', 42, 217);
|
||||
});
|
||||
|
||||
waits(50);
|
||||
|
||||
runs(function() {
|
||||
var event = page.evaluate(function() {
|
||||
return window.loggedEvent;
|
||||
});
|
||||
expect(event.mouseup.clientX).toEqual(42);
|
||||
expect(event.mouseup.clientY).toEqual(217);
|
||||
expect(event.mousedown.clientX).toEqual(42);
|
||||
expect(event.mousedown.clientY).toEqual(217);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue