detect and handle Moo-Tools-Comboboxes

master
Marc Wäckerlin 10 years ago
parent fb8aa83f25
commit c5b5c456ac
  1. 85
      src/commands.hxx
  2. 41
      src/testgui.hxx

@ -80,41 +80,13 @@ class Command: public QObject {
QString result() {
return _result;
}
protected:
void sleep(int s) {
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));
static void realMouseClick(QWebFrame* frame, QString selector) {
QWebElement element(find(frame, 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();
QPoint elPoint=elGeom.center();
int elX=elPoint.x();
@ -129,8 +101,8 @@ class Command: public QObject {
if (elY>webHeight)
pixelsToScrolDown = //the +10 scrolls a bit further
elY-webHeight+elGeom.height()/2+10;
frame->setScrollBarValue(Qt::Horizontal, pixelsToScrolRight);
frame->setScrollBarValue(Qt::Vertical, pixelsToScrolDown);
element.webFrame()->setScrollBarValue(Qt::Horizontal, pixelsToScrolRight);
element.webFrame()->setScrollBarValue(Qt::Vertical, pixelsToScrolDown);
QPoint pointToClick(elX-pixelsToScrolRight, elY-pixelsToScrolDown);
QMouseEvent pressEvent(QMouseEvent::MouseButtonPress,
pointToClick, Qt::LeftButton, Qt::LeftButton,
@ -142,6 +114,39 @@ class Command: public QObject {
QCoreApplication::sendEvent(web, &releaseEvent);
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*);
bool _log;
protected:
@ -715,7 +720,7 @@ class Do: public Command {
}
bool execute(Script* script, QWebFrame* frame) {
Logger log(this, script);
QWebElement element(find(frame, _selector, log));
QWebElement element(find(frame, _selector));
if (element.isNull()) throw ElementNotFound(_selector);
_result =
element.evaluateJavaScript(script->replacevars(_javascript)).toString();
@ -986,7 +991,7 @@ class Upload: public Command {
if (files.size()==1) filename=files[0];
}
page->setNextUploadFile(filename);
realMouseClick(frame, script->replacevars(_selector), log);
realMouseClick(frame, script->replacevars(_selector));
if (page->uploadPrepared())
throw SetFileUploadFailed(script->replacevars(_selector), filename);
return true;
@ -1032,7 +1037,7 @@ class Exists: public Command {
if (text.isEmpty()) return true; // just find element
if (element.toOuterXml().indexOf(text)!=-1) return true;
}
QWebElement element(find(frame, selector, log));
QWebElement element(find(frame, selector));
if (text.isEmpty())
throw AssertionFailed("element not found: "+selector);
else if (element.isNull())
@ -1263,7 +1268,7 @@ class Click: public Command {
}
bool execute(Script* script, QWebFrame* frame) {
Logger log(this, script);
realMouseClick(frame, script->replacevars(_selector), log);
realMouseClick(frame, script->replacevars(_selector));
return true;
}
private:

@ -230,14 +230,14 @@ class TestGUI: public QMainWindow, protected Ui::TestGUI {
if (reply->header(QNetworkRequest::ContentDispositionHeader).isValid()) {
QString part(reply->header(QNetworkRequest::ContentDispositionHeader)
.toString());
if (part.contains(QRegExp("attachment; *filename="))) {
part.replace(QRegExp(".*attachment; *filename="), "");
if (part.contains(QRegularExpression("attachment; *filename="))) {
part.replace(QRegularExpression(".*attachment; *filename="), "");
if (part.size()) filename = part;
}
}
QString text(_testscript->toPlainText());
int pos1(text.lastIndexOf(QRegExp("^do ")));
int pos2(text.lastIndexOf(QRegExp("^load ")));
int pos1(text.lastIndexOf(QRegularExpression("^do ")));
int pos2(text.lastIndexOf(QRegularExpression("^load ")));
text.insert(pos1>pos2?pos1:pos2, "download "+filename);
_testscript->setPlainText(text);
_testscript->moveCursor(QTextCursor::End);
@ -290,8 +290,29 @@ class TestGUI: public QMainWindow, protected Ui::TestGUI {
case QEvent::MouseButtonRelease: {
enterText(true);
_lastFocused=element;
if (_record->isChecked() && !element.isNull())
_testscript->appendPlainText("click "+selector(_lastFocused));
if (_record->isChecked() && !element.isNull()) {
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;
case QEvent::InputMethodQuery:
case QEvent::ToolTipChange:
@ -339,7 +360,13 @@ class TestGUI: public QMainWindow, protected Ui::TestGUI {
}
if (event) { // try to find element using mouse position
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();
}

Loading…
Cancel
Save