1 | <?php
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | $script = $argv[1]; if ($script=="") die("ERROR - specify flow filename as first parameter\n");
|
7 | if (strpos(pathinfo($script, PATHINFO_BASENAME), '.') !== false)
|
8 | if ((pathinfo($script, PATHINFO_EXTENSION)!="gui") and (pathinfo($script, PATHINFO_EXTENSION)!="txt") and (pathinfo($script, PATHINFO_EXTENSION)!="js") and (pathinfo($script, PATHINFO_EXTENSION)!="tagui"))
|
9 | die("ERROR - use .tagui .js .txt or no extension for flow filename\n");
|
10 |
|
11 |
|
12 | if (!file_exists($script)) die("ERROR - cannot find " . $script . "\n");
|
13 | $input_file = fopen($script,'r') or die("ERROR - cannot open " . $script . "\n");
|
14 | $output_file = fopen($script . '.js','w') or die("ERROR - cannot open " . $script . '.js' . "\n");
|
15 | $config_file = fopen('tagui_config.txt','r') or die("ERROR - cannot open tagui_config.txt" . "\n");
|
16 | $header_file = fopen('tagui_header.js','r') or die("ERROR - cannot open tagui_header.js" . "\n");
|
17 | $footer_file = fopen('tagui_footer.js','r') or die("ERROR - cannot open tagui_footer.js" . "\n");
|
18 |
|
19 | $repo_count = 0; if (file_exists(getenv('custom_csv_file'))) {
|
20 | $repo_file = fopen(getenv('custom_csv_file'),'r') or die("ERROR - cannot open " . getenv('custom_csv_file') . "\n");
|
21 | while (!feof($repo_file)) {$repo_data[$repo_count] = fgetcsv($repo_file);
|
22 | if (count($repo_data[$repo_count]) == 0) die("ERROR - empty row found in " . getenv('custom_csv_file') . "\n");
|
23 | $repo_count++;} fclose($repo_file); $repo_count-=1;
|
24 | if (count($repo_data[$repo_count]) == 1) $repo_count-=1;}
|
25 |
|
26 | $local_repo_location = str_replace("\\","/",dirname($script)) . '/tagui_local.csv';
|
27 | if (file_exists($local_repo_location)) {
|
28 | $local_repo_file = fopen($local_repo_location,'r') or die("ERROR - cannot open " . 'tagui_local.csv' . "\n");
|
29 | if ($repo_count != 0) $repo_count++; fgetcsv($local_repo_file);
|
30 | while (!feof($local_repo_file)) {$repo_data[$repo_count] = fgetcsv($local_repo_file);
|
31 | if (count($repo_data[$repo_count]) == 0) die("ERROR - empty row found in " . 'tagui_local.csv' . "\n");
|
32 | if (count($repo_data[$repo_count]) != 1)
|
33 | {$repo_data[$repo_count] = array_pad($repo_data[$repo_count], count($repo_data[0]), $repo_data[$repo_count][1]);}
|
34 | $repo_count++;} fclose($local_repo_file); $repo_count-=1; if (count($repo_data[$repo_count]) == 1) $repo_count-=1;}
|
35 |
|
36 | if (file_exists('tagui_global.csv')) {
|
37 | $global_repo_file = fopen('tagui_global.csv','r') or die("ERROR - cannot open " . 'tagui_global.csv' . "\n");
|
38 | if ($repo_count != 0) $repo_count++; fgetcsv($global_repo_file);
|
39 | while (!feof($global_repo_file)) {$repo_data[$repo_count] = fgetcsv($global_repo_file);
|
40 | if (count($repo_data[$repo_count]) == 0) die("ERROR - empty row found in " . 'tagui_global.csv' . "\n");
|
41 | if (count($repo_data[$repo_count]) != 1)
|
42 | {$repo_data[$repo_count] = array_pad($repo_data[$repo_count], count($repo_data[0]), $repo_data[$repo_count][1]);}
|
43 | $repo_count++;} fclose($global_repo_file); $repo_count-=1; if (count($repo_data[$repo_count]) == 1) $repo_count-=1;}
|
44 |
|
45 | $tagui_web_browser = "this";
|
46 | if ((getenv('tagui_web_browser')=='headless') or (getenv('tagui_web_browser')=='chrome')) $tagui_web_browser = 'chrome';
|
47 |
|
48 | $inside_code_block = 0;
|
49 | $inside_while_loop = 0;
|
50 | $for_loop_tracker = "";
|
51 | $code_block_tracker = "";
|
52 | $integration_block_body = "";
|
53 | $line_number = 0;
|
54 | $real_line_number = 0;
|
55 | $test_automation = 0;
|
56 | $url_provided = false;
|
57 |
|
58 |
|
59 | $inside_py_block = 0; $inside_r_block = 0; $inside_run_block = 0;
|
60 | $inside_vision_block = 0; $inside_js_block = 0; $inside_dom_block = 0;
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | fwrite($output_file,"/* OUTPUT CASPERJS SCRIPT FOR TAGUI FRAMEWORK ~ TEBEL.ORG */\n\n");
|
66 | fwrite($output_file,"var casper = require('casper').create();\n");
|
67 | while(!feof($config_file)) {fwrite($output_file,fgets($config_file));} fclose($config_file);
|
68 | while(!feof($header_file)) {fwrite($output_file,fgets($header_file));} fclose($header_file);
|
69 |
|
70 |
|
71 | if (file_exists('tagui_global.js')) {
|
72 | $global_functions_file = fopen('tagui_global.js','r') or die("ERROR - cannot open tagui_global.js" . "\n");
|
73 | while(!feof($global_functions_file)) {fwrite($output_file,fgets($global_functions_file));}
|
74 | fwrite($output_file,"\n"); fclose($global_functions_file);}
|
75 |
|
76 |
|
77 | $local_functions_location = str_replace("\\","/",dirname($script)) . '/tagui_local.js';
|
78 | if (file_exists($local_functions_location)) {
|
79 | $local_functions_file = fopen($local_functions_location,'r') or die("ERROR - cannot open tagui_local.js" . "\n");
|
80 | while(!feof($local_functions_file)) {fwrite($output_file,fgets($local_functions_file));}
|
81 | fwrite($output_file,"\n"); fclose($local_functions_file);}
|
82 |
|
83 |
|
84 | fwrite($output_file,"// flow path for save_text and snap_image\n");
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | $marker_for_opening_url = "var flow_path = '" . str_replace("\\","/",dirname($script)) . "';\n\n";
|
90 | fwrite($output_file,$marker_for_opening_url);
|
91 |
|
92 |
|
93 | $temp_output_file = fopen($script . '.raw','w') or die("ERROR - cannot open " . $script . '.raw' . "\n");
|
94 | while(!feof($input_file)) {fwrite($temp_output_file,expand_intent(fgets($input_file)));} fclose($input_file);
|
95 | fclose($temp_output_file);
|
96 | $input_file = fopen($script . '.raw','r') or die("ERROR - cannot open " . $script . '.raw' . "\n");
|
97 |
|
98 | if (strpos(strtolower(file_get_contents('tagui_config.txt')),"var tagui_language = 'english';")==false)
|
99 | {
|
100 | $temp_argv1 = $argv[1]; $temp_argv2 = $argv[2]; $temp_argv3 = $argv[3];
|
101 | $argv[1] = 'tagui_parse.php'; $argv[2] = 'from';
|
102 | $temp_tagui_config = strtolower(file_get_contents('tagui_config.txt'));
|
103 | $temp_tagui_config_start = strpos($temp_tagui_config,'var tagui_language');
|
104 | $temp_tagui_config_end = strpos($temp_tagui_config,"\n",$temp_tagui_config_start);
|
105 | $temp_tagui_config = substr($temp_tagui_config,$temp_tagui_config_start,$temp_tagui_config_end-$temp_tagui_config_start);
|
106 | $temp_tagui_config = str_replace('var tagui_language','',$temp_tagui_config);
|
107 | $temp_tagui_config = str_replace('"','',$temp_tagui_config); $temp_tagui_config = str_replace("'",'',$temp_tagui_config);
|
108 | $temp_tagui_config = str_replace('=','',$temp_tagui_config); $temp_tagui_config = str_replace(';','',$temp_tagui_config);
|
109 | $argv[3] = trim($temp_tagui_config); require 'translate.php';
|
110 | $argv[1] = $temp_argv1; $argv[2] = $temp_argv2; $argv[3] = $temp_argv3; $translated_raw_flow = "";
|
111 | while(!feof($input_file)) {$translated_raw_flow .= translate_intent(fgets($input_file));} fclose($input_file);
|
112 | file_put_contents($script . '.raw', $translated_raw_flow);
|
113 | $input_file = fopen($script . '.raw','r') or die("ERROR - cannot open " . $script . '.raw' . "\n");}
|
114 |
|
115 |
|
116 | $padded_raw_flow = ""; $previous_line_is_condition = false; $reference_indentation = "";
|
117 | while(!feof($input_file)) {$padded_raw_flow_line = fgets($input_file);
|
118 | $indentation_tracker = str_replace(ltrim($padded_raw_flow_line),'',$padded_raw_flow_line);
|
119 | $indentation_tracker = substr($indentation_tracker,strlen($reference_indentation));
|
120 |
|
121 | $padded_raw_flow_line = ltrim($padded_raw_flow_line);
|
122 |
|
123 |
|
124 | if (strtolower(trim($padded_raw_flow_line)) == "js begin") $inside_js_block = 1;
|
125 | else if (strtolower(trim($padded_raw_flow_line)) == "js finish") $inside_js_block = 0;
|
126 | else if (strtolower(trim($padded_raw_flow_line)) == "py begin")
|
127 | {$inside_py_block = 1; $reference_indentation = $indentation_tracker;}
|
128 | else if (strtolower(trim($padded_raw_flow_line)) == "py finish")
|
129 | {$inside_py_block = 0; $reference_indentation = "";}
|
130 | else if (strtolower(trim($padded_raw_flow_line)) == "r begin") $inside_r_block = 1;
|
131 | else if (strtolower(trim($padded_raw_flow_line)) == "r finish") $inside_r_block = 0;
|
132 | else if (strtolower(trim($padded_raw_flow_line)) == "dom begin") $inside_dom_block = 1;
|
133 | else if (strtolower(trim($padded_raw_flow_line)) == "dom finish") $inside_dom_block = 0;
|
134 | else if (strtolower(trim($padded_raw_flow_line)) == "run begin") $inside_run_block = 1;
|
135 | else if (strtolower(trim($padded_raw_flow_line)) == "run finish") $inside_run_block = 0;
|
136 | else if (strtolower(trim($padded_raw_flow_line)) == "vision begin")
|
137 | {$inside_vision_block = 1; $reference_indentation = $indentation_tracker;}
|
138 | else if (strtolower(trim($padded_raw_flow_line)) == "vision finish")
|
139 | {$inside_vision_block = 0; $reference_indentation = "";}
|
140 |
|
141 |
|
142 | if (($inside_js_block + $inside_py_block + $inside_r_block +
|
143 | $inside_dom_block + $inside_run_block + $inside_vision_block) > 0)
|
144 | {$padded_raw_flow .= $indentation_tracker . $padded_raw_flow_line; continue;}
|
145 |
|
146 |
|
147 | if ((substr($padded_raw_flow_line,0,9)=="function ") or (substr($padded_raw_flow_line,0,12)=="js function "))
|
148 | if (strpos($padded_raw_flow_line,"(")!==false) {$js_function_name_startpos = strpos($padded_raw_flow_line,"function ")+9;
|
149 | $js_function_name_endpos = strpos($padded_raw_flow_line,"(",$js_function_name_startpos);
|
150 | $padded_raw_flow_line = trim(substr($padded_raw_flow_line,$js_function_name_startpos,$js_function_name_endpos -
|
151 | $js_function_name_startpos)) . ' = function ' . trim(substr($padded_raw_flow_line,$js_function_name_endpos))."\n";}
|
152 | else die("ERROR - missing brackets () for ".$padded_raw_flow_line);
|
153 |
|
154 | if ((substr($padded_raw_flow_line,0,3)=="if ") or (substr($padded_raw_flow_line,0,8)=="else if ")
|
155 | or (substr($padded_raw_flow_line,0,4)=="for ") or (substr($padded_raw_flow_line,0,6)=="while ") or
|
156 | (substr($padded_raw_flow_line,0,6)=="popup ") or (substr($padded_raw_flow_line,0,6)=="frame ") or
|
157 | (trim($padded_raw_flow_line)=="else")) $current_line_is_condition = true; else $current_line_is_condition = false;
|
158 | if (($previous_line_is_condition == true) and ($current_line_is_condition == true))
|
159 | die("ERROR - for nested conditions, loops, popup, frame, set { and } explicitly\n".
|
160 | "ERROR - add { before this line and add } accordingly - ".$padded_raw_flow_line);
|
161 | if (($previous_line_is_condition == true) and (substr($padded_raw_flow_line,0,1)!="{"))
|
162 | $padded_raw_flow .= "{\n".trim($padded_raw_flow_line)."\n}\n"; else $padded_raw_flow .= $padded_raw_flow_line;
|
163 | $previous_line_is_condition = $current_line_is_condition;
|
164 | } fclose($input_file); file_put_contents($script . '.raw', $padded_raw_flow);
|
165 |
|
166 | $input_file = fopen($script . '.raw','r') or die("ERROR - cannot open " . $script . '.raw' . "\n");
|
167 |
|
168 | $inside_py_block = 0; $inside_r_block = 0; $inside_run_block = 0;
|
169 | $inside_vision_block = 0; $inside_js_block = 0; $inside_dom_block = 0;
|
170 |
|
171 |
|
172 | while(!feof($input_file)) {fwrite($output_file,parse_intent(fgets($input_file)));} fclose($input_file);
|
173 |
|
174 |
|
175 | while(!feof($footer_file)) {fwrite($output_file,fgets($footer_file));} fclose($footer_file); fclose($output_file);
|
176 | chmod ($script . '.js',0600);
|
177 | if (!$url_provided) {
|
178 | $GLOBALS['real_line_number'] = 1; $generated_js_file_contents = file_get_contents($script . '.js');
|
179 | $generated_js_file_contents = str_replace($marker_for_opening_url, $marker_for_opening_url .
|
180 | url_intent('about:blank'), $generated_js_file_contents); file_put_contents($script . '.js',$generated_js_file_contents);}
|
181 | if ($inside_code_block != 0) echo "ERROR - number of step { does not tally with with }\n";
|
182 |
|
183 |
|
184 | $script_content = file_get_contents($script . '.js');
|
185 | $script_content = str_replace("});\n\ncasper.then(function() {else\n","\nelse\n\n",$script_content);
|
186 | $script_content = str_replace("});\n\ncasper.then(function() {else if ","\nelse if ",$script_content);
|
187 | $script_content = str_replace("}); // end of JS code\n\ncasper.then(function() { // start of JS code\n",
|
188 | "",$script_content);
|
189 | $script_content = preg_replace('/^casper\.then\(function\(\) {{(.*) \/\/ beg_tx while loop marker/m',
|
190 | '$1',$script_content);
|
191 | $script_content = preg_replace('/^casper\.then\(function\(\) {(.*) \/\/ end_fi while loop marker(.*)}\);/m',
|
192 | '$1$2',$script_content);
|
193 | $script_content = preg_replace('/^casper\.then\(function\(\) {(.*)\n(.*) \/\/ end_fi while loop marker(.*)}\);/m',
|
194 | "$1\n$2$3",$script_content);
|
195 | file_put_contents($script . '.js',$script_content);
|
196 |
|
197 |
|
198 |
|
199 | if ($tagui_web_browser == 'chrome') {$script_content = file_get_contents($script . '.js');
|
200 | $script_content = str_replace("var chrome_id = 0;","var chrome_id = 1;",$script_content);
|
201 | $script_content = str_replace("casper.exists","chrome.exists",$script_content);
|
202 | $script_content = str_replace("this.exists","chrome.exists",$script_content);
|
203 | $script_content = str_replace("casper.click","chrome.click",$script_content);
|
204 | $script_content = str_replace("this.click","chrome.click",$script_content);
|
205 | $script_content = str_replace("casper.mouse","chrome.mouse",$script_content);
|
206 | $script_content = str_replace("this.mouse","chrome.mouse",$script_content);
|
207 | $script_content = str_replace("casper.sendKeys","chrome.sendKeys",$script_content);
|
208 | $script_content = str_replace("this.sendKeys","chrome.sendKeys",$script_content);
|
209 |
|
210 | $script_content = str_replace("casper.selectOptionByValue(","chrome.selectOptionByValue(",$script_content);
|
211 | $script_content = str_replace("this.selectOptionByValue(","chrome.selectOptionByValue(",$script_content);
|
212 |
|
213 | $script_content = str_replace("casper.countElements(","chrome.countElements(",$script_content);
|
214 | $script_content = str_replace("this.countElements(","chrome.countElements(",$script_content);
|
215 |
|
216 | $script_content = str_replace("casper.elementVisible(","chrome.elementVisible(",$script_content);
|
217 | $script_content = str_replace("this.elementVisible(","chrome.elementVisible(",$script_content);
|
218 | $script_content = str_replace("casper.fetchText","chrome.fetchText",$script_content);
|
219 | $script_content = str_replace("this.fetchText","chrome.fetchText",$script_content);
|
220 | $script_content = str_replace("casper.capture","chrome.capture",$script_content);
|
221 | $script_content = str_replace("this.capture","chrome.capture",$script_content);
|
222 | $script_content = str_replace("casper.captureSelector","chrome.captureSelector",$script_content);
|
223 | $script_content = str_replace("this.captureSelector","chrome.captureSelector",$script_content);
|
224 | $script_content = str_replace("chrome.page.uploadFile","chrome.upload",$script_content);
|
225 | $script_content = str_replace("casper.page.uploadFile","chrome.upload",$script_content);
|
226 | $script_content = str_replace("this.page.uploadFile","chrome.upload",$script_content);
|
227 | $script_content = str_replace("casper.download","chrome.download",$script_content);
|
228 | $script_content = str_replace("this.download","chrome.download",$script_content);
|
229 | $script_content = str_replace("casper.evaluate","chrome.evaluate",$script_content);
|
230 | $script_content = str_replace("this.evaluate","chrome.evaluate",$script_content);
|
231 | $script_content = str_replace("casper.withFrame","chrome.withFrame",$script_content);
|
232 | $script_content = str_replace("this.withFrame","chrome.withFrame",$script_content);
|
233 | $script_content = str_replace("casper.waitForPopup","chrome.waitForPopup",$script_content);
|
234 | $script_content = str_replace("this.waitForPopup","chrome.waitForPopup",$script_content);
|
235 | $script_content = str_replace("casper.withPopup","chrome.withPopup",$script_content);
|
236 | $script_content = str_replace("this.withPopup","chrome.withPopup",$script_content);
|
237 | $script_content = str_replace("casper.getHTML","chrome.getHTML",$script_content);
|
238 | $script_content = str_replace("this.getHTML","chrome.getHTML",$script_content);
|
239 | $script_content = str_replace("casper.getTitle","chrome.getTitle",$script_content);
|
240 | $script_content = str_replace("this.getTitle","chrome.getTitle",$script_content);
|
241 | $script_content = str_replace("casper.getCurrentUrl","chrome.getCurrentUrl",$script_content);
|
242 | $script_content = str_replace("this.getCurrentUrl","chrome.getCurrentUrl",$script_content);
|
243 | $script_content = str_replace("casper.debugHTML","chrome.debugHTML",$script_content);
|
244 | $script_content = str_replace("this.debugHTML","chrome.debugHTML",$script_content);
|
245 | $script_content = str_replace("casper.reload","chrome.reload",$script_content);
|
246 | $script_content = str_replace("this.reload","chrome.reload",$script_content);
|
247 | $script_content = str_replace("casper.back","chrome.back",$script_content);
|
248 | $script_content = str_replace("this.back","chrome.back",$script_content);
|
249 | $script_content = str_replace("casper.forward","chrome.forward",$script_content);
|
250 | $script_content = str_replace("this.forward","chrome.forward",$script_content);
|
251 | file_put_contents($script . '.js',$script_content);
|
252 | if (!touch('tagui_chrome.in')) die("ERROR - cannot initialise tagui_chrome.in\n");
|
253 | if (!touch('tagui_chrome.out')) die("ERROR - cannot initialise tagui_chrome.out\n");}
|
254 |
|
255 |
|
256 | if (getenv('tagui_quiet_mode') == 'true') {$script_content = file_get_contents($script . '.js');
|
257 | $script_content = str_replace("var quiet_mode = false;","var quiet_mode = true;",$script_content);
|
258 | $script_content = str_replace("casper.echo('\\nSTART - automation started - ","dummy_echo('",$script_content);
|
259 | file_put_contents($script . '.js',$script_content);}
|
260 |
|
261 |
|
262 | if (getenv('tagui_test_mode') == 'true') {$script_content = file_get_contents($script . '.js');
|
263 | $script_content = str_replace("casper.echo('\\nSTART - automation started - ","casper.echo('",$script_content);
|
264 | $script_content = str_replace("techo('FINISH - automation","dummy_echo('FINISH - test",$script_content);
|
265 | $script_content = str_replace("this.echo(","test.comment(",$script_content);
|
266 | $script_content = str_replace("test.comment('ERROR","test.fail('ERROR",$script_content);
|
267 |
|
268 | $script_content = str_replace("casper.echo(echo_string);","casper.test.comment(echo_string);",$script_content);
|
269 | $script_content = str_replace("casper.echo(translated_string);","casper.test.comment(translated_string);",$script_content);
|
270 | $script_content = str_replace("\\n'","'",str_replace("'\\n","'",$script_content));
|
271 |
|
272 | $script_content = str_replace("var casper = require(","// var casper = require(",$script_content);
|
273 |
|
274 | $script_content = str_replace("casper.start(","casper.test.begin('" . str_replace("\\","\\\\",$script) . "', " .
|
275 | $test_automation.", function(test) {\ncasper.start(",$script_content);
|
276 | $script_content = str_replace("casper.run();","casper.run(function() {test.done();});});",$script_content);
|
277 | file_put_contents($script . '.js',$script_content);}
|
278 |
|
279 |
|
280 | else if ($test_automation > 0) {$script_content = file_get_contents($script . '.js');
|
281 | $script_content = str_replace("test.","// test.",$script_content); file_put_contents($script . '.js',$script_content);}
|
282 |
|
283 | function expand_intent($script_line) {
|
284 | if ((strpos(strtolower(trim($script_line)),'tagui ') === 0) or (strtolower(trim($script_line)) == 'tagui')) {
|
285 | $params = trim(substr(trim($script_line)." ",1+strpos(trim($script_line)." "," "))); if ($params == "")
|
286 | die("ERROR - filename missing for step " . trim($script_line) . "\n");
|
287 | else if (!file_exists(abs_file($params)))
|
288 | die("ERROR - file not found for step " . trim($script_line) . "\n");
|
289 | else {$expanded_intent = ""; $temp_input_file = fopen(abs_file($params),'r'); if ($temp_input_file == false)
|
290 | die("ERROR - cannot open file for step " . trim($script_line) . "\n");
|
291 | while(!feof($temp_input_file)) {$expanded_intent .= expand_intent(fgets($temp_input_file));} fclose($temp_input_file);
|
292 | return $expanded_intent;}} else return rtrim($script_line) . "\n";}
|
293 |
|
294 | function current_line() {return "[LINE " . $GLOBALS['line_number'] . "]";}
|
295 |
|
296 | function parse_intent($script_line) {$GLOBALS['line_number']++; $GLOBALS['real_line_number']++;
|
297 | $indentation_tracker = str_replace(ltrim($script_line),'',$script_line);
|
298 | $script_line = trim($script_line); if ($script_line=="") {$GLOBALS['real_line_number']--; return "";}
|
299 | $script_line = parse_backticks($script_line);
|
300 | $script_line = trim($script_line); if ($script_line=="") {$GLOBALS['real_line_number']--; return "";}
|
301 |
|
302 | $intent_type = get_intent($script_line); if ($intent_type == "integration_block")
|
303 | {$GLOBALS['integration_block_body'] .= $indentation_tracker . $script_line ."[END_OF_LINE]"; return "";}
|
304 | else {$script_line = parse_closure($script_line); return process_intent($intent_type, $script_line);}}
|
305 |
|
306 | function parse_backticks($script_line) {
|
307 |
|
308 |
|
309 | if ((substr_count($script_line,'`') > 1) and (!(substr_count($script_line,'`') & 1))) {
|
310 | if ($GLOBALS['repo_count'] == 0) {
|
311 | $script_line = parse_variables($script_line);
|
312 | } else {
|
313 | if (getenv('tagui_data_set')!==false) {
|
314 | $data_set = intval(getenv('tagui_data_set'));
|
315 | } else {
|
316 | $data_set = 1;
|
317 | }
|
318 | $escaped_line_ends = ["\n" => "\\n", "\r" => "\\r"];
|
319 |
|
320 | for ($repo_check = 0; $repo_check <= $GLOBALS['repo_count']; $repo_check++) {
|
321 | $repo_keyword = "`".$GLOBALS['repo_data'][$repo_check][0]."`";
|
322 | $repo_data_value = strtr($GLOBALS['repo_data'][$repo_check][$data_set], $escaped_line_ends);
|
323 | $script_line = str_replace($repo_keyword, $repo_data_value, $script_line);
|
324 | }
|
325 | for ($repo_check = 0; $repo_check <= $GLOBALS['repo_count']; $repo_check++) {
|
326 | $repo_keyword = "`".$GLOBALS['repo_data'][$repo_check][0]."`";
|
327 | $repo_data_value = strtr($GLOBALS['repo_data'][$repo_check][$data_set], $escaped_line_ends);
|
328 | $script_line = str_replace($repo_keyword, $repo_data_value, $script_line);
|
329 | }
|
330 | if (strpos($script_line,'`')!==false) {
|
331 | $script_line = parse_variables($script_line);
|
332 | }
|
333 | }
|
334 | } return $script_line;}
|
335 |
|
336 | function parse_variables($script_line) {
|
337 | $quote_token = "'+";
|
338 | for ($char_counter = 0; $char_counter < strlen($script_line); $char_counter++) {
|
339 | if (substr($script_line,$char_counter,1) == "`") {
|
340 | $script_line = substr_replace($script_line,$quote_token,$char_counter,1);
|
341 | if ($quote_token == "'+") $quote_token = "+'"; else $quote_token = "'+";
|
342 | }
|
343 | } return $script_line;}
|
344 |
|
345 | function parse_closure($script_line) {switch($script_line) {
|
346 |
|
347 |
|
348 | case "py finish":
|
349 | {$script_line = substr($GLOBALS['integration_block_body'],0,-13); $GLOBALS['inside_py_block'] = 0; break;}
|
350 | case "r finish":
|
351 | {$script_line = substr($GLOBALS['integration_block_body'],0,-13); $GLOBALS['inside_r_block'] = 0;
|
352 | $script_line = str_replace(";; ","; ",str_replace("[END_OF_LINE]","; ",$GLOBALS['integration_block_body'])); break;}
|
353 | case "vision finish":
|
354 | {$script_line = substr($GLOBALS['integration_block_body'],0,-13); $GLOBALS['inside_vision_block'] = 0; break;}
|
355 | case "js finish":
|
356 | {$script_line = str_replace("[END_OF_LINE]", "\n", $GLOBALS['integration_block_body']); $GLOBALS['inside_js_block'] = 0; break;}
|
357 | case "dom finish":
|
358 | {$script_line = str_replace("[END_OF_LINE]", "\n", $GLOBALS['integration_block_body']); $GLOBALS['inside_dom_block'] = 0; break;}}
|
359 | return $script_line;}
|
360 |
|
361 | function process_intent($intent_type, $script_line) {
|
362 |
|
363 | switch ($intent_type) {
|
364 | case "url": return url_intent($script_line); break;
|
365 | case "tap": return tap_intent($script_line); break;
|
366 | case "rtap": return rtap_intent($script_line); break;
|
367 | case "dtap": return dtap_intent($script_line); break;
|
368 | case "hover": return hover_intent($script_line); break;
|
369 | case "type": return type_intent($script_line); break;
|
370 | case "select": return select_intent($script_line); break;
|
371 | case "read": return read_intent($script_line); break;
|
372 | case "show": return show_intent($script_line); break;
|
373 | case "upload": return upload_intent($script_line); break;
|
374 | case "down": return down_intent($script_line); break;
|
375 | case "receive": return receive_intent($script_line); break;
|
376 | case "echo": return echo_intent($script_line); break;
|
377 | case "save": return save_intent($script_line); break;
|
378 | case "dump": return dump_intent($script_line); break;
|
379 | case "write": return write_intent($script_line); break;
|
380 | case "load": return load_intent($script_line); break;
|
381 | case "snap": return snap_intent($script_line); break;
|
382 | case "table": return table_intent($script_line); break;
|
383 | case "wait": return wait_intent($script_line); break;
|
384 | case "live": return live_intent($script_line); break;
|
385 | case "ask": return ask_intent($script_line); break;
|
386 | case "check": return check_intent($script_line); break;
|
387 | case "test": return test_intent($script_line); break;
|
388 | case "frame": return frame_intent($script_line); break;
|
389 | case "popup": return popup_intent($script_line); break;
|
390 | case "api": return api_intent($script_line); break;
|
391 | case "run": return run_intent($script_line); break;
|
392 | case "dom": return dom_intent($script_line); break;
|
393 | case "js": return js_intent($script_line); break;
|
394 | case "r": return r_intent($script_line); break;
|
395 | case "py": return py_intent($script_line); break;
|
396 | case "vision": return vision_intent($script_line); break;
|
397 | case "timeout": return timeout_intent($script_line); break;
|
398 | case "code": return code_intent($script_line); break;
|
399 | default: echo "ERROR - " . current_line() . " cannot understand step " . $script_line . "\n";}}
|
400 |
|
401 | function get_intent($raw_intent) {$lc_raw_intent = strtolower($raw_intent);
|
402 |
|
403 | if ($lc_raw_intent == "py finish") return "py"; if ($lc_raw_intent == "r finish") return "r";
|
404 | if ($lc_raw_intent == "vision finish") return "vision"; if ($lc_raw_intent == "js finish") return "js";
|
405 | if ($lc_raw_intent == "dom finish") return "dom";
|
406 |
|
407 | if ($GLOBALS['inside_py_block'] != 0 || $GLOBALS['inside_r_block'] != 0 ||
|
408 | $GLOBALS['inside_vision_block'] != 0 || $GLOBALS['inside_js_block'] != 0 ||
|
409 | $GLOBALS['inside_dom_block'] != 0) return "integration_block";
|
410 | if ($GLOBALS['inside_run_block'] != 0) return "run";
|
411 |
|
412 | if ((substr($lc_raw_intent,0,7)=="http://") or (substr($lc_raw_intent,0,8)=="https://") or (substr($lc_raw_intent,0,11)=="about:blank")) return "url";
|
413 |
|
414 |
|
415 | if ((substr($lc_raw_intent,0,4)=="tap ") or (substr($lc_raw_intent,0,6)=="click ")) return "tap";
|
416 | if ((substr($lc_raw_intent,0,5)=="rtap ") or (substr($lc_raw_intent,0,7)=="rclick ")) return "rtap";
|
417 | if ((substr($lc_raw_intent,0,5)=="dtap ") or (substr($lc_raw_intent,0,7)=="dclick ")) return "dtap";
|
418 | if ((substr($lc_raw_intent,0,6)=="hover ")or(substr($lc_raw_intent,0,5)=="move ")) return "hover";
|
419 | if ((substr($lc_raw_intent,0,5)=="type ") or (substr($lc_raw_intent,0,6)=="enter ")) return "type";
|
420 | if ((substr($lc_raw_intent,0,7)=="select ") or (substr($lc_raw_intent,0,7)=="choose ")) return "select";
|
421 | if ((substr($lc_raw_intent,0,5)=="read ") or (substr($lc_raw_intent,0,6)=="fetch ")) return "read";
|
422 | if ((substr($lc_raw_intent,0,5)=="show ") or (substr($lc_raw_intent,0,6)=="print ")) return "show";
|
423 | if ((substr($lc_raw_intent,0,3)=="up ") or (substr($lc_raw_intent,0,7)=="upload ")) return "upload";
|
424 | if ((substr($lc_raw_intent,0,5)=="down ") or (substr($lc_raw_intent,0,9)=="download ")) return "down";
|
425 | if (substr($lc_raw_intent,0,8)=="receive ") return "receive";
|
426 | if (substr($lc_raw_intent,0,5)=="echo ") return "echo";
|
427 | if (substr($lc_raw_intent,0,5)=="save ") return "save";
|
428 | if (substr($lc_raw_intent,0,5)=="dump ") return "dump";
|
429 | if (substr($lc_raw_intent,0,6)=="write ") return "write";
|
430 | if (substr($lc_raw_intent,0,5)=="load ") return "load";
|
431 | if (substr($lc_raw_intent,0,5)=="snap ") return "snap";
|
432 | if (substr($lc_raw_intent,0,6)=="table ") return "table";
|
433 | if (substr($lc_raw_intent,0,5)=="wait ") return "wait";
|
434 | if (substr($lc_raw_intent,0,5)=="live ") return "live";
|
435 | if (substr($lc_raw_intent,0,4)=="ask ") return "ask";
|
436 | if (substr($lc_raw_intent,0,6)=="check ") {$GLOBALS['test_automation']++; return "check";}
|
437 | if (substr($lc_raw_intent,0,5)=="test ") return "test";
|
438 | if (substr($lc_raw_intent,0,6)=="frame ") return "frame";
|
439 | if (substr($lc_raw_intent,0,6)=="popup ") return "popup";
|
440 | if (substr($lc_raw_intent,0,4)=="api ") return "api";
|
441 | if (substr($lc_raw_intent,0,4)=="run ") return "run";
|
442 | if (substr($lc_raw_intent,0,4)=="dom ") return "dom";
|
443 | if (substr($lc_raw_intent,0,3)=="js ") return "js";
|
444 | if (substr($lc_raw_intent,0,2)=="r ") return "r";
|
445 | if (substr($lc_raw_intent,0,3)=="py ") return "py";
|
446 | if (substr($lc_raw_intent,0,7)=="vision ") return "vision";
|
447 | if (substr($lc_raw_intent,0,8)=="timeout ") return "timeout";
|
448 |
|
449 |
|
450 | if (($lc_raw_intent=="tap") or ($lc_raw_intent=="click")) return "tap";
|
451 | if (($lc_raw_intent=="rtap") or ($lc_raw_intent=="rclick")) return "rtap";
|
452 | if (($lc_raw_intent=="dtap") or ($lc_raw_intent=="dclick")) return "dtap";
|
453 | if (($lc_raw_intent=="hover") or ($lc_raw_intent=="move")) return "hover";
|
454 | if (($lc_raw_intent=="type") or ($lc_raw_intent=="enter")) return "type";
|
455 | if (($lc_raw_intent=="select") or ($lc_raw_intent=="choose")) return "select";
|
456 | if (($lc_raw_intent=="read") or ($lc_raw_intent=="fetch")) return "read";
|
457 | if (($lc_raw_intent=="show") or ($lc_raw_intent=="print")) return "show";
|
458 | if (($lc_raw_intent=="up") or ($lc_raw_intent=="upload")) return "upload";
|
459 | if (($lc_raw_intent=="down") or ($lc_raw_intent=="download")) return "down";
|
460 | if ($lc_raw_intent=="receive") return "receive";
|
461 | if ($lc_raw_intent=="echo") return "echo";
|
462 | if ($lc_raw_intent=="save") return "save";
|
463 | if ($lc_raw_intent=="dump") return "dump";
|
464 | if ($lc_raw_intent=="write") return "write";
|
465 | if ($lc_raw_intent=="load") return "load";
|
466 | if ($lc_raw_intent=="snap") return "snap";
|
467 | if ($lc_raw_intent=="table") return "table";
|
468 | if ($lc_raw_intent=="wait") return "wait";
|
469 | if ($lc_raw_intent=="live") return "live";
|
470 | if ($lc_raw_intent=="ask") return "ask";
|
471 | if ($lc_raw_intent=="check") {$GLOBALS['test_automation']++; return "check";}
|
472 | if ($lc_raw_intent=="test") return "test";
|
473 | if ($lc_raw_intent=="frame") return "frame";
|
474 | if ($lc_raw_intent=="popup") return "popup";
|
475 | if ($lc_raw_intent=="api") return "api";
|
476 | if ($lc_raw_intent=="run") return "run";
|
477 | if ($lc_raw_intent=="dom") return "dom";
|
478 | if ($lc_raw_intent=="js") return "js";
|
479 | if ($lc_raw_intent=="r") return "r";
|
480 | if ($lc_raw_intent=="py") return "py";
|
481 | if ($lc_raw_intent=="vision") return "vision";
|
482 | if ($lc_raw_intent=="timeout") return "timeout";
|
483 |
|
484 |
|
485 | if (is_code($raw_intent)) return "code"; else return "error";}
|
486 |
|
487 | function is_code($raw_intent) {
|
488 |
|
489 |
|
490 |
|
491 | if ((substr($raw_intent,0,4)=="var ") or (substr($raw_intent,0,3)=="do ")) return true;
|
492 | if ((substr($raw_intent,0,1)=="{") or (substr($raw_intent,0,1)=="}")) return true;
|
493 | if ((substr($raw_intent,-1)=="{") or (substr($raw_intent,-1)=="}")) return true;
|
494 | if ((substr($raw_intent,0,3)=="if ") or (substr($raw_intent,0,4)=="else")) return true;
|
495 | if ((substr($raw_intent,0,4)=="for ") or (substr($raw_intent,0,6)=="while ")) return true;
|
496 | if ((substr($raw_intent,0,7)=="switch ") or (substr($raw_intent,0,5)=="case ")) return true;
|
497 | if ((substr($raw_intent,0,6)=="break;") or ($raw_intent=="break")) return true;
|
498 | if ((substr($raw_intent,0,9)=="continue;") or ($raw_intent=="continue")) return true;
|
499 | if ((substr($raw_intent,0,7)=="casper.") or (substr($raw_intent,0,5)=="this.")) return true;
|
500 | if (substr($raw_intent,0,7)=="chrome.") return true;
|
501 | if (substr($raw_intent,0,5)=="test.") {$GLOBALS['test_automation']++; return true;}
|
502 | if (substr($raw_intent,0,2)=="//") {$GLOBALS['real_line_number']--; return true;}
|
503 | if (substr($raw_intent,-1)==";") return true; if (substr($raw_intent,0,9)=="function ") return true;
|
504 |
|
505 | if (strpos($raw_intent,"=")!==false) return true; return false;}
|
506 |
|
507 | function abs_file($filename) {
|
508 | if ($filename == "") return ""; $flow_script = $GLOBALS['script'];
|
509 | if (substr($filename,0,1)=="/") return $filename;
|
510 | if (substr($filename,1,1)==":") return str_replace("\\","/",$filename);
|
511 | if (strpos($filename,"'+")!==false and strpos($filename,"+'")!==false)
|
512 | return "'+abs_file('" . $filename . "')+'";
|
513 | $flow_path = str_replace("\\","/",dirname($flow_script));
|
514 |
|
515 | if (strpos($flow_path,"/")!==false) return str_replace("\\","/",$flow_path . '/' . $filename);
|
516 | else return $flow_path . '\\' . $filename;}
|
517 |
|
518 | function beg_tx($locator) {
|
519 | if ($GLOBALS['inside_while_loop'] == 0)
|
520 | return "\ncasper.waitFor(function check() {return check_tx('".$locator."');},\nfunction then() {";
|
521 | else return " // beg_tx while loop marker\n";}
|
522 |
|
523 | function end_tx($locator) {
|
524 | if ($GLOBALS['inside_while_loop'] == 0)
|
525 | return "},\nfunction timeout() {this.echo('ERROR - cannot find ".$locator."').exit();});}"."});".end_fi()."\n\n";
|
526 | else if ($GLOBALS['inside_code_block']==0)
|
527 | {$GLOBALS['inside_while_loop'] = 0;
|
528 | $GLOBALS['for_loop_tracker'] = "";
|
529 | return "\n";} else return "\n";}
|
530 |
|
531 | function end_fi() {
|
532 | if ($GLOBALS['inside_code_block'] == 0) $GLOBALS['inside_while_loop'] = 0;
|
533 | if ($GLOBALS['inside_code_block'] == 0) $GLOBALS['for_loop_tracker'] = "";
|
534 | if ($GLOBALS['inside_while_loop'] == 1) return " // end_fi while loop marker"; return "";}
|
535 |
|
536 | function add_concat($source_string) {
|
537 | if ((strpos($source_string,"'")!==false) and (strpos($source_string,"\"")!==false))
|
538 | {echo "ERROR - " . current_line() . " inconsistent quotes in " . $source_string . "\n";}
|
539 | else if (strpos($source_string,"'")!==false) $quote_type = "'";
|
540 | else if (strpos($source_string,"\"")!==false) $quote_type = "\""; else $quote_type = "none";
|
541 | $within_quote = false; $source_string = trim($source_string);
|
542 | for ($srcpos=0; $srcpos<strlen($source_string); $srcpos++) {
|
543 | if ($source_string[$srcpos] == $quote_type) $within_quote = !$within_quote;
|
544 | if (($within_quote == false) and ($source_string[$srcpos]==" ")) $source_string[$srcpos] = "+";}
|
545 | $source_string = str_replace("+++++","+",$source_string); $source_string = str_replace("++++","+",$source_string);
|
546 | $source_string = str_replace("+++","+",$source_string); $source_string = str_replace("++","+",$source_string);
|
547 | return $source_string;}
|
548 |
|
549 | function is_sikuli($input_params) {
|
550 | if (strlen($input_params)>4 and strtolower(substr($input_params,-4))=='.png') return true;
|
551 | else if (strlen($input_params)>4 and strtolower(substr($input_params,-4))=='.bmp') return true; else return false;}
|
552 |
|
553 | function call_sikuli($input_intent,$input_params,$other_actions = '') {
|
554 | if (!touch('tagui.sikuli/tagui_sikuli.in')) die("ERROR - cannot initialise tagui_sikuli.in\n");
|
555 | if (!touch('tagui.sikuli/tagui_sikuli.out')) die("ERROR - cannot initialise tagui_sikuli.out\n");
|
556 | if ($other_actions != '') $other_actions = "\n" . $other_actions;
|
557 | return "{techo('".str_replace(' to snap_image()','',$input_intent)."'); var fs = require('fs');\n" .
|
558 | "if (!sikuli_step('".$input_intent."')) if (!fs.exists('".$input_params."'))\n" .
|
559 | "this.echo('ERROR - cannot find image file ".$input_params."').exit(); else\n" .
|
560 | "this.echo('ERROR - cannot find " . $input_params." on screen').exit(); this.wait(0);" . $other_actions. "}" .
|
561 | end_fi()."});\n\n";}
|
562 |
|
563 | function call_r($input_intent) {
|
564 | if (!touch('tagui_r/tagui_r.in')) die("ERROR - cannot initialise tagui_r.in\n");
|
565 | if (!touch('tagui_r/tagui_r.out')) die("ERROR - cannot initialise tagui_r.out\n");
|
566 | return "{techo('".$input_intent."');\n" . "r_result = ''; if (!r_step('".$input_intent."'))\n" .
|
567 | "this.echo('ERROR - cannot execute R command(s)').exit(); this.wait(0);\n" .
|
568 | "r_result = fetch_r_text(); clear_r_text();\n" .
|
569 | "try {r_json = JSON.parse(r_result);} catch(e) {r_json = JSON.parse('null');}}" .
|
570 | end_fi()."});\n\n";}
|
571 |
|
572 | function call_py($input_intent) {
|
573 | if (!touch('tagui_py/tagui_py.in')) die("ERROR - cannot initialise tagui_py.in\n");
|
574 | if (!touch('tagui_py/tagui_py.out')) die("ERROR - cannot initialise tagui_py.out\n");
|
575 | return "{techo('".$input_intent."');\n" . "py_result = ''; if (!py_step('".$input_intent."'))\n" .
|
576 | "this.echo('ERROR - cannot execute Python command(s)').exit(); this.wait(0);\n" .
|
577 | "py_result = fetch_py_text(); clear_py_text();\n" .
|
578 | "try {py_json = JSON.parse(py_result);} catch(e) {py_json = JSON.parse('null');}}" .
|
579 | end_fi()."});\n\n";}
|
580 |
|
581 |
|
582 | function url_intent($raw_intent) {$twb = $GLOBALS['tagui_web_browser']; $casper_url = $raw_intent; $chrome_call = '';
|
583 | if ($twb == 'chrome') {$chrome_call = "var download_path = flow_path; // to set path correctly for Windows\n" .
|
584 | "if (download_path.indexOf(':')>0) download_path = download_path.replace(/\//g,'\\\\').replace(/\\\\/g,'\\\\');\n" .
|
585 | "chrome_step('Page.setDownloadBehavior',{behavior: 'allow', downloadPath: download_path});\n";
|
586 | $casper_url = 'about:blank'; $chrome_call .= "chrome_step('Page.navigate',{url: '".$raw_intent."'}); sleep(1000);\n";}
|
587 | if (strpos($raw_intent,"'+")!==false and strpos($raw_intent,"+'")!==false)
|
588 |
|
589 | {$dynamic_header = "casper.then(function() {"; $dynamic_footer = "}); // end of dynamic url block";}
|
590 | else {$dynamic_header = ""; $dynamic_footer = "";
|
591 | if (filter_var($raw_intent, FILTER_VALIDATE_URL) == false)
|
592 | if ($raw_intent != 'about:blank') echo "ERROR - " . current_line() . " invalid URL " . $raw_intent . "\n";}
|
593 | if ($GLOBALS['real_line_number'] == 1) {
|
594 | $GLOBALS['url_provided']=true; return $dynamic_header."casper.start('".$casper_url."', function() {\n".$chrome_call.
|
595 | "techo('".$raw_intent."' + ' - ' + ".$twb.".getTitle() + '\\n');});".$dynamic_footer."\n\n";}
|
596 | else return $dynamic_header."casper.thenOpen('".$casper_url."', function() {\n".$chrome_call."techo('".
|
597 | $raw_intent."' + ' - ' + ".$twb.".getTitle());});".$dynamic_footer."\n\n";}
|
598 |
|
599 | function tap_intent($raw_intent) {$twb = $GLOBALS['tagui_web_browser'];
|
600 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
601 | if (is_sikuli($params)) {$abs_params = abs_file($params); $abs_intent = str_replace($params,$abs_params,$raw_intent);
|
602 | return "casper.then(function() {".call_sikuli($abs_intent,$abs_params);}
|
603 | if ($params == "") echo "ERROR - " . current_line() . " target missing for " . $raw_intent . "\n"; else
|
604 | return "casper.then(function() {"."{techo('".$raw_intent."');".beg_tx($params).$twb.".click(tx('" . $params . "'));".end_tx($params);}
|
605 |
|
606 | function rtap_intent($raw_intent) {$twb = $GLOBALS['tagui_web_browser'];
|
607 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
608 | if (is_sikuli($params)) {$abs_params = abs_file($params); $abs_intent = str_replace($params,$abs_params,$raw_intent);
|
609 | return "casper.then(function() {".call_sikuli($abs_intent,$abs_params);}
|
610 | if ($params == "") echo "ERROR - " . current_line() . " target missing for " . $raw_intent . "\n"; else
|
611 | return "casper.then(function() {"."{techo('".$raw_intent."');".beg_tx($params).$twb.".mouse.rightclick(tx('" . $params . "'));".end_tx($params);}
|
612 |
|
613 | function dtap_intent($raw_intent) {$twb = $GLOBALS['tagui_web_browser'];
|
614 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
615 | if (is_sikuli($params)) {$abs_params = abs_file($params); $abs_intent = str_replace($params,$abs_params,$raw_intent);
|
616 | return "casper.then(function() {".call_sikuli($abs_intent,$abs_params);}
|
617 | if ($params == "") echo "ERROR - " . current_line() . " target missing for " . $raw_intent . "\n"; else
|
618 | return "casper.then(function() {"."{techo('".$raw_intent."');".beg_tx($params).$twb.".mouse.doubleclick(tx('" . $params . "'));".end_tx($params);}
|
619 |
|
620 | function hover_intent($raw_intent) {$twb = $GLOBALS['tagui_web_browser'];
|
621 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
622 | if (is_sikuli($params)) {$abs_params = abs_file($params); $abs_intent = str_replace($params,$abs_params,$raw_intent);
|
623 | return "casper.then(function() {".call_sikuli($abs_intent,$abs_params);}
|
624 | if ($params == "") echo "ERROR - " . current_line() . " target missing for " . $raw_intent . "\n"; else
|
625 | return "casper.then(function() {"."{techo('".$raw_intent."');".beg_tx($params).$twb.".mouse.move(tx('" . $params . "'));".end_tx($params);}
|
626 |
|
627 | function type_intent($raw_intent) {$twb = $GLOBALS['tagui_web_browser'];
|
628 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
629 | $param1 = trim(substr($params,0,strpos($params," as "))); $param2 = trim(substr($params,4+strpos($params," as ")));
|
630 | if (is_sikuli($param1) and $param2 != "") {
|
631 | $abs_param1 = abs_file($param1); $abs_intent = str_replace($param1,$abs_param1,$raw_intent);
|
632 | return "casper.then(function() {".call_sikuli($abs_intent,$abs_param1);}
|
633 | if (($param1 == "") or ($param2 == ""))
|
634 | echo "ERROR - " . current_line() . " target/text missing for " . $raw_intent . "\n"; else {
|
635 |
|
636 | if (substr($param2,0,7)=="[clear]") {if (strlen($param2)>7) $param2 = substr($param2,7); else $param2 = "";
|
637 | $clear_field = $twb.".sendKeys(tx('".$param1."'),'',{reset: true});\n";} else $clear_field = "";
|
638 | if (strpos($param2,"[enter]")===false) return "casper.then(function() {"."{techo('".$raw_intent."');".
|
639 | beg_tx($param1).$clear_field.$twb.".sendKeys(tx('".$param1."'),'".$param2."');".end_tx($param1);
|
640 | else
|
641 | {$param2 = str_replace("[enter]","',{keepFocus: true});\n" .
|
642 | $twb.".sendKeys(tx('".$param1."'),casper.page.event.key.Enter,{keepFocus: true});\n" .
|
643 | $twb.".sendKeys(tx('".$param1."'),'",$param2); return "casper.then(function() {"."{techo('".$raw_intent."');".
|
644 | beg_tx($param1).$clear_field.$twb.".sendKeys(tx('".$param1."'),'".$param2."',{keepFocus: true});".end_tx($param1);}}}
|
645 |
|
646 | function select_intent($raw_intent) {$twb = $GLOBALS['tagui_web_browser'];
|
647 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
648 | $param1 = trim(substr($params,0,strpos($params," as "))); $param2 = trim(substr($params,4+strpos($params," as ")));
|
649 | if (is_sikuli($param1) and is_sikuli($param2)) {
|
650 | $abs_param1 = abs_file($param1); $abs_intent = str_replace($param1,$abs_param1,$raw_intent);
|
651 | $abs_param2 = abs_file($param2); $abs_intent = str_replace($param2,$abs_param2,$abs_intent);
|
652 | return "casper.then(function() {".call_sikuli($abs_intent,$abs_param1);}
|
653 | if (($param1 == "") or ($param2 == ""))
|
654 | echo "ERROR - " . current_line() . " target/option missing for " . $raw_intent . "\n"; else
|
655 | return "casper.then(function() {"."{techo('".$raw_intent."');".beg_tx($param1)."var select_locator = tx('".$param1."');\n"
|
656 | ."if (is_xpath_selector(select_locator.toString().replace('xpath selector: ','')))\n".
|
657 | "select_locator = select_locator.toString().substring(16);\n".
|
658 | $twb.".selectOptionByValue(select_locator,'".$param2."');".end_tx($param1);}
|
659 |
|
660 | function read_intent($raw_intent) {$twb = $GLOBALS['tagui_web_browser'];
|
661 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
662 | $param1 = trim(substr($params,0,strpos($params," to "))); $param2 = trim(substr($params,4+strpos($params," to ")));
|
663 | if (is_sikuli($param1) and (strpos($params," to ")!==false)) {
|
664 | $abs_param1 = abs_file($param1); $abs_intent = str_replace($param1,$abs_param1,$raw_intent);
|
665 | return "casper.then(function() {".
|
666 | call_sikuli($abs_intent,$abs_param1,$param2." = fetch_sikuli_text(); clear_sikuli_text();");}
|
667 | if ((strtolower($param1) == "page") and ($param2 != ""))
|
668 | return "casper.then(function() {"."{techo('".$raw_intent."');\n".$param2." = ".$twb.".getHTML();}".end_fi()."});"."\n\n";
|
669 | if (($param1 == "") or ($param2 == ""))
|
670 | echo "ERROR - " . current_line() . " target/variable missing for " . $raw_intent . "\n"; else
|
671 | return "casper.then(function() {".
|
672 | "{techo('".$raw_intent."');".beg_tx($param1).$param2." = ".$twb.".fetchText(tx('".$param1."')).trim();".end_tx($param1);}
|
673 |
|
674 | function show_intent($raw_intent) {$twb = $GLOBALS['tagui_web_browser'];
|
675 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
676 | if (is_sikuli($params)) {
|
677 | $abs_params = abs_file($params); $abs_intent = str_replace($params,$abs_params,$raw_intent);
|
678 | return "casper.then(function() {".
|
679 | call_sikuli($abs_intent,$abs_params,"this.echo(fetch_sikuli_text()); clear_sikuli_text();");}
|
680 | if (strtolower($params) == "page") return "casper.then(function() {".
|
681 | "this.echo('".$raw_intent."' + ' - \\n' + ".$twb.".getHTML());".end_fi()."});"."\n\n";
|
682 | if ($params == "") echo "ERROR - " . current_line() . " target missing for " . $raw_intent . "\n"; else
|
683 | return "casper.then(function() {"."{techo('".$raw_intent."');".beg_tx($params).
|
684 | "this.echo(".$twb.".fetchText(tx('" . $params . "')).trim());".end_tx($params);}
|
685 |
|
686 | function upload_intent($raw_intent) {$twb = $GLOBALS['tagui_web_browser'];
|
687 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
688 | $param1 = trim(substr($params,0,strpos($params," as "))); $param2 = trim(substr($params,4+strpos($params," as ")));
|
689 | if (($param1 == "") or ($param2 == ""))
|
690 | echo "ERROR - " . current_line() . " filename missing for " . $raw_intent . "\n"; else
|
691 | return "casper.then(function() {"."{techo('".$raw_intent."');".beg_tx($param1).
|
692 | $twb.".page.uploadFile(tx('".$param1."'),'".abs_file($param2)."');".end_tx($param1);}
|
693 |
|
694 | function down_intent($raw_intent) {$twb = $GLOBALS['tagui_web_browser'];
|
695 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
696 | $param1 = trim(substr($params,0,strpos($params," to "))); $param2 = trim(substr($params,4+strpos($params," to ")));
|
697 | if (($param1 == "") or ($param2 == ""))
|
698 | echo "ERROR - " . current_line() . " url/filename missing for " . $raw_intent . "\n"; else
|
699 | return "casper.then(function() {"."{techo('".$raw_intent."');\n".$twb.".download('".$param1."','".abs_file($param2)."');}".end_fi()."});"."\n\n";}
|
700 |
|
701 | function receive_intent($raw_intent) {$twb = $GLOBALS['tagui_web_browser'];
|
702 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
703 | $param1 = trim(substr($params,0,strpos($params," to "))); $param2 = trim(substr($params,4+strpos($params," to ")));
|
704 | if (($param1 == "") or ($param2 == ""))
|
705 | echo "ERROR - " . current_line() . " keyword/filename missing for " . $raw_intent . "\n"; else
|
706 | return "casper.then(function() {"."{techo('".$raw_intent."');\n".
|
707 | "casper.on('resource.received', function(resource) {if (resource.stage !== 'end') return;\n".
|
708 | "if (resource.url.indexOf('".$param1."') > -1) ".$twb.".download(resource.url, '".abs_file($param2)."');});}".end_fi()."});"."\n\n";}
|
709 |
|
710 | function echo_intent($raw_intent) {
|
711 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
712 | if ($params == "") echo "ERROR - " . current_line() . " text missing for " . $raw_intent . "\n"; else
|
713 | return "casper.then(function() {"."this.echo(".add_concat($params).");".end_fi()."});"."\n\n";}
|
714 |
|
715 | function save_intent($raw_intent) {$twb = $GLOBALS['tagui_web_browser'];
|
716 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
717 | $param1 = trim(substr($params,0,strpos($params," to "))); $param2 = trim(substr($params,4+strpos($params," to ")));
|
718 | if (is_sikuli($param1) and (strpos($params," to ")!==false)) {
|
719 | $abs_param1 = abs_file($param1); $abs_intent = str_replace($param1,$abs_param1,$raw_intent);
|
720 | return "casper.then(function() {".
|
721 | call_sikuli($abs_intent,$abs_param1,"save_text('".abs_file($param2)."',fetch_sikuli_text()); clear_sikuli_text();");}
|
722 | else if (is_sikuli($params) and (strpos($params," to ")==false)) {
|
723 | $abs_params = abs_file($params); $abs_intent = str_replace($params,$abs_params,$raw_intent);
|
724 | return "casper.then(function() {".
|
725 | call_sikuli($abs_intent,$abs_param1,"save_text('',fetch_sikuli_text()); clear_sikuli_text();");}
|
726 | if ((strtolower($params) == "page") or (strtolower($param1) == "page")) {if (strpos($params," to ")!==false)
|
727 | return "casper.then(function() {".
|
728 | "{techo('".$raw_intent."');\nsave_text('".abs_file($param2)."',".$twb.".getHTML());}".end_fi()."});"."\n\n";
|
729 | else return "casper.then(function() {"."{techo('".$raw_intent."');\nsave_text('',".$twb.".getHTML());}".end_fi()."});"."\n\n";}
|
730 | if ($params == "") echo "ERROR - " . current_line() . " target missing for " . $raw_intent . "\n";
|
731 | else if (strpos($params," to ")!==false)
|
732 | return "casper.then(function() {"."{techo('".$raw_intent."');".beg_tx($param1).
|
733 | "save_text('".abs_file($param2)."',".$twb.".fetchText(tx('".$param1."')).trim());".end_tx($param1); else
|
734 | return "casper.then(function() {"."{techo('".$raw_intent."');".beg_tx($params).
|
735 | "save_text('',".$twb.".fetchText(tx('" . $params . "')).trim());".end_tx($params);}
|
736 |
|
737 | function dump_intent($raw_intent) {
|
738 | $safe_intent = str_replace("'","\'",$raw_intent);
|
739 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
740 | $param1 = trim(substr($params,0,strpos($params," to "))); $param2 = trim(substr($params,4+strpos($params," to ")));
|
741 | if ($params == "") echo "ERROR - " . current_line() . " variable missing for " . $raw_intent . "\n";
|
742 | else if (strpos($params," to ")!==false)
|
743 | return "casper.then(function() {".
|
744 | "{techo('".$safe_intent."');\nsave_text('".abs_file($param2)."',".add_concat($param1).");}".end_fi()."});"."\n\n";
|
745 | else return "casper.then(function() {".
|
746 | "{techo('".$safe_intent."');\nsave_text(''," . add_concat($params) . ");}".end_fi()."});"."\n\n";}
|
747 |
|
748 | function write_intent($raw_intent) {
|
749 | $safe_intent = str_replace("'","\'",$raw_intent);
|
750 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
751 | $param1 = trim(substr($params,0,strpos($params," to "))); $param2 = trim(substr($params,4+strpos($params," to ")));
|
752 | if ($params == "") echo "ERROR - " . current_line() . " variable missing for " . $raw_intent . "\n";
|
753 | else if (strpos($params," to ")!==false)
|
754 | return "casper.then(function() {".
|
755 | "{techo('".$safe_intent."');\nappend_text('".abs_file($param2)."',".add_concat($param1).");}".end_fi()."});"."\n\n";
|
756 | else return "casper.then(function() {".
|
757 | "{techo('".$safe_intent."');\nappend_text(''," . add_concat($params) . ");}".end_fi()."});"."\n\n";}
|
758 |
|
759 | function load_intent($raw_intent) {
|
760 | $safe_intent = str_replace("'","\'",$raw_intent);
|
761 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
762 | $param1 = trim(substr($params,0,strpos($params," to "))); $param2 = trim(substr($params,4+strpos($params," to ")));
|
763 | if ($params == "") echo "ERROR - " . current_line() . " filename missing for " . $raw_intent . "\n";
|
764 | else if (strpos($params," to ")!==false)
|
765 | return "casper.then(function() {"."{techo('".$safe_intent."');\nvar fs = require('fs'); ".$param2." = '';\n".
|
766 | "if (fs.exists('".abs_file($param1)."'))\n".$param2." = fs.read('".abs_file($param1)."').trim();\n".
|
767 | "else this.echo('ERROR - cannot find file ".$param1."').exit();}".end_fi()."});"."\n\n";
|
768 | else echo "ERROR - " . current_line() . " variable missing for " . $raw_intent . "\n";}
|
769 |
|
770 | function snap_intent($raw_intent) {$twb = $GLOBALS['tagui_web_browser'];
|
771 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
772 | $param1 = trim(substr($params,0,strpos($params," to "))); $param2 = trim(substr($params,4+strpos($params," to ")));
|
773 | if (is_sikuli($param1) and (strpos($params," to ")!==false)) {
|
774 | $abs_param1 = abs_file($param1); $abs_intent = str_replace($param1,$abs_param1,$raw_intent);
|
775 | $abs_param2 = abs_file($param2); $abs_intent = str_replace($param2,$abs_param2,$abs_intent);
|
776 | return "casper.then(function() {".call_sikuli($abs_intent,$abs_param1);}
|
777 | else if (is_sikuli($params) and (strpos($params," to ")==false)) {
|
778 | $abs_params = abs_file($params); $abs_intent = str_replace($params,$abs_params,$raw_intent);
|
779 | return "casper.then(function() {".call_sikuli($abs_intent.' to snap_image()',$abs_params);}
|
780 | if ((strtolower($params) == "page") or (strtolower($param1) == "page")) {if (strpos($params," to ")!==false)
|
781 | return "casper.then(function() {".
|
782 | "{techo('".$raw_intent."');\n".$twb.".capture('".abs_file($param2)."');}".end_fi()."});"."\n\n";
|
783 | else return "casper.then(function() {".
|
784 | "{techo('".$raw_intent."');\n".$twb.".capture(snap_image());}".end_fi()."});"."\n\n";}
|
785 | if ($params == "") echo "ERROR - " . current_line() . " target missing for " . $raw_intent . "\n";
|
786 | else if (strpos($params," to ")!==false)
|
787 | return "casper.then(function() {"."{techo('".$raw_intent."');".beg_tx($param1).
|
788 | $twb.".captureSelector('".abs_file($param2)."',tx('".$param1."'));".end_tx($param1); else
|
789 | return "casper.then(function() {"."{techo('".$raw_intent."');".beg_tx($params).
|
790 | $twb.".captureSelector(snap_image(),tx('".$params."'));".end_tx($params);}
|
791 |
|
792 | function table_intent($raw_intent) {$twb = $GLOBALS['tagui_web_browser'];
|
793 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
794 | $param1 = trim(substr($params,0,strpos($params," to "))); $param2 = trim(substr($params,4+strpos($params," to ")));
|
795 | if ($params == "") echo "ERROR - " . current_line() . " target missing for " . $raw_intent . "\n";
|
796 | else if (strpos($params," to ")!==false)
|
797 | return "casper.then(function() {"."{techo('".$raw_intent."');".beg_tx($param1).
|
798 | "save_table('".abs_file($param2)."',tx('".$param1."'));".end_tx($param1); else
|
799 | return "casper.then(function() {"."{techo('".$raw_intent."');".beg_tx($params).
|
800 | "save_table('',tx('".$params."'));".end_tx($params);}
|
801 |
|
802 | function wait_intent($raw_intent) {
|
803 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," "))); if ($params == "") $params = "5";
|
804 | if (strpos($params,"'+")!==false and strpos($params,"+'")!==false)
|
805 | return "casper.then(function() {".
|
806 | "techo('".$raw_intent."');});\ncasper.then(function() {casper.wait((parseFloat('".$params."')*1000), function() {"."});});"."\n\n";
|
807 | else return "casper.then(function() {".
|
808 | "techo('".$raw_intent."');});\ncasper.then(function() {casper.wait(" . (floatval($params)*1000) . ", function() {"."});});"."\n\n";}
|
809 |
|
810 | function live_intent($raw_intent) {
|
811 | return "casper.then(function() {".
|
812 | "{var live_input = '';\nvar sys = require('system'); sys.stdout.write('LIVE MODE - type done to quit\\n \\b');\n".
|
813 | "while (true) {live_input = sys.stdin.readLine(); // evaluate input in casperjs context until done is entered\n".
|
814 | "if (live_input.indexOf('done') == 0) break; eval(tagui_parse(live_input));}}".end_fi()."});"."\n\n";}
|
815 |
|
816 | function ask_intent($raw_intent) {
|
817 | $raw_intent = str_replace("\'","'",$raw_intent); $raw_intent = str_replace("'","\'",$raw_intent);
|
818 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
819 | if ($params == "") echo "ERROR - " . current_line() . " prompt missing for " . $raw_intent . "\n";
|
820 | else if (getenv('tagui_web_browser')=='chrome') {
|
821 | return "casper.then(function() {".
|
822 | "{ask_result = ''; ask_result = chrome.evaluate(function() {\nreturn prompt('".$params."');}); ".
|
823 | "if (ask_result == null) ask_result = '';}".end_fi()."});"."\n\n";}
|
824 | else return "casper.then(function() {".
|
825 | "{ask_result = ''; var sys = require('system');\nthis.echo('".$params." '); ".
|
826 | "ask_result = sys.stdin.readLine();}".end_fi()."});"."\n\n";}
|
827 |
|
828 | function check_intent($raw_intent) {
|
829 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
830 | $params = str_replace("||"," JAVASCRIPT_OR ",$params);
|
831 | $param1 = trim(substr($params,0,strpos($params,"|"))); $param2 = trim(substr($params,1+strpos($params,"|")));
|
832 | $param3 = trim(substr($param2,1+strpos($param2,"|"))); $param2 = trim(substr($param2,0,strpos($param2,"|")));
|
833 | $param1 = str_replace(" JAVASCRIPT_OR ","||",$param1);
|
834 | $param2 = str_replace(" JAVASCRIPT_OR ","||",$param2); $param3 = str_replace(" JAVASCRIPT_OR ","||",$param3);
|
835 | if (substr_count($params,"|")!=2)
|
836 | echo "ERROR - " . current_line() . " if/true/false missing for " . $raw_intent . "\n";
|
837 | else if (getenv('tagui_test_mode') == 'true') return "casper.then(function() {"."{".parse_condition("if ".$param1).
|
838 | "\ntest.assert(true,".add_concat($param2).");\nelse test.assert(false,".add_concat($param3).");}".end_fi()."});"."\n\n";
|
839 | else return "casper.then(function() {"."{".parse_condition("if ".$param1)."\nthis.echo(".add_concat($param2).");\nelse this.echo(".add_concat($param3).");}".end_fi()."});"."\n\n";}
|
840 |
|
841 | function test_intent($raw_intent) {
|
842 | echo "ERROR - " . current_line() . " use CasperJS tester module to professionally " . $raw_intent . "\n";
|
843 | echo "ERROR - " . current_line() . " info at http://docs.casperjs.org/en/latest/modules/tester.html" . "\n";
|
844 | echo "ERROR - " . current_line() . " support CSS selector or tx('selector') for XPath algo by TagUI" . "\n";}
|
845 |
|
846 | function frame_intent($raw_intent) {
|
847 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
848 | $param1 = trim(substr($params,0,strpos($params,"|"))); $param2 = trim(substr($params,1+strpos($params,"|")));
|
849 | if ($params == "") echo "ERROR - " . current_line() . " name missing for " . $raw_intent . "\n";
|
850 | else if (strpos($params,"|")!==false)
|
851 | {$GLOBALS['code_block_tracker'] .= "|" . 'dframe';
|
852 | return "casper.then(function() {"."techo('".$raw_intent."');});\n".
|
853 | "casper.withFrame('".$param1."', function() {casper.withFrame('".$param2."', function() {\n";} else
|
854 | {$GLOBALS['code_block_tracker'] .= "|" . 'frame';
|
855 | return "casper.then(function() {"."techo('".$raw_intent."');});\n"."casper.withFrame('".$params."', function() {\n";}}
|
856 |
|
857 | function popup_intent($raw_intent) {
|
858 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
859 | if ($params == "") echo "ERROR - " . current_line() . " keyword missing for " . $raw_intent . "\n";
|
860 | else {$GLOBALS['code_block_tracker'] .= "|" . 'popup';
|
861 | return "casper.then(function() {"."techo('".$raw_intent."');"."});\n".
|
862 | "casper.waitForPopup(/".preg_quote($params)."/, function then() {},\n".
|
863 | "function timeout() {this.echo('ERROR - cannot find popup ".$params."').exit();});\n".
|
864 | "casper.withPopup(/".preg_quote($params)."/, function() {\n";}}
|
865 |
|
866 | function api_intent($raw_intent) {
|
867 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
868 | if ($params == "") echo "ERROR - " . current_line() . " API URL missing for " . $raw_intent . "\n"; else
|
869 | return "casper.then(function() {"."{techo('".$raw_intent."');\napi_result = ''; api_result = call_api('".$params."');\n".
|
870 | "try {api_json = JSON.parse(api_result);} catch(e) {api_json = JSON.parse('null');}}".end_fi()."});"."\n\n";}
|
871 |
|
872 | function run_intent($raw_intent) {
|
873 | $raw_intent = str_replace('\\','\\\\',$raw_intent);
|
874 | if (strtolower($raw_intent) == "run begin") {$GLOBALS['inside_run_block'] = 1; return "";}
|
875 | else if (strtolower($raw_intent) == "run finish") {$GLOBALS['inside_run_block'] = 0; return "";}
|
876 | if ($GLOBALS['inside_run_block'] == 1) $raw_intent = "run " . $raw_intent;
|
877 | $safe_intent = str_replace("'","\'",$raw_intent);
|
878 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
879 | if ($params == "") echo "ERROR - " . current_line() . " command to run missing for " . $raw_intent . "\n"; else
|
880 | return "casper.then(function() {"."techo('".$safe_intent."');});\ncasper.then(function() {".
|
881 | "casper.waitForExec('".$params."', null, function(response) {run_result = '';\n" .
|
882 | "run_result = (response.data.stdout.trim() || response.data.stderr.trim()); " .
|
883 | "run_result = run_result.replace(/\\r\\n/g,'\\n');\nrun_json = response.data;}, function() {" .
|
884 | "this.echo('ERROR - command to run exceeded '+(casper.options.waitTimeout/1000).toFixed(1)+'s timeout').exit();},".
|
885 | "casper.options.waitTimeout);});\n\n";}
|
886 |
|
887 | function dom_intent($raw_intent) {$twb = $GLOBALS['tagui_web_browser']; if (strtolower($raw_intent) == "dom begin")
|
888 | {$GLOBALS['inside_dom_block'] = 1; $GLOBALS['integration_block_body'] = "dom "; return "";}
|
889 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
890 | if ($params == "") echo "ERROR - " . current_line() . " statement missing for " . $raw_intent . "\n";
|
891 | else return "casper.then(function() {"."dom_result = ''; dom_result = ".$twb.".evaluate(function(dom_json) {".$params."}, dom_json);".end_fi()."});"."\n\n";}
|
892 |
|
893 | function js_intent($raw_intent) {if (strtolower($raw_intent) == "js begin")
|
894 | {$GLOBALS['inside_js_block'] = 1; $GLOBALS['integration_block_body'] = "js "; return "";}
|
895 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
896 | if ($params == "") echo "ERROR - " . current_line() . " statement missing for " . $raw_intent . "\n";
|
897 | else return "casper.then(function() { // start of JS code\n".$params."\n}); // end of JS code"."\n\n";}
|
898 |
|
899 | function r_intent($raw_intent) {if (strtolower($raw_intent) == "r begin")
|
900 | {$GLOBALS['inside_r_block'] = 1; $GLOBALS['integration_block_body'] = "r "; return "";}
|
901 | $raw_intent = str_replace('\\','\\\\',$raw_intent);
|
902 | $raw_intent = str_replace('[END_OF_LINE]','\\n',$raw_intent);
|
903 | $safe_intent = str_replace("'","\'",$raw_intent);
|
904 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
905 | if ($params == "") echo "ERROR - " . current_line() . " R command(s) missing for " . $raw_intent . "\n"; else
|
906 | return "casper.then(function() {".call_r($safe_intent);}
|
907 |
|
908 | function py_intent($raw_intent) {if (strtolower($raw_intent) == "py begin")
|
909 | {$GLOBALS['inside_py_block'] = 1; $GLOBALS['integration_block_body'] = "py "; return "";}
|
910 | $raw_intent = str_replace('\\','\\\\',$raw_intent);
|
911 | $raw_intent = str_replace('[END_OF_LINE]','\\n',$raw_intent);
|
912 | $safe_intent = str_replace("'","\'",$raw_intent);
|
913 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
914 | if ($params == "") echo "ERROR - " . current_line() . " Python command(s) missing for " . $raw_intent . "\n"; else
|
915 | return "casper.then(function() {".call_py($safe_intent);}
|
916 |
|
917 | function vision_intent($raw_intent) {if (strtolower($raw_intent) == "vision begin")
|
918 | {$GLOBALS['inside_vision_block'] = 1; $GLOBALS['integration_block_body'] = "vision "; return "";}
|
919 | $raw_intent = str_replace('\\','\\\\',$raw_intent);
|
920 | $raw_intent = str_replace('[END_OF_LINE]','\\n',$raw_intent);
|
921 | $safe_intent = str_replace("'","\'",$raw_intent);
|
922 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
923 | if ($params == "") echo "ERROR - " . current_line() . " Sikuli command(s) missing for " . $raw_intent . "\n"; else
|
924 | return "casper.then(function() {".call_sikuli($safe_intent,'for vision step');}
|
925 |
|
926 | function timeout_intent($raw_intent) {
|
927 | $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," ")));
|
928 | if ($params == "") echo "ERROR - " . current_line() . " time in seconds missing for " . $raw_intent . "\n";
|
929 | else return "casper.then(function() {"."casper.options.waitTimeout = " . (floatval($params)*1000) . ";" . end_fi()."});"."\n\n";}
|
930 |
|
931 | function code_intent($raw_intent) {
|
932 | $params = parse_condition($raw_intent);
|
933 | if (substr($raw_intent,0,2)=="//") return $params."\n";
|
934 | if ((substr($raw_intent,0,1)=="{") or (substr($raw_intent,0,1)=="}")) return $params."\n";
|
935 | if ((substr($params,0,3)=="if ") or (substr($params,0,8)=="else if ")
|
936 | or (substr($params,0,4)=="for ") or (substr($params,0,6)=="while ") or
|
937 | (trim($raw_intent)=="else")) return "casper.then(function() {".$params."\n";
|
938 | else if ($GLOBALS['inside_while_loop'] == 1) return $params."\n";
|
939 | else return "casper.then(function() { // start of JS code\n".$params."\n}); // end of JS code"."\n\n";}
|
940 |
|
941 | function parse_condition($logic) {
|
942 | $raw_logic = $logic;
|
943 | if (substr($logic,0,2)=="//") return $logic;
|
944 |
|
945 |
|
946 |
|
947 | if (substr($logic,0,1) == "{") $GLOBALS['inside_code_block']++;
|
948 | if (substr($logic,0,1) == "}") $GLOBALS['inside_code_block']--;
|
949 | $code_block_header = ""; $code_block_footer = "";
|
950 | $last_delimiter_pos = strrpos($GLOBALS['code_block_tracker'],"|");
|
951 | $code_block_intent = substr($GLOBALS['code_block_tracker'],$last_delimiter_pos+1);
|
952 |
|
953 | if (($GLOBALS['inside_code_block'] > substr_count($GLOBALS['code_block_tracker'],"|")) and (substr($logic,0,1) == "{"))
|
954 | {$GLOBALS['code_block_tracker'] .= "|" . 'normal_bracket'; return $logic."\n";}
|
955 | else if ((substr($logic,0,1) == "}") and ($code_block_intent == "normal_bracket"))
|
956 | {$GLOBALS['code_block_tracker']=substr($GLOBALS['code_block_tracker'],0,$last_delimiter_pos); return $logic."\n";}
|
957 |
|
958 | if (($code_block_intent == "if") or ($code_block_intent == "else if") or ($code_block_intent == "else")
|
959 | or ($code_block_intent == "popup") or ($code_block_intent == "frame")) {if (substr($logic,0,1) == "}")
|
960 | $GLOBALS['code_block_tracker']=substr($GLOBALS['code_block_tracker'],0,$last_delimiter_pos);}
|
961 |
|
962 | else if (($code_block_intent == "for") and (substr($logic,0,1) == "{")) {
|
963 | $last_delimiter_pos = strrpos($GLOBALS['for_loop_tracker'],"|");
|
964 | $for_loop_variable_name = substr($GLOBALS['for_loop_tracker'],$last_delimiter_pos+1);
|
965 | $code_block_header = "{casper.then(function() {for_loop_signal = '[CONTINUE_SIGNAL][".$for_loop_variable_name."]';});\n".
|
966 | "(function (" . $for_loop_variable_name . ") { // start of IIFE pattern\n";}
|
967 |
|
968 | else if (($code_block_intent == "for") and (substr($logic,0,1) == "}")) {
|
969 | $last_delimiter_pos = strrpos($GLOBALS['for_loop_tracker'],"|");
|
970 | $for_loop_variable_name = substr($GLOBALS['for_loop_tracker'],$last_delimiter_pos+1);
|
971 | $code_block_footer = "})(" . $for_loop_variable_name . "); // end of IIFE pattern, with dummy marker for break step\n".
|
972 | "casper.then(function() {for_loop_signal = '[BREAK_SIGNAL][".$for_loop_variable_name."]';});}";
|
973 | $last_delimiter_pos = strrpos($GLOBALS['code_block_tracker'],"|");
|
974 | $GLOBALS['code_block_tracker']=substr($GLOBALS['code_block_tracker'],0,$last_delimiter_pos);
|
975 | $last_delimiter_pos = strrpos($GLOBALS['for_loop_tracker'],"|");
|
976 | $GLOBALS['for_loop_tracker']=substr($GLOBALS['for_loop_tracker'],0,$last_delimiter_pos);}
|
977 |
|
978 | else if ($code_block_intent == "dframe") {if (substr($logic,0,1) == "}") {$code_block_footer = "});";
|
979 | $GLOBALS['code_block_tracker']=substr($GLOBALS['code_block_tracker'],0,$last_delimiter_pos);}}
|
980 |
|
981 | else if ($code_block_intent == "while") {if (substr($logic,0,1) == "}") {$GLOBALS['inside_while_loop'] = 0;
|
982 | $GLOBALS['code_block_tracker']=substr($GLOBALS['code_block_tracker'],0,$last_delimiter_pos);}}
|
983 |
|
984 | if (substr($logic,0,1) == "{") $logic = $code_block_header."{ // start of code block\n".substr($logic,1)."\n";
|
985 | else if (substr($logic,0,1) == "}") $logic = "} // end of code block\n".substr($logic,1)."\n".$code_block_footer."});\n";
|
986 | $logic = str_replace("\n\n","\n",$logic);
|
987 |
|
988 |
|
989 | if ((substr($logic,0,3)=="if ") or (substr($logic,0,8)=="else if ")
|
990 | or (substr($logic,0,4)=="for ") or (substr($logic,0,6)=="while ")) {
|
991 |
|
992 | $logic = str_replace(" more than or equals to "," >= ",$logic);
|
993 | $logic = str_replace(" greater than or equals to "," >= ",$logic);
|
994 | $logic = str_replace(" higher than or equals to "," >= ",$logic);
|
995 | $logic = str_replace(" less than or equals to "," <= ",$logic);
|
996 | $logic = str_replace(" lesser than or equals to "," <= ",$logic);
|
997 | $logic = str_replace(" lower than or equals to "," <= ",$logic);
|
998 | $logic = str_replace(" more than or equal to "," >= ",$logic);
|
999 | $logic = str_replace(" greater than or equal to "," >= ",$logic);
|
1000 | $logic = str_replace(" higher than or equal to "," >= ",$logic);
|
1001 | $logic = str_replace(" less than or equal to "," <= ",$logic);
|
1002 | $logic = str_replace(" lesser than or equal to "," <= ",$logic);
|
1003 | $logic = str_replace(" lower than or equal to "," <= ",$logic);
|
1004 |
|
1005 | $logic = str_replace(" more than "," > ",$logic); $logic = str_replace(" greater than "," > ",$logic);
|
1006 | $logic = str_replace(" higher than "," > ",$logic); $logic = str_replace(" less than "," < ",$logic);
|
1007 | $logic = str_replace(" lesser than "," < ",$logic); $logic = str_replace(" lower than "," < ",$logic);
|
1008 | $logic = str_replace(" not equals to "," != ",$logic); $logic = str_replace(" equals to "," == ",$logic);
|
1009 | $logic = str_replace(" not equal to "," != ",$logic); $logic = str_replace(" equal to "," == ",$logic);
|
1010 |
|
1011 |
|
1012 | $contain_list = array(" not contains ", " not contain ", " contains ", " contain ");
|
1013 | foreach ($contain_list as $contain_type) {
|
1014 | for ($condition_counter=1;$condition_counter<=5;$condition_counter++) {
|
1015 | if (strpos($logic,$contain_type)==!false) {$pos_keyword = strpos($logic,$contain_type);
|
1016 | $pos_single_quote = strpos($logic,"'",$pos_keyword+strlen($contain_type));
|
1017 | if ($pos_single_quote == false) $pos_single_quote = 1024;
|
1018 | $pos_double_quote = strpos($logic,"\"",$pos_keyword+strlen($contain_type));
|
1019 | if ($pos_double_quote == false) $pos_double_quote = 1024;
|
1020 | if ($pos_double_quote < $pos_single_quote) {$pos_quote_start = $pos_double_quote; $quote_type = "\"";}
|
1021 | else if ($pos_single_quote < $pos_double_quote) {$pos_quote_start = $pos_single_quote; $quote_type = "'";}
|
1022 | else {echo "ERROR - " . current_line() . " no quoted text - " . $raw_logic . "\n"; $quote_type = "missing";}
|
1023 | if ($quote_type != "missing") {$pos_quote_end = strpos($logic,$quote_type,$pos_quote_start+1);
|
1024 | $pos_variable_start = strrpos($logic," ",$pos_keyword-strlen($logic)-2); $contain_operator = "<0";
|
1025 | if (($contain_type == " contains ") or ($contain_type == " contain ")) $contain_operator = ">-1";
|
1026 | $logic = substr($logic,0,$pos_variable_start+1)."(".
|
1027 | trim(substr($logic,$pos_variable_start,$pos_keyword-$pos_variable_start)).".indexOf(".
|
1028 | $quote_type.substr($logic,$pos_quote_start+1,$pos_quote_end-$pos_quote_start-1).
|
1029 | $quote_type.")".$contain_operator.")".substr($logic,$pos_quote_end+1);}}
|
1030 | else break;}}
|
1031 |
|
1032 |
|
1033 | $logic = str_replace(" and ",") && (",$logic); $logic = str_replace(" or ",") || (",$logic);
|
1034 |
|
1035 |
|
1036 | if ((substr($logic,0,4)=="for ") and (strpos($logic,";")==false)) {
|
1037 | if (strpos($raw_logic,"count(")!==false)
|
1038 | echo "ERROR - " . current_line() . " assign count() to variable before using - " . $raw_logic . "\n";
|
1039 | $logic = str_replace("(","",$logic); $logic = str_replace(")","",$logic);
|
1040 | $logic = str_replace(" "," ",$logic); $logic = str_replace(" "," ",$logic);
|
1041 | $token = explode(" ",$logic);
|
1042 | if (strpos($raw_logic,"{")!==false)
|
1043 | echo "ERROR - " . current_line() . " put { to next line - " . $raw_logic . "\n";
|
1044 | else if (count($token) != 6) echo "ERROR - " . current_line() . " invalid for loop - " . $raw_logic . "\n";
|
1045 | else $logic = $token[0]." (".$token[1]."=".$token[3]."; ".$token[1]."<=".$token[5]."; ".$token[1]."++)";}
|
1046 | else if ((substr($logic,0,4)=="for ") and (strpos($raw_logic,"{")!==false))
|
1047 | echo "ERROR - " . current_line() . " put { to next line - " . $raw_logic . "\n";
|
1048 |
|
1049 |
|
1050 | if (substr($logic,0,4)=="for ") {
|
1051 | $GLOBALS['code_block_tracker'] .= "|" . 'for';
|
1052 | $GLOBALS['for_loop_tracker'] .= "|" . trim(substr($logic,strpos($logic,"(")+1,strpos($logic,"=")-strpos($logic,"(")-1));}
|
1053 |
|
1054 |
|
1055 | if (substr($logic,0,3)=="if ") {
|
1056 | $GLOBALS['code_block_tracker'] .= "|" . 'if'; $logic = "if ((" . trim(substr($logic,3)) . "))";
|
1057 | if (strpos($raw_logic,"{")!==false) echo "ERROR - " . current_line() . " put { to next line - " . $raw_logic . "\n";}
|
1058 | if (substr($logic,0,8)=="else if ") {
|
1059 | $GLOBALS['code_block_tracker'] .= "|" . 'else if'; $logic = "else if ((" . trim(substr($logic,8)) . "))";
|
1060 | if (strpos($raw_logic,"{")!==false) echo "ERROR - " . current_line() . " put { to next line - " . $raw_logic . "\n";}
|
1061 | if (substr($logic,0,6)=="while ") {
|
1062 | $GLOBALS['code_block_tracker'] .= "|" . 'while'; $logic = "while ((" . trim(substr($logic,6)) . "))";
|
1063 | if (strpos($raw_logic,"{")!==false) echo "ERROR - " . current_line() . " put { to next line - " . $raw_logic . "\n";}}
|
1064 |
|
1065 |
|
1066 | if (trim($logic)=="else") {$GLOBALS['code_block_tracker'] .= "|" . 'else'; $logic = "else";
|
1067 | if (strpos($raw_logic,"{")!==false) echo "ERROR - " . current_line() . " put { to next line - " . $raw_logic . "\n";}
|
1068 |
|
1069 |
|
1070 |
|
1071 | if (substr($logic,0,6)=="while ") $GLOBALS['inside_while_loop'] = 1;
|
1072 |
|
1073 |
|
1074 | if (($logic=="break") or ($logic=="break;"))
|
1075 | {$teleport_marker = str_replace("|","",substr($GLOBALS['for_loop_tracker'],strrpos($GLOBALS['for_loop_tracker'],"|")));
|
1076 | $logic = "casper.bypass(teleport_distance('[BREAK_SIGNAL][".$teleport_marker."]')); return;";}
|
1077 | if (($logic=="continue") or ($logic=="continue;"))
|
1078 | {$teleport_marker = str_replace("|","",substr($GLOBALS['for_loop_tracker'],strrpos($GLOBALS['for_loop_tracker'],"|")));
|
1079 | $logic = "casper.bypass(teleport_distance('[CONTINUE_SIGNAL][".$teleport_marker."]')); return;";}
|
1080 |
|
1081 |
|
1082 | return $logic;}
|
1083 |
|
1084 | ?>
|