Skip to content

Commit

Permalink
#42 #26 - improve blocks, loops, live mode
Browse files Browse the repository at this point in the history
- improve code block and loop handling. to process { and } into casper
blocks if the lines start with { or }. otherwise will cause many valid
javascript code to throw error. such as when using chrome_step or
setting api_config. with this change, the js step is not needed to
avoid those errors. add more error checking for loops to show error at
parsing if { is written on the same line. this will guide users to type
{ on the next line to be auto-parsed into casperjs blocks.

- updated details for steps that are not relevant/supported in live
mode, add support for live mode navigate to url when running chrome
browser. for phantomjs, set window.location.href or using
casper.thenOpen will not work when event loop is not yet passed back to
main 'thread'. also checked that recent changes to steps have been
mirrored to live mode.

- add code in positive test to auto-cleanse local folder path instead
of manually searching and replacing
  • Loading branch information
kensoh committed Aug 7, 2017
1 parent 82a2180 commit e69f4e4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 26 deletions.
15 changes: 8 additions & 7 deletions src/tagui_header.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,8 @@ return "var fs = require('fs'); if (!sikuli_step('"+input_intent+"')) if (!fs.ex
"else this.echo('ERROR - cannot find "+input_params+" on screen');";}

function url_intent(raw_intent) {
return "this.echo('ERROR - step not supported in live mode')";}
if (chrome_id == 0) return "this.echo('ERROR - step only supported in live mode using Chrome browser')";
else return "this.evaluate(function() {window.location.href = \"" + raw_intent + "\"})";}

function tap_intent(raw_intent) {
var params = ((raw_intent + ' ').substr(1+(raw_intent + ' ').indexOf(' '))).trim();
Expand Down Expand Up @@ -559,7 +560,7 @@ if ((param1 == '') || (param2 == '')) return "this.echo('ERROR - url/filename mi
else return "this.download('" + param1 + "','" + abs_file(param2) + "')";}

function receive_intent(raw_intent) {
return "this.echo('ERROR - step not supported in live mode')";}
return "this.echo('ERROR - step not supported in live mode, it requires creating CasperJS event')";}

function echo_intent(raw_intent) {
var params = ((raw_intent + ' ').substr(1+(raw_intent + ' ').indexOf(' '))).trim();
Expand Down Expand Up @@ -604,22 +605,22 @@ else {if (check_tx(params)) return "this.captureSelector(snap_image(),tx('" + pa
else return "this.echo('ERROR - cannot find " + params + "')";}}

function wait_intent(raw_intent) {
return "this.echo('ERROR - step not supported in live mode')";}
return "this.echo('ERROR - waiting for some time is not relevant in live mode')";}

function live_intent(raw_intent) {
return "this.echo('ERROR - step not supported in live mode')";}
return "this.echo('ERROR - you are already in live mode, type done to quit live mode')";}

function check_intent(raw_intent) {
return "this.echo('ERROR - step not supported in live mode')";}
return "this.echo('ERROR - step not supported in live mode, there is no conditions language parser')";}

function test_intent(raw_intent) {
return "this.echo('ERROR - use CasperJS tester module to professionally " + raw_intent + "\\nERROR - info at http://docs.casperjs.org/en/latest/modules/tester.html\\nERROR - support CSS selector or tx(\\'selector\\') for XPath algo by TagUI')";}

function frame_intent(raw_intent) {
return "this.echo('ERROR - step not supported in live mode')";}
return "this.echo('ERROR - step not supported in live mode, it is meant for trying single steps')";}

function popup_intent(raw_intent) {
return "this.echo('ERROR - step not supported in live mode')";}
return "this.echo('ERROR - step not supported in live mode, it is meant for trying single steps')";}

