new command while, similar to if
This commit is contained in:
@@ -25,7 +25,7 @@
|
|||||||
;; echo $(webrunner -h | sed -n 's, COMMAND: ,,p') | sed 's, ,\\\\|,g'
|
;; echo $(webrunner -h | sed -n 's, COMMAND: ,,p') | sed 's, ,\\\\|,g'
|
||||||
(defconst wt-font-lock-keywords
|
(defconst wt-font-lock-keywords
|
||||||
(list
|
(list
|
||||||
'("^ *\\(auth\\|ca-certificate\\|call\\|case\\|check\\|clear-cookies\\|click\\|clicktype\\|client-certificate\\|do\\|download\\|echo\\|execute\\|exists\\|exit\\|expect\\|fail\\|for\\|function\\|if\\|ignoreto\\|include\\|label\\|load\\|not\\|offline-storage-path\\|open\\|screenshot\\|set\\|setvalue\\|sleep\\|testcase\\|testsuite\\|timeout\\|unset\\|upload\\) " . font-lock-builtin-face)
|
'("^ *\\(auth\\|ca-certificate\\|call\\|case\\|check\\|clear-cookies\\|click\\|clicktype\\|client-certificate\\|do\\|download\\|echo\\|execute\\|exists\\|exit\\|expect\\|fail\\|for\\|function\\|if\\|while\\|ignoreto\\|include\\|label\\|load\\|not\\|offline-storage-path\\|open\\|screenshot\\|set\\|setvalue\\|sleep\\|testcase\\|testsuite\\|timeout\\|unset\\|upload\\) " . font-lock-builtin-face)
|
||||||
'("^ *#.*$" . font-lock-comment-face))
|
'("^ *#.*$" . font-lock-comment-face))
|
||||||
"Highlighting expressions for Webtester")
|
"Highlighting expressions for Webtester")
|
||||||
|
|
||||||
|
103
src/commands.hxx
103
src/commands.hxx
@@ -2234,7 +2234,8 @@ class If: public CommandContainer {
|
|||||||
}
|
}
|
||||||
QString command() const {
|
QString command() const {
|
||||||
return tag()+" "+_variable+" "+_cmp+" "+_value
|
return tag()+" "+_variable+" "+_cmp+" "+_value
|
||||||
+(_script.get()?"\n "+_script->print().join("\n "):"");
|
+(_script.get()?"\n "+_script->print().join("\n "):"")
|
||||||
|
+(_else.get()?"\nelse\n "+_else->print().join("\n "):"");
|
||||||
}
|
}
|
||||||
std::shared_ptr<Command> parse(Script* script, QString args,
|
std::shared_ptr<Command> parse(Script* script, QString args,
|
||||||
QStringList& in, QString file, int line,
|
QStringList& in, QString file, int line,
|
||||||
@@ -2313,6 +2314,105 @@ class If: public CommandContainer {
|
|||||||
std::shared_ptr<Script> _else;
|
std::shared_ptr<Script> _else;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class While: public CommandContainer {
|
||||||
|
public:
|
||||||
|
QString tag() const {
|
||||||
|
return "while";
|
||||||
|
}
|
||||||
|
QString description() const {
|
||||||
|
return
|
||||||
|
tag()+" <variable> <cmp> <value>\n"
|
||||||
|
" <command1>\n"
|
||||||
|
" <command2>\n"
|
||||||
|
" <...>\n"
|
||||||
|
"\n\n"+
|
||||||
|
tag()+" <selector> -> <text>\n"
|
||||||
|
" <command1>\n"
|
||||||
|
" <command2>\n"
|
||||||
|
" <...>\n"
|
||||||
|
"\n\n"
|
||||||
|
"Repeats commands conditionally. "
|
||||||
|
"The first variant compares a variable to a value. "
|
||||||
|
"The comparision <cmp> can be = ! . ^ ~ < >, "
|
||||||
|
"which means equal, different, contains, contains not, match, "
|
||||||
|
"less (as integer), bigger (as integer). "
|
||||||
|
"Match allows a regular expression. "
|
||||||
|
"The second variant checks for a text in a selector, "
|
||||||
|
"similar to command exists. The text can be empty to just "
|
||||||
|
"check for the existence of a selector.";
|
||||||
|
}
|
||||||
|
QString command() const {
|
||||||
|
return tag()+" "+_variable+" "+_cmp+" "+_value
|
||||||
|
+(_script.get()?"\n "+_script->print().join("\n "):"");
|
||||||
|
}
|
||||||
|
std::shared_ptr<Command> parse(Script* script, QString args,
|
||||||
|
QStringList& in, QString file, int line,
|
||||||
|
int indent) {
|
||||||
|
std::shared_ptr<While> cmd(new While());
|
||||||
|
int pos(args.indexOf(QRegularExpression("[=!.^~<>]")));
|
||||||
|
int len(1);
|
||||||
|
if (args.contains("->")) {
|
||||||
|
pos = args.indexOf("->");
|
||||||
|
len = 2;
|
||||||
|
cmd->_cmp = "->";
|
||||||
|
} else {
|
||||||
|
if (pos<0) throw BadArgument(tag()+" needs a comparision, not: "+args);
|
||||||
|
cmd->_cmp = args[pos];
|
||||||
|
}
|
||||||
|
cmd->_variable = args.left(pos).trimmed();
|
||||||
|
cmd->_value = args.mid(pos+len).trimmed();
|
||||||
|
cmd->_script = cmd->subParser(script, subCommandBlock(in), file, line, indent);
|
||||||
|
return cmd;
|
||||||
|
}
|
||||||
|
bool execute(Script* script, QWebFrame* frame) {
|
||||||
|
Logger log(this, script, false);
|
||||||
|
QString value(script->replacevars(_value));
|
||||||
|
QString selector(script->replacevars(_variable));
|
||||||
|
for (bool check(true); check;) {
|
||||||
|
if (_cmp=="->") {
|
||||||
|
check = false;
|
||||||
|
Q_FOREACH(QWebElement element, frame->findAllElements(selector)) {
|
||||||
|
if (value.isEmpty() || // just find element
|
||||||
|
element.toOuterXml().indexOf(value)!=-1 ||
|
||||||
|
element.toPlainText().indexOf(value)!=-1) {
|
||||||
|
check = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log(QString("evaluated expression to ")+(check?"true":"false")+": "
|
||||||
|
+selector+" "+_cmp+" "+value);
|
||||||
|
} else {
|
||||||
|
switch (_cmp[0].toLatin1()) {
|
||||||
|
case '=': check = script->variable(_variable)==value;
|
||||||
|
break;
|
||||||
|
case '!': check = script->variable(_variable)!=value;
|
||||||
|
break;
|
||||||
|
case '.': check = script->variable(_variable).contains(value);
|
||||||
|
break;
|
||||||
|
case '^': check = !script->variable(_variable).contains(value);
|
||||||
|
break;
|
||||||
|
case '~': check =
|
||||||
|
script->variable(_variable).contains(QRegularExpression(value));
|
||||||
|
break;
|
||||||
|
case '<': check = script->variable(_variable).toInt()<value.toInt();
|
||||||
|
break;
|
||||||
|
case '>': check = script->variable(_variable).toInt()>value.toInt();
|
||||||
|
break;
|
||||||
|
default:;
|
||||||
|
}
|
||||||
|
log(QString("evaluated expression to ")+(check?"true":"false")+": "
|
||||||
|
+script->variable(_variable)+" "+_cmp+" "+value);
|
||||||
|
}
|
||||||
|
if (check) if (!runScript(log, this, _script, script, frame)) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
QString _variable;
|
||||||
|
QString _cmp;
|
||||||
|
QString _value;
|
||||||
|
};
|
||||||
|
|
||||||
class TestSuite: public Command {
|
class TestSuite: public Command {
|
||||||
public:
|
public:
|
||||||
QString tag() const {
|
QString tag() const {
|
||||||
@@ -3006,6 +3106,7 @@ inline void Script::initPrototypes() {
|
|||||||
add(new Function);
|
add(new Function);
|
||||||
add(new Call);
|
add(new Call);
|
||||||
add(new If);
|
add(new If);
|
||||||
|
add(new While);
|
||||||
add(new TestSuite);
|
add(new TestSuite);
|
||||||
add(new TestCase);
|
add(new TestCase);
|
||||||
add(new Check);
|
add(new Check);
|
||||||
|
@@ -19,7 +19,7 @@ class Highlighter: public QSyntaxHighlighter {
|
|||||||
void include(QString);
|
void include(QString);
|
||||||
public:
|
public:
|
||||||
Highlighter(QTextDocument *parent): QSyntaxHighlighter(parent) {
|
Highlighter(QTextDocument *parent): QSyntaxHighlighter(parent) {
|
||||||
QString commands="auth|ca-certificate|call|case|check|clear-cookies|click|clicktype|client-certificate|do|download|echo|execute|exists|exit|expect|fail|for|function|if|ignoreto|include|label|load|not|offline-storage-path|open|screenshot|set|setvalue|sleep|testcase|testsuite|timeout|unset|upload";
|
QString commands="auth|ca-certificate|call|case|check|clear-cookies|click|clicktype|client-certificate|do|download|echo|execute|exists|exit|expect|fail|for|function|if|while|ignoreto|include|label|load|not|offline-storage-path|open|screenshot|set|setvalue|sleep|testcase|testsuite|timeout|unset|upload";
|
||||||
_expressions<<Expression("^ *("+commands+")\\b").weight(QFont::Bold).fg(Qt::darkBlue)
|
_expressions<<Expression("^ *("+commands+")\\b").weight(QFont::Bold).fg(Qt::darkBlue)
|
||||||
<<Expression("^ *#.*$").weight(QFont::Bold).fg(Qt::black);
|
<<Expression("^ *#.*$").weight(QFont::Bold).fg(Qt::black);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user