From 84c60fecd42394df5d831577130ecc40b8296381 Mon Sep 17 00:00:00 2001 From: Ken Soh Date: Mon, 16 Jul 2018 20:56:53 +0800 Subject: [PATCH] #241 - improve use of variables - `variable` instead of '+variable+' more details in issue - #241 --- src/tagui_parse.php | 13 +++++++-- src/test/positive_test | 15 +++++++++++ src/test/positive_test.signature | 46 ++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/src/tagui_parse.php b/src/tagui_parse.php index 16bdaa04..57dcd25c 100755 --- a/src/tagui_parse.php +++ b/src/tagui_parse.php @@ -308,7 +308,7 @@ function parse_backticks($script_line) { // check for even number of ` to reduce false-positive because backtick syntax is supposed to be matching pairs if ((substr_count($script_line,'`') > 1) and (!(substr_count($script_line,'`') & 1))) { if ($GLOBALS['repo_count'] == 0) { - echo "ERROR - ".current_line()." no repository data for ".$script_line."\n"; + $script_line = parse_variables($script_line); } else { if (getenv('tagui_data_set')!==false) { $data_set = intval(getenv('tagui_data_set')); @@ -328,11 +328,20 @@ function parse_backticks($script_line) { $script_line = str_replace($repo_keyword, $repo_data_value, $script_line); } if (strpos($script_line,'`')!==false) { - echo "ERROR - ".current_line()." no repository data for ".$script_line."\n"; + $script_line = parse_variables($script_line); } } } return $script_line;} +function parse_variables($script_line) { // `variable` --> '+variable+' +$quote_token = "'+"; // token to alternate replacements for '+variable+' +for ($char_counter = 0; $char_counter < strlen($script_line); $char_counter++) { + if (substr($script_line,$char_counter,1) == "`") { + $script_line = substr_replace($script_line,$quote_token,$char_counter,1); + if ($quote_token == "'+") $quote_token = "+'"; else $quote_token = "'+"; + } +} return $script_line;} + function parse_closure($script_line) {switch($script_line) { // \\n is needed for py, r, vision as multi-line string needs to have \n escaped to work in javascript // replacement code for [END_OF_LINE] custom token to denote line break is done at py, r, vision intents diff --git a/src/test/positive_test b/src/test/positive_test index fc1f7c4d..8a757961 100644 --- a/src/test/positive_test +++ b/src/test/positive_test @@ -436,6 +436,7 @@ api http://www.dummytestsite.com/api?COLOR1=red&COLOR2=green&COLOR3=blue // test api with variables var COLOR1='red'; var COLOR2='green'; var COLOR3='blue'; api http://www.dummytestsite.com/api?COLOR1='+COLOR1+'&COLOR2='+COLOR2+'&COLOR3='+COLOR3+' +api http://www.dummytestsite.com/api?COLOR1=`COLOR1`&COLOR2=`COLOR2`&COLOR3=`COLOR3` // test run run face_recognition par1 par2 par3 @@ -557,6 +558,20 @@ http://www.dummytestsite.com // test url with variable target_url = "www.dummytestsite.com"; http://'+target_url+' +http://`target_url` + +// test use of variable +// old way for '+variable+' backward compatibility +locator = 'email_button' +click '+locator+' +number = 5 +click (//*[@id="test"])['+variable+'] + +// new way of using `variable` to denote variable +locator = 'email_button' +click `locator` +number = 5 +click (//*[@id="test"])[`variable`] // test present function if present('footer') diff --git a/src/test/positive_test.signature b/src/test/positive_test.signature index c4ba5343..77e77fa1 100644 --- a/src/test/positive_test.signature +++ b/src/test/positive_test.signature @@ -2631,6 +2631,10 @@ casper.then(function() {{techo('api http://www.dummytestsite.com/api?COLOR1='+CO api_result = ''; api_result = call_api('http://www.dummytestsite.com/api?COLOR1='+COLOR1+'&COLOR2='+COLOR2+'&COLOR3='+COLOR3+''); try {api_json = JSON.parse(api_result);} catch(e) {api_json = JSON.parse('null');}}}); +casper.then(function() {{techo('api http://www.dummytestsite.com/api?COLOR1='+COLOR1+'&COLOR2='+COLOR2+'&COLOR3='+COLOR3+''); +api_result = ''; api_result = call_api('http://www.dummytestsite.com/api?COLOR1='+COLOR1+'&COLOR2='+COLOR2+'&COLOR3='+COLOR3+''); +try {api_json = JSON.parse(api_result);} catch(e) {api_json = JSON.parse('null');}}}); + // test run casper.then(function() {techo('run face_recognition par1 par2 par3');}); casper.then(function() {casper.waitForExec('face_recognition par1 par2 par3', null, function(response) {run_result = ''; @@ -2795,6 +2799,48 @@ target_url = "www.dummytestsite.com"; casper.then(function() {casper.thenOpen('http://'+target_url+'', function() { techo('http://'+target_url+'' + ' - ' + this.getTitle());});}); // end of dynamic url block +casper.then(function() {casper.thenOpen('http://'+target_url+'', function() { +techo('http://'+target_url+'' + ' - ' + this.getTitle());});}); // end of dynamic url block + +// test use of variable +// old way for '+variable+' backward compatibility +casper.then(function() { // start of JS code +locator = 'email_button' +}); // end of JS code + +casper.then(function() {{techo('click '+locator+''); +casper.waitFor(function check() {return check_tx(''+locator+'');}, +function then() {this.click(tx(''+locator+''));}, +function timeout() {this.echo('ERROR - cannot find '+locator+'').exit();});}}); + +casper.then(function() { // start of JS code +number = 5 +}); // end of JS code + +casper.then(function() {{techo('click (//*[@id="test"])['+variable+']'); +casper.waitFor(function check() {return check_tx('(//*[@id="test"])['+variable+']');}, +function then() {this.click(tx('(//*[@id="test"])['+variable+']'));}, +function timeout() {this.echo('ERROR - cannot find (//*[@id="test"])['+variable+']').exit();});}}); + +// new way of using '+variable+' to denote variable +casper.then(function() { // start of JS code +locator = 'email_button' +}); // end of JS code + +casper.then(function() {{techo('click '+locator+''); +casper.waitFor(function check() {return check_tx(''+locator+'');}, +function then() {this.click(tx(''+locator+''));}, +function timeout() {this.echo('ERROR - cannot find '+locator+'').exit();});}}); + +casper.then(function() { // start of JS code +number = 5 +}); // end of JS code + +casper.then(function() {{techo('click (//*[@id="test"])['+variable+']'); +casper.waitFor(function check() {return check_tx('(//*[@id="test"])['+variable+']');}, +function then() {this.click(tx('(//*[@id="test"])['+variable+']'));}, +function timeout() {this.echo('ERROR - cannot find (//*[@id="test"])['+variable+']').exit();});}}); + // test present function casper.then(function() {if ((present('footer'))) { // start of code block