function api_intent(raw_intent) {
var params = ((raw_intent + ' ').substr(1+(raw_intent + ' ').indexOf(' '))).trim();
Expand Down
19 changes: 11 additions & 8 deletions src/tagui_parse.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,9 +528,9 @@ function parse_condition($logic) { // natural language handling for conditions
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_count($raw_logic,"{")>1) echo "ERROR - " . current_line() . " multiple step { found - " . $raw_logic . "\n";
if (substr_count($raw_logic,"}")>1) echo "ERROR - " . current_line() . " multiple step } found - " . $raw_logic . "\n";
$GLOBALS['inside_code_block'] += substr_count($logic,"{"); $GLOBALS['inside_code_block'] -= substr_count($logic,"}");
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 ($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
Expand Down Expand Up @@ -594,21 +594,24 @@ function parse_condition($logic) { // natural language handling for conditions
$logic = str_replace("(","",$logic); $logic = str_replace(")","",$logic); // remove brackets if present
$logic = str_replace(" "," ",$logic); $logic = str_replace(" "," ",$logic); // remove typo extra spaces
$token = explode(" ",$logic); // split into tokens for loop in natural language, eg - for cupcake from 1 to 4
if (substr($raw_logic,-1)=="{") // show error to put { to next line for parsing as { step
if (strpos($raw_logic,"{")!==false) // show error to put { to next line for parsing as { step
echo "ERROR - " . current_line() . " put { to next line - " . $raw_logic . "\n";
else if (count($token) != 6) echo "ERROR - " . current_line() . " invalid for loop - " . $raw_logic . "\n";
else $logic = $token[0]." (".$token[1]."=".$token[3]."; ".$token[1]."<=".$token[5]."; ".$token[1]."++)";}
else if ((substr($logic,0,4)=="for ") and (substr($raw_logic,-1)=="{"))
else if ((substr($logic,0,4)=="for ") and (strpos($raw_logic,"{")!==false))
echo "ERROR - " . current_line() . " put { to next line - " . $raw_logic . "\n";

// add to tracker the for loop variable name, to implement IIFE pattern if step/code blocks are used
if (substr($logic,0,4)=="for ") { // get the variable name used in the for loop and append into tracker
$GLOBALS['for_loop_tracker'] .= "|" . (substr($logic,strpos($logic,"(")+1,strpos($logic,"=")-strpos($logic,"(")-1));}

// add opening and closing brackets twice to handle no brackets, and, or cases
if (substr($logic,0,3)=="if ") $logic = "if ((" . trim(substr($logic,3)) . "))";
if (substr($logic,0,8)=="else if ") $logic = "else if ((" . trim(substr($logic,8)) . "))";
if (substr($logic,0,6)=="while ") $logic = "while ((" . trim(substr($logic,6)) . "))";}
if (substr($logic,0,3)=="if ") {$logic = "if ((" . trim(substr($logic,3)) . "))";
if (strpos($raw_logic,"{")!==false) echo "ERROR - " . current_line() . " put { to next line - " . $raw_logic . "\n";}
if (substr($logic,0,8)=="else if ") {$logic = "else if ((" . trim(substr($logic,8)) . "))";
if (strpos($raw_logic,"{")!==false) echo "ERROR - " . current_line() . " put { to next line - " . $raw_logic . "\n";}
if (substr($logic,0,6)=="while ") {$logic = "while ((" . trim(substr($logic,6)) . "))";
if (strpos($raw_logic,"{")!==false) echo "ERROR - " . current_line() . " put { to next line - " . $raw_logic . "\n";}}

// section 3 - track if next statement is going to be or still inside while loop,
// then avoid async wait (casper.then/waitFor/timeout will hang casperjs/phantomjs)
Expand Down
5 changes: 3 additions & 2 deletions src/test/positive_test
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
http://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(fs.workingDirectory + fs.separator + 'test' + fs.separator + 'positive_test.js');
var js_result = fs.read('test' + fs.separator + 'positive_test.js');
var local_path = js_result.substring(js_result.indexOf("var flow_path = '") + 17);
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(fs.workingDirectory + fs.separator + 'test' + fs.separator + 'positive_test.js', js_result, 'w');
fs.write('test' + fs.separator + 'positive_test.js', js_result, 'w');
fs.remove('test' + fs.separator + 'positive_test.log');

