Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Render a mouse pointer when saving screenshots

* Helpful for ClickFailed errors
  • Loading branch information...
commit 9f2c71c5d2ed50aaf8f1473b9261cf2dc70cb3f0 1 parent 76a362c
Joe Ferris and Matt Horan authored jferris committed
View
25 src/JavascriptInvocation.cpp
@@ -39,40 +39,31 @@ InvocationResult JavascriptInvocation::invoke(QWebFrame *frame) {
void JavascriptInvocation::leftClick(int x, int y) {
QPoint mousePos(x, y);
- hover(mousePos);
- JavascriptInvocation::mouseEvent(QEvent::MouseButtonPress, mousePos, Qt::LeftButton);
- JavascriptInvocation::mouseEvent(QEvent::MouseButtonRelease, mousePos, Qt::LeftButton);
+ m_page->mouseEvent(QEvent::MouseButtonPress, mousePos, Qt::LeftButton);
+ m_page->mouseEvent(QEvent::MouseButtonRelease, mousePos, Qt::LeftButton);
}
void JavascriptInvocation::rightClick(int x, int y) {
QPoint mousePos(x, y);
- hover(mousePos);
- JavascriptInvocation::mouseEvent(QEvent::MouseButtonPress, mousePos, Qt::RightButton);
+ m_page->mouseEvent(QEvent::MouseButtonPress, mousePos, Qt::RightButton);
// swallowContextMenuEvent tries to fire contextmenu event in html page
QContextMenuEvent *event = new QContextMenuEvent(QContextMenuEvent::Mouse, mousePos);
m_page->swallowContextMenuEvent(event);
- JavascriptInvocation::mouseEvent(QEvent::MouseButtonRelease, mousePos, Qt::RightButton);
+ m_page->mouseEvent(QEvent::MouseButtonRelease, mousePos, Qt::RightButton);
}
void JavascriptInvocation::doubleClick(int x, int y) {
QPoint mousePos(x, y);
- JavascriptInvocation::mouseEvent(QEvent::MouseButtonDblClick, mousePos, Qt::LeftButton);
- JavascriptInvocation::mouseEvent(QEvent::MouseButtonRelease, mousePos, Qt::LeftButton);
-}
-
-void JavascriptInvocation::mouseEvent(QEvent::Type type, const QPoint &position, Qt::MouseButton button) {
- QMouseEvent event(type, position, button, button, Qt::NoModifier);
- QApplication::sendEvent(m_page, &event);
+ m_page->mouseEvent(QEvent::MouseButtonDblClick, mousePos, Qt::LeftButton);
+ m_page->mouseEvent(QEvent::MouseButtonRelease, mousePos, Qt::LeftButton);
}
bool JavascriptInvocation::clickTest(QWebElement element, int absoluteX, int absoluteY) {
- QPoint mousePos(absoluteX, absoluteY);
- QWebHitTestResult res = m_page->mainFrame()->hitTestContent(mousePos);
- return res.frame() == element.webFrame();
+ return m_page->clickTest(element, absoluteX, absoluteY);
}
QVariantMap JavascriptInvocation::clickPosition(QWebElement element, int left, int top, int width, int height) {
@@ -106,7 +97,7 @@ void JavascriptInvocation::hover(int absoluteX, int absoluteY) {
}
void JavascriptInvocation::hover(const QPoint &mousePos) {
- mouseEvent(QEvent::MouseMove, mousePos, Qt::NoButton);
+ m_page->mouseEvent(QEvent::MouseMove, mousePos, Qt::NoButton);
}
int JavascriptInvocation::keyCodeFor(const QChar &key) {
View
1  src/JavascriptInvocation.h
@@ -34,7 +34,6 @@ class JavascriptInvocation : public QObject {
QStringList m_arguments;
WebPage *m_page;
QVariant m_error;
- void mouseEvent(QEvent::Type type, const QPoint & position, Qt::MouseButton button);
void hover(const QPoint &);
int keyCodeFor(const QChar &);
};
View
18 src/WebPage.cpp
@@ -10,6 +10,7 @@
#include <iostream>
#include <QWebSettings>
#include <QUuid>
+#include <QApplication>
WebPage::WebPage(WebPageManager *manager, QObject *parent) : QWebPage(parent) {
m_loading = false;
@@ -214,6 +215,19 @@ QString WebPage::failureString() {
return message + m_errorPageMessage;
}
+void WebPage::mouseEvent(QEvent::Type type, const QPoint &position, Qt::MouseButton button) {
+ m_mousePosition = position;
+ QMouseEvent event(type, position, button, button, Qt::NoModifier);
+ QApplication::sendEvent(this, &event);
+}
+
+bool WebPage::clickTest(QWebElement element, int absoluteX, int absoluteY) {
+ QPoint mousePos(absoluteX, absoluteY);
+ m_mousePosition = mousePos;
+ QWebHitTestResult res = mainFrame()->hitTestContent(mousePos);
+ return res.frame() == element.webFrame();
+}
+
bool WebPage::render(const QString &fileName, const QSize &minimumSize) {
QFileInfo fileInfo(fileName);
QDir dir;
@@ -236,6 +250,10 @@ bool WebPage::render(const QString &fileName, const QSize &minimumSize) {
this->setViewportSize(pageSize);
this->mainFrame()->render(&p);
+
+ QImage pointer = QImage(":/pointer.png");
+ p.drawImage(m_mousePosition, pointer);
+
p.end();
this->setViewportSize(viewportSize);
View
3  src/WebPage.h
@@ -44,6 +44,8 @@ class WebPage : public QWebPage {
QStringList pageHeaders();
QByteArray body();
QString contentType();
+ void mouseEvent(QEvent::Type type, const QPoint &position, Qt::MouseButton button);
+ bool clickTest(QWebElement element, int absoluteX, int absoluteY);
public slots:
bool shouldInterruptJavaScript();
@@ -88,6 +90,7 @@ class WebPage : public QWebPage {
WebPageManager *m_manager;
QString m_errorPageMessage;
void setFrameProperties(QWebFrame *, QUrl &, NetworkReplyProxy *);
+ QPoint m_mousePosition;
};
#endif //_WEBPAGE_H
View
1  src/capybara.js
@@ -189,6 +189,7 @@ Capybara = {
var node = this.nodes[index];
node.scrollIntoViewIfNeeded();
var pos = this.clickPosition(node);
+ CapybaraInvocation.hover(pos.relativeX, pos.relativeY);
this.expectNodeAtPosition(node, pos);
action(pos.absoluteX, pos.absoluteY);
},
View
BIN  src/pointer.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
1  src/webkit_server.qrc
@@ -1,5 +1,6 @@
<RCC>
<qresource prefix="/">
<file>capybara.js</file>
+ <file>pointer.png</file>
</qresource>
</RCC>
Please sign in to comment.
Something went wrong with that request. Please try again.