detect and handle Moo-Tools-Comboboxes
This commit is contained in:
@@ -80,41 +80,13 @@ class Command: public QObject {
|
|||||||
QString result() {
|
QString result() {
|
||||||
return _result;
|
return _result;
|
||||||
}
|
}
|
||||||
protected:
|
static void realMouseClick(QWebFrame* frame, QString selector) {
|
||||||
void sleep(int s) {
|
QWebElement element(find(frame, selector));
|
||||||
QTime dieTime= QTime::currentTime().addSecs(s);
|
|
||||||
while (QTime::currentTime()<dieTime)
|
|
||||||
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
|
|
||||||
}
|
|
||||||
QWebElement find(QWebFrame* frame, QString selector, Logger& log,
|
|
||||||
int repeat = 2, int sleepsec = 1) {
|
|
||||||
QWebElement element;
|
|
||||||
element = find1(frame, selector, log, repeat, sleepsec);
|
|
||||||
if (!element.isNull()) return element;
|
|
||||||
element = find1(frame->page()->currentFrame(), selector, log, repeat, sleepsec);
|
|
||||||
if (!element.isNull()) return element;
|
|
||||||
element = find1(frame->page()->mainFrame(), selector, log, repeat, sleepsec);
|
|
||||||
if (!element.isNull()) return element;
|
|
||||||
return element;
|
|
||||||
}
|
|
||||||
QWebElement find1(QWebFrame* frame, QString selector, Logger& log,
|
|
||||||
int repeat = 5, int sleepsec = 1) {
|
|
||||||
QWebElement element;
|
|
||||||
for (int i=0; i<repeat; ++i) {
|
|
||||||
element = frame->findFirstElement(selector);
|
|
||||||
if (!element.isNull()) return element;
|
|
||||||
Q_FOREACH(QWebFrame* childFrame, frame->childFrames()) {
|
|
||||||
element = find1(childFrame, selector, log, 1, 0);
|
|
||||||
if (!element.isNull()) return element;
|
|
||||||
}
|
|
||||||
if (sleepsec) sleep(sleepsec);
|
|
||||||
}
|
|
||||||
return element;
|
|
||||||
}
|
|
||||||
void realMouseClick(QWebFrame* frame, QString selector, Logger& log) {
|
|
||||||
QWebElement element(find(frame, selector, log));
|
|
||||||
if (element.isNull()) throw ElementNotFound(selector);
|
if (element.isNull()) throw ElementNotFound(selector);
|
||||||
QWidget* web(frame->page()->view());
|
realMouseClick(element);
|
||||||
|
}
|
||||||
|
static void realMouseClick(const QWebElement& element) {
|
||||||
|
QWidget* web(element.webFrame()->page()->view());
|
||||||
QRect elGeom=element.geometry();
|
QRect elGeom=element.geometry();
|
||||||
QPoint elPoint=elGeom.center();
|
QPoint elPoint=elGeom.center();
|
||||||
int elX=elPoint.x();
|
int elX=elPoint.x();
|
||||||
@@ -129,8 +101,8 @@ class Command: public QObject {
|
|||||||
if (elY>webHeight)
|
if (elY>webHeight)
|
||||||
pixelsToScrolDown = //the +10 scrolls a bit further
|
pixelsToScrolDown = //the +10 scrolls a bit further
|
||||||
elY-webHeight+elGeom.height()/2+10;
|
elY-webHeight+elGeom.height()/2+10;
|
||||||
frame->setScrollBarValue(Qt::Horizontal, pixelsToScrolRight);
|
element.webFrame()->setScrollBarValue(Qt::Horizontal, pixelsToScrolRight);
|
||||||
frame->setScrollBarValue(Qt::Vertical, pixelsToScrolDown);
|
element.webFrame()->setScrollBarValue(Qt::Vertical, pixelsToScrolDown);
|
||||||
QPoint pointToClick(elX-pixelsToScrolRight, elY-pixelsToScrolDown);
|
QPoint pointToClick(elX-pixelsToScrolRight, elY-pixelsToScrolDown);
|
||||||
QMouseEvent pressEvent(QMouseEvent::MouseButtonPress,
|
QMouseEvent pressEvent(QMouseEvent::MouseButtonPress,
|
||||||
pointToClick, Qt::LeftButton, Qt::LeftButton,
|
pointToClick, Qt::LeftButton, Qt::LeftButton,
|
||||||
@@ -142,6 +114,39 @@ class Command: public QObject {
|
|||||||
QCoreApplication::sendEvent(web, &releaseEvent);
|
QCoreApplication::sendEvent(web, &releaseEvent);
|
||||||
QCoreApplication::processEvents();
|
QCoreApplication::processEvents();
|
||||||
}
|
}
|
||||||
|
static void sleep(int s) {
|
||||||
|
QTime dieTime= QTime::currentTime().addSecs(s);
|
||||||
|
while (QTime::currentTime()<dieTime)
|
||||||
|
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
|
||||||
|
}
|
||||||
|
public:
|
||||||
|
static QWebElement find(QWebFrame* frame, QString selector,
|
||||||
|
int repeat = 2, int sleepsec = 1) {
|
||||||
|
QWebElement element;
|
||||||
|
element = find1(frame, selector, repeat, sleepsec);
|
||||||
|
if (!element.isNull()) return element;
|
||||||
|
element = find1(frame->page()->currentFrame(), selector,
|
||||||
|
repeat, sleepsec);
|
||||||
|
if (!element.isNull()) return element;
|
||||||
|
element = find1(frame->page()->mainFrame(), selector,
|
||||||
|
repeat, sleepsec);
|
||||||
|
if (!element.isNull()) return element;
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
static QWebElement find1(QWebFrame* frame, QString selector,
|
||||||
|
int repeat = 5, int sleepsec = 1) {
|
||||||
|
QWebElement element;
|
||||||
|
for (int i=0; i<repeat; ++i) {
|
||||||
|
element = frame->findFirstElement(selector);
|
||||||
|
if (!element.isNull()) return element;
|
||||||
|
Q_FOREACH(QWebFrame* childFrame, frame->childFrames()) {
|
||||||
|
element = find1(childFrame, selector, 1, 0);
|
||||||
|
if (!element.isNull()) return element;
|
||||||
|
}
|
||||||
|
if (sleepsec) sleep(sleepsec);
|
||||||
|
}
|
||||||
|
return element;
|
||||||
|
}
|
||||||
void log(Script*);
|
void log(Script*);
|
||||||
bool _log;
|
bool _log;
|
||||||
protected:
|
protected:
|
||||||
@@ -715,7 +720,7 @@ class Do: public Command {
|
|||||||
}
|
}
|
||||||
bool execute(Script* script, QWebFrame* frame) {
|
bool execute(Script* script, QWebFrame* frame) {
|
||||||
Logger log(this, script);
|
Logger log(this, script);
|
||||||
QWebElement element(find(frame, _selector, log));
|
QWebElement element(find(frame, _selector));
|
||||||
if (element.isNull()) throw ElementNotFound(_selector);
|
if (element.isNull()) throw ElementNotFound(_selector);
|
||||||
_result =
|
_result =
|
||||||
element.evaluateJavaScript(script->replacevars(_javascript)).toString();
|
element.evaluateJavaScript(script->replacevars(_javascript)).toString();
|
||||||
@@ -986,7 +991,7 @@ class Upload: public Command {
|
|||||||
if (files.size()==1) filename=files[0];
|
if (files.size()==1) filename=files[0];
|
||||||
}
|
}
|
||||||
page->setNextUploadFile(filename);
|
page->setNextUploadFile(filename);
|
||||||
realMouseClick(frame, script->replacevars(_selector), log);
|
realMouseClick(frame, script->replacevars(_selector));
|
||||||
if (page->uploadPrepared())
|
if (page->uploadPrepared())
|
||||||
throw SetFileUploadFailed(script->replacevars(_selector), filename);
|
throw SetFileUploadFailed(script->replacevars(_selector), filename);
|
||||||
return true;
|
return true;
|
||||||
@@ -1032,7 +1037,7 @@ class Exists: public Command {
|
|||||||
if (text.isEmpty()) return true; // just find element
|
if (text.isEmpty()) return true; // just find element
|
||||||
if (element.toOuterXml().indexOf(text)!=-1) return true;
|
if (element.toOuterXml().indexOf(text)!=-1) return true;
|
||||||
}
|
}
|
||||||
QWebElement element(find(frame, selector, log));
|
QWebElement element(find(frame, selector));
|
||||||
if (text.isEmpty())
|
if (text.isEmpty())
|
||||||
throw AssertionFailed("element not found: "+selector);
|
throw AssertionFailed("element not found: "+selector);
|
||||||
else if (element.isNull())
|
else if (element.isNull())
|
||||||
@@ -1263,7 +1268,7 @@ class Click: public Command {
|
|||||||
}
|
}
|
||||||
bool execute(Script* script, QWebFrame* frame) {
|
bool execute(Script* script, QWebFrame* frame) {
|
||||||
Logger log(this, script);
|
Logger log(this, script);
|
||||||
realMouseClick(frame, script->replacevars(_selector), log);
|
realMouseClick(frame, script->replacevars(_selector));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
|
@@ -230,14 +230,14 @@ class TestGUI: public QMainWindow, protected Ui::TestGUI {
|
|||||||
if (reply->header(QNetworkRequest::ContentDispositionHeader).isValid()) {
|
if (reply->header(QNetworkRequest::ContentDispositionHeader).isValid()) {
|
||||||
QString part(reply->header(QNetworkRequest::ContentDispositionHeader)
|
QString part(reply->header(QNetworkRequest::ContentDispositionHeader)
|
||||||
.toString());
|
.toString());
|
||||||
if (part.contains(QRegExp("attachment; *filename="))) {
|
if (part.contains(QRegularExpression("attachment; *filename="))) {
|
||||||
part.replace(QRegExp(".*attachment; *filename="), "");
|
part.replace(QRegularExpression(".*attachment; *filename="), "");
|
||||||
if (part.size()) filename = part;
|
if (part.size()) filename = part;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
QString text(_testscript->toPlainText());
|
QString text(_testscript->toPlainText());
|
||||||
int pos1(text.lastIndexOf(QRegExp("^do ")));
|
int pos1(text.lastIndexOf(QRegularExpression("^do ")));
|
||||||
int pos2(text.lastIndexOf(QRegExp("^load ")));
|
int pos2(text.lastIndexOf(QRegularExpression("^load ")));
|
||||||
text.insert(pos1>pos2?pos1:pos2, "download "+filename);
|
text.insert(pos1>pos2?pos1:pos2, "download "+filename);
|
||||||
_testscript->setPlainText(text);
|
_testscript->setPlainText(text);
|
||||||
_testscript->moveCursor(QTextCursor::End);
|
_testscript->moveCursor(QTextCursor::End);
|
||||||
@@ -290,8 +290,29 @@ class TestGUI: public QMainWindow, protected Ui::TestGUI {
|
|||||||
case QEvent::MouseButtonRelease: {
|
case QEvent::MouseButtonRelease: {
|
||||||
enterText(true);
|
enterText(true);
|
||||||
_lastFocused=element;
|
_lastFocused=element;
|
||||||
if (_record->isChecked() && !element.isNull())
|
if (_record->isChecked() && !element.isNull()) {
|
||||||
_testscript->appendPlainText("click "+selector(_lastFocused));
|
QString selected(selector(_lastFocused));
|
||||||
|
QRegularExpressionMatch mooCombo
|
||||||
|
(QRegularExpression("^(#jform_[_A-Za-z0-9]+)_chzn>.*$")
|
||||||
|
.match(selected));
|
||||||
|
QRegularExpressionMatch mooComboItem
|
||||||
|
(QRegularExpression
|
||||||
|
("^li\\.highlighted(\\.result-selected)?\\.active-result$")
|
||||||
|
.match(selected));
|
||||||
|
if (mooCombo.hasMatch()) {
|
||||||
|
// special treatment for moo tools combobox (e.g. used in joomla)
|
||||||
|
_testscript->appendPlainText("click "+mooCombo.captured(1)+">a");
|
||||||
|
_testscript->appendPlainText("sleep 1");
|
||||||
|
} else if (mooComboItem.hasMatch()) {
|
||||||
|
// special treatment for item in moo tools combobox
|
||||||
|
_testscript->appendPlainText
|
||||||
|
("click li.active-result[data-option-array-index=\""
|
||||||
|
+element.attribute("data-option-array-index")+"\"]");
|
||||||
|
_testscript->appendPlainText("sleep 1");
|
||||||
|
} else {
|
||||||
|
_testscript->appendPlainText("click "+selected);
|
||||||
|
}
|
||||||
|
}
|
||||||
} break;
|
} break;
|
||||||
case QEvent::InputMethodQuery:
|
case QEvent::InputMethodQuery:
|
||||||
case QEvent::ToolTipChange:
|
case QEvent::ToolTipChange:
|
||||||
@@ -339,7 +360,13 @@ class TestGUI: public QMainWindow, protected Ui::TestGUI {
|
|||||||
}
|
}
|
||||||
if (event) { // try to find element using mouse position
|
if (event) { // try to find element using mouse position
|
||||||
QWebFrame* frame(_web->page()->frameAt(event->pos()));
|
QWebFrame* frame(_web->page()->frameAt(event->pos()));
|
||||||
if (frame) return frame->hitTestContent(event->pos()).element();
|
if (frame) {
|
||||||
|
QWebHitTestResult hit(frame->hitTestContent(event->pos()));
|
||||||
|
if (!hit.element().isNull())
|
||||||
|
return hit.element();
|
||||||
|
if (!hit.enclosingBlockElement().isNull())
|
||||||
|
return hit.enclosingBlockElement();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return QWebElement();
|
return QWebElement();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user