// TEST CONDITIONS

Expand Down
22 changes: 13 additions & 9 deletions src/test/positive_test.signature
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,8 @@ return "var fs = require('fs'); if (!sikuli_step('"+input_intent+"')) if (!fs.ex
"else this.echo('ERROR - cannot find "+input_params+" on screen');";}

function url_intent(raw_intent) {
return "this.echo('ERROR - step not supported in live mode')";}
if (chrome_id == 0) return "this.echo('ERROR - step only supported in live mode using Chrome browser')";
else return "this.evaluate(function() {window.location.href = \"" + raw_intent + "\"})";}

function tap_intent(raw_intent) {
var params = ((raw_intent + ' ').substr(1+(raw_intent + ' ').indexOf(' '))).trim();
Expand Down Expand Up @@ -580,7 +581,7 @@ if ((param1 == '') || (param2 == '')) return "this.echo('ERROR - url/filename mi
else return "this.download('" + param1 + "','" + abs_file(param2) + "')";}

function receive_intent(raw_intent) {
return "this.echo('ERROR - step not supported in live mode')";}
return "this.echo('ERROR - step not supported in live mode, it requires creating CasperJS event')";}

function echo_intent(raw_intent) {
var params = ((raw_intent + ' ').substr(1+(raw_intent + ' ').indexOf(' '))).trim();
Expand Down Expand Up @@ -625,22 +626,22 @@ else {if (check_tx(params)) return "this.captureSelector(snap_image(),tx('" + pa
else return "this.echo('ERROR - cannot find " + params + "')";}}

function wait_intent(raw_intent) {
return "this.echo('ERROR - step not supported in live mode')";}
return "this.echo('ERROR - waiting for some time is not relevant in live mode')";}

function live_intent(raw_intent) {
return "this.echo('ERROR - step not supported in live mode')";}
return "this.echo('ERROR - you are already in live mode, type done to quit live mode')";}

function check_intent(raw_intent) {
return "this.echo('ERROR - step not supported in live mode')";}
return "this.echo('ERROR - step not supported in live mode, there is no conditions language parser')";}

function test_intent(raw_intent) {
return "this.echo('ERROR - use CasperJS tester module to professionally " + raw_intent + "\\nERROR - info at http://docs.casperjs.org/en/latest/modules/tester.html\\nERROR - support CSS selector or tx(\\'selector\\') for XPath algo by TagUI')";}

function frame_intent(raw_intent) {
return "this.echo('ERROR - step not supported in live mode')";}
return "this.echo('ERROR - step not supported in live mode, it is meant for trying single steps')";}

function popup_intent(raw_intent) {
return "this.echo('ERROR - step not supported in live mode')";}
return "this.echo('ERROR - step not supported in live mode, it is meant for trying single steps')";}

function api_intent(raw_intent) {
var params = ((raw_intent + ' ').substr(1+(raw_intent + ' ').indexOf(' '))).trim();
Expand Down Expand Up @@ -716,11 +717,14 @@ casper.start('http://www.google.com', function() {
techo('http://www.google.com' + ' - ' + this.getTitle() + '\n');});

casper.then(function() {
var js_result = fs.read(fs.workingDirectory + fs.separator + 'test' + fs.separator + 'positive_test.js');
// 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');
var local_path = js_result.substring(js_result.indexOf("var flow_path = '") + 17);
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(fs.workingDirectory + fs.separator + 'test' + fs.separator + 'positive_test.js', js_result, 'w');
fs.write('test' + fs.separator + 'positive_test.js', js_result, 'w');
fs.remove('test' + fs.separator + 'positive_test.log');
// TEST CONDITIONS
// test contain / not contain
var variable = "test variable with some text";
Expand Down

0 comments on commit e69f4e4

Please sign in to comment.