|
|
|
@ -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: |
|
|
|
|