Skip to content

Commit

Permalink
#42 #26 - frame / popup to work in for loop
Browse files Browse the repository at this point in the history
- fixed bug that can mess up complicated flows with multiple loop calls

- frame and popup previously don’t work in for loop. now it can be used
in for loop. however, no code blocks for frame / popup. just call
multiple times in the for loop. can become very messy and harder for
user to debug issues if try to implement that now.

frame name
step a
frame name
step b
  • Loading branch information
kensoh committed Aug 7, 2017
1 parent e69f4e4 commit 85e77cd
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 57 deletions.
25 changes: 16 additions & 9 deletions src/tagui_parse.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,14 @@ function end_tx($locator) { // helper function to return ending string for handl
return "},\nfunction timeout() {this.echo('ERROR - cannot find ".
$locator."').exit();});}".end_fi()."});\n\ncasper.then(function() {\n";
else if ($GLOBALS['inside_code_block']==0)
{$GLOBALS['inside_while_loop'] = 0; // reset inside_while_loop if not inside code block
$GLOBALS['for_loop_tracker'] = ""; // reset for_loop_tracker if not inside code block
{$GLOBALS['inside_while_loop'] = 0; // reset inside_while_loop if not inside block
$GLOBALS['for_loop_tracker'] = ""; // reset for_loop_tracker if not inside block
return "}});\n\ncasper.then(function() {\n";} else return "}\n";}

function end_fi() { $end_step = ""; // helper function to end frame_intent and popup_intent by closing parsed step block
if ($GLOBALS['inside_code_block']>0) return ""; // don't return frame or popup closure when inside code block
if ($GLOBALS['inside_code_block'] == 0) $GLOBALS['inside_while_loop'] = 0; // reset inside_while_loop if not inside block
if ($GLOBALS['inside_code_block'] == 0) $GLOBALS['for_loop_tracker'] = ""; // reset for_loop_tracker if not inside block
if (($GLOBALS['inside_code_block'] > 0) and ($GLOBALS['for_loop_tracker'] == "") and ($GLOBALS['inside_while_loop'] == 0)) return ""; // check for code block for frame or popup then exit, don't support frame/popup code blocks within loops
if (($GLOBALS['inside_popup'] == 1) or ($GLOBALS['inside_frame'] != 0)) $end_step = "});\n\ncasper.then(function() {";
if ($GLOBALS['inside_popup'] == 1) {$GLOBALS['inside_popup']=0; $popup_exit = " });} ";} else $popup_exit = "";
if ($GLOBALS['inside_frame'] == 0) {return "".$popup_exit.$end_step;} // form exit brackets for frame and popup
Expand Down Expand Up @@ -521,26 +523,31 @@ function timeout_intent($raw_intent) {
else return "casper.options.waitTimeout = " . (floatval($params)*1000) . ";" . end_fi()."\n";}

function code_intent($raw_intent) {
$params = parse_condition($raw_intent); return $params.end_fi()."\n";}
$params = parse_condition($raw_intent);
// not relevant to call end_fi for condition statement, will reset for and while loop tracker prematurely
if ((substr($params,0,3)=="if ") or (substr($params,0,8)=="else if ")
or (substr($params,0,4)=="for ") or (substr($params,0,6)=="while "))
return $params."\n"; else return $params.end_fi()."\n";}

function parse_condition($logic) { // natural language handling for conditions
$raw_logic = $logic; // store an original copy for use when showing error message
if (substr($logic,0,2)=="//") return $logic; // skip processing for comment

// section 1 - replace braces block {} with casperjs block to group steps or code
if ((substr($logic,0,1) == "{") or (substr($logic,0,1) == "}"))
// take only lines starting with { or } as code blocks for processing, otherwise will break many valid javascript code
{$GLOBALS['inside_code_block'] += substr_count($logic,"{"); $GLOBALS['inside_code_block'] -= substr_count($logic,"}");}
if (substr($logic,0,1) == "{") $GLOBALS['inside_code_block']++; // assume nothing on rest of the line except comment
if (substr($logic,0,1) == "}") $GLOBALS['inside_code_block']--; // assume nothing on rest of the line except comment
if ($GLOBALS['inside_while_loop']==0) { // while loop check as casper.then will hang while loop, recommend use for loop
$for_loop_header = ""; $for_loop_footer = ""; // add provision to implement IIFE pattern if inside for loop code block
if ($GLOBALS['for_loop_tracker']!="") { // otherwise the number variable used by for loop will be a wrong static number
$last_delimiter_pos = strrpos($GLOBALS['for_loop_tracker'],"|");
$for_loop_variable_name = substr($GLOBALS['for_loop_tracker'],$last_delimiter_pos+1);
$for_loop_header = "\n// start of IIFE pattern\n(function (" . $for_loop_variable_name . ") {";
$for_loop_footer = "})(" . $for_loop_variable_name . "); // end of IIFE pattern\n});\n\ncasper.then(function() {";
if (strpos($logic,"}")!==false) $GLOBALS['for_loop_tracker']=substr($GLOBALS['for_loop_tracker'],0,$last_delimiter_pos);}
$logic = str_replace("{",$for_loop_header."\n// start of code block\n{casper.then(function() {\n",$logic);
$logic = str_replace("}","})} // end of code block\n".$for_loop_footer,$logic);}
if (substr($logic,0,1) == "}") $GLOBALS['for_loop_tracker']=substr($GLOBALS['for_loop_tracker'],0,$last_delimiter_pos);}
if (substr($logic,0,1) == "{")
$logic = $for_loop_header."\n// start of code block\n{casper.then(function() {\n".substr($logic,1);
else if (substr($logic,0,1) == "}") $logic = "})} // end of code block\n".$for_loop_footer.substr($logic,1);}

