capybara-webkit/src/JavascriptInvocation.cpp

74 lines
2.3 KiB
C++
Raw Normal View History

#include "JavascriptInvocation.h"
2012-12-11 03:53:03 +00:00
#include "WebPage.h"
2013-01-20 23:57:04 +00:00
#include "InvocationResult.h"
2012-12-11 03:53:03 +00:00
#include <QApplication>
2012-12-11 03:53:03 +00:00
JavascriptInvocation::JavascriptInvocation(const QString &functionName, const QStringList &arguments, WebPage *page, QObject *parent) : QObject(parent) {
m_functionName = functionName;
m_arguments = arguments;
2012-12-11 03:53:03 +00:00
m_page = page;
}
QString &JavascriptInvocation::functionName() {
return m_functionName;
}
QStringList &JavascriptInvocation::arguments() {
return m_arguments;
}
2012-12-11 03:53:03 +00:00
2013-01-20 23:57:04 +00:00
QVariantMap JavascriptInvocation::getError() {
return m_error;
}
void JavascriptInvocation::setError(QVariantMap error) {
m_error = error;
}
InvocationResult JavascriptInvocation::invoke(QWebFrame *frame) {
frame->addToJavaScriptWindowObject("CapybaraInvocation", this);
QVariant result = frame->evaluateJavaScript("Capybara.invoke()");
if (getError().isEmpty())
return InvocationResult(result);
else
return InvocationResult(getError(), true);
}
bool JavascriptInvocation::click(QWebElement element, int left, int top, int width, int height) {
2012-12-11 03:53:03 +00:00
QRect elementBox(left, top, width, height);
QRect viewport(QPoint(0, 0), m_page->viewportSize());
QRect boundedBox = elementBox.intersected(viewport);
QPoint mousePos = boundedBox.center();
QString script = QString("Capybara.clickTest(this, %1, %2);").arg(mousePos.x()).arg(mousePos.y());
bool ok = element.evaluateJavaScript(script).toBool();
2012-12-11 03:53:03 +00:00
QWebFrame *parent = element.webFrame();
while (parent) {
elementBox.translate(parent->geometry().topLeft());
2012-12-11 03:53:03 +00:00
parent = parent->parentFrame();
}
boundedBox = elementBox.intersected(viewport);
mousePos = boundedBox.center();
QWebHitTestResult res = m_page->mainFrame()->hitTestContent(mousePos);
ok = ok && res.frame() == element.webFrame();
if (ok) {
execClick(mousePos);
}
return ok;
}
void JavascriptInvocation::execClick(QPoint mousePos) {
2012-12-11 03:53:03 +00:00
QMouseEvent event(QEvent::MouseMove, mousePos, Qt::NoButton, Qt::NoButton, Qt::NoModifier);
QApplication::sendEvent(m_page, &event);
event = QMouseEvent(QEvent::MouseButtonPress, mousePos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(m_page, &event);
event = QMouseEvent(QEvent::MouseButtonRelease, mousePos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(m_page, &event);
}