// section 2 - natural language handling for conditions and loops
if ((substr($logic,0,3)=="if ") or (substr($logic,0,8)=="else if ")
Expand Down
25 changes: 14 additions & 11 deletions src/test/positive_test
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
http://www.google.com
https://www.google.com
// code to auto-replace local path names in generated js file with /full_path
var fs = require('fs');
var js_result = fs.read('test' + fs.separator + 'positive_test.js');
Expand All @@ -7,11 +7,14 @@ local_path = local_path.substring(0,local_path.indexOf("'"));
var regex = new RegExp(local_path,"g"); js_result = js_result.replace(regex,"/full_path");
fs.write('test' + fs.separator + 'positive_test.js', js_result, 'w');
fs.remove('test' + fs.separator + 'positive_test.log');
this.exit(); // don't actually run the steps and code, test is for language conversion
// add a navigation step in order to exit in time and block below output from running
https://ca.yahoo.com

// TEST CONDITIONS

// test contain / not contain
var variable = "test variable with some text";
variable = "test variable with some text"
if variable contain "some text"
echo "FOUND"
if variable contains "some text"
Expand All @@ -20,7 +23,7 @@ if variable not contain "some text"
echo "NOT FOUND"
if variable not contains "some text"
echo "NOT FOUND"
var variable ='test variable with some text';
variable = 'test variable with some text'
if variable contain 'some text'
echo 'FOUND'
if variable contains 'some text'
Expand All @@ -31,7 +34,7 @@ if variable not contains 'some text'
echo 'NOT FOUND'

// test equal to / not equal to
var variable = "lemon";
variable = "lemon"
if variable equal to "lemon"
echo "LEMON"
if variable equals to "lemon"
Expand All @@ -48,7 +51,7 @@ if variable not equal to 'lemon'
echo 'NOT LEMON'
if variable not equals to 'lemon'
echo 'NOT LEMON'
var variable = 12345;
variable = 12345
if variable equal to 12345
echo "12345"
if variable equals to 12345
Expand All @@ -59,7 +62,7 @@ if variable not equals to 12345
echo "NOT 12345"

// test more than / greater than / higher than
var variable = 5;
variable = 5
if variable more than 3
echo "MORE THAN 3"
if variable greater than 3
Expand All @@ -68,7 +71,7 @@ if variable higher than 3
echo "MORE THAN 3"

// test more than or equal to / greater than or equal to / higher than or equal to
var variable = 5;
variable = 5
if variable more than or equal to 5
echo "MORE THAN OR EQUAL TO 5"
if variable greater than or equal to 5
Expand All @@ -77,7 +80,7 @@ if variable higher than or equal to 5
echo "MORE THAN OR EQUAL TO 5"

// test less than / lesser than / lower than
var variable = 3;
variable = 3
if variable less than 5
echo "LESS THAN 5"
if variable lesser than 5
Expand All @@ -86,7 +89,7 @@ if variable lower than 5
echo "LESS THAN 5"

// test less than or equal to / lesser than or equal to / lower than or equal to
var variable = 5;
variable = 5
if variable less than or equal to 5
echo "LESS THAN OR EQUAL TO 5"
if variable lesser than or equal to 5
Expand All @@ -111,7 +114,7 @@ if 5 lesser than 3 or 3 lesser than 4 and "kiwi" equals to "kiwi"
echo "TRUE"

// test combinations of conditions
var menu = "fruits and salads";
menu = "fruits and salads"
if menu contains 'fruits' and menu not contains "potatoes"
echo "FRUITS ONLY"

Expand Down Expand Up @@ -171,7 +174,7 @@ click test3
}

// test {} while loop simple
var number = 1;
number = 1
while (number <= 5)
{
click test
Expand Down

0 comments on commit 85e77cd

Please sign in to comment.