Skip to content

Commit

Permalink
add skeleton to tagui_header.js for #24
Browse files Browse the repository at this point in the history
looks like there isn't a direct way to use xpath for chrome debugging protocol.

next steps
- verify how to access by xpath
- add websocket helper code
  • Loading branch information
kensoh authored Jun 21, 2017
1 parent 41a8a40 commit b65d582
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
59 changes: 56 additions & 3 deletions src/tagui_header.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,58 @@ var sikuli_result = ''; do {sikuli_result = fs.read('tagui.sikuli'+ds+'tagui_sik
while (sikuli_result.indexOf('['+sikuli_count.toString()+'] ') == -1);
if (sikuli_result.indexOf('SUCCESS') !== -1) return true; else return false;}

// chrome object for handling integration with chrome or headless chrome
var chrome = new Object(); chrome.mouse = new Object();

// chrome methods as casper methods replacement for chrome integration
chrome.click = function(selector) {
};

chrome.mouse.move = function(selector) {
};

chrome.sendKeys = function(selector,value) {
};

chrome.selectOptionByValue = function(selector,value) {
};

chrome.fetchText = function(selector) {
return '';
};

chrome.capture = function(filename) {
};

chrome.captureSelector = function(filename,selector) {
};

chrome.download = function(url,filename) {
};

chrome.evaluate = function(statement) {
};

chrome.getHTML = function() {
return '';
};

chrome.getTitle = function() {
return '';
};

chrome.getCurrentUrl = function() {
return '';
};

chrome.exists = function(selector) {
return false;
};

chrome.echo = function(value) {casper.echo(value);};

chrome.on = function(value,statement) {casper.on(value,statement);};

// for live mode simple parsing of tagui steps into js code
function tagui_parse(raw_input) {return parse_intent(raw_input);}

Expand Down Expand Up @@ -194,6 +246,7 @@ if ((raw_intent.substr(0,4) == 'for ') || (raw_intent.substr(0,6) == 'while '))
if ((raw_intent.substr(0,7) == 'switch ') || (raw_intent.substr(0,5) == 'case ')) return true;
if ((raw_intent.substr(0,6) == 'break;') || (raw_intent.substr(0,9) == 'function ')) return true;
if ((raw_intent.substr(0,7) == 'casper.') || (raw_intent.substr(0,5) == 'this.')) return true;
if (raw_intent.substr(0,7) == 'chrome.') return true; // chrome object for chrome integration
if (raw_intent.substr(0,5) == 'test.') return true;
if ((raw_intent.substr(0,2) == '//') || (raw_intent.charAt(raw_intent.length-1) == ';')) return true; return false;}

Expand All @@ -211,7 +264,7 @@ return "'ERROR - inconsistent quotes in text'";
else if (source_string.indexOf("'") > -1) var quote_type = "'"; // derive quote type used
else if (source_string.indexOf('"') > -1) var quote_type = '"'; else var quote_type = "none";
var within_quote = false; source_string = source_string.trim(); // trim for future proof
for (srcpos = 0; srcpos < source_string.length; srcpos++){
for (srcpos = 0; srcpos < source_string.length; srcpos++) {
if (source_string.charAt(srcpos) == quote_type) within_quote = !(within_quote);
if ((within_quote == false) && (source_string.charAt(srcpos)==" "))
source_string = source_string.substring(0,srcpos) + "+" + source_string.substring(srcpos+1);}
Expand Down Expand Up @@ -393,11 +446,11 @@ xhttp.setRequestHeader(header_value_pair[0].trim(),header_value_pair[1].trim());
xhttp.send(JSON.stringify(api_config.body)); return xhttp.responseText;}

// custom function to handle dropdown option
casper.selectOptionByValue = function(selector, valueToMatch){ // solution posted in casperjs issue #1390
casper.selectOptionByValue = function(selector, valueToMatch) { // solution posted in casperjs issue #1390
this.evaluate(function(selector, valueToMatch) {var found = false; // modified to allow xpath / css locators
if ((selector.indexOf('/') == 0) || (selector.indexOf('(') == 0)) var select = __utils__.getElementByXPath(selector);
else var select = document.querySelector(selector); // auto-select xpath or query css method to get element
Array.prototype.forEach.call(select.children, function(opt, i){ // loop through list to select option
Array.prototype.forEach.call(select.children, function(opt, i) { // loop through list to select option
if (!found && opt.value.indexOf(valueToMatch) !== -1) {select.selectedIndex = i; found = true;}});
var evt = document.createEvent("UIEvents"); // dispatch change event in case there is validation
evt.initUIEvent("change", true, true); select.dispatchEvent(evt);}, selector, valueToMatch);};
Expand Down
14 changes: 10 additions & 4 deletions src/tagui_parse.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
while(!feof($footer_file)) {fwrite($output_file,fgets($footer_file));} fclose($footer_file); fclose($output_file);
chmod ($script . '.js',0600); if (!$url_provided) echo "ERROR - first line of " . $script . " not URL\n";

// special handling if chrome or headless chrome is used as browser for automation
if ($tagui_web_browser == 'chrome') {$script_content = file_get_contents($script . '.js'); // read generated script
$script_content = str_replace("casper.exists","chrome.exists",$script_content); // change locator check to chrome
file_put_contents($script . '.js',$script_content);}

// check quiet parameter to run flow quietly by only showing explicit output
if (getenv('tagui_quiet_mode') == 'true') {$script_content = file_get_contents($script . '.js'); // read generated script
$script_content = str_replace("var quiet_mode = false;","var quiet_mode = true;",$script_content); // set quiet_mode
Expand All @@ -70,7 +75,7 @@
// following help to define the script structure required by casperjs for test automation purpose
$script_content = str_replace("casper.start(","casper.test.begin('" . str_replace("\\","\\\\",$script) . "', " .
$test_automation.", function(test) {\ncasper.start(",$script_content); // define required casperjs test structure
$script_content = str_replace("casper.run();","casper.run(function(){test.done();});});",$script_content);
$script_content = str_replace("casper.run();","casper.run(function() {test.done();});});",$script_content);
file_put_contents($script . '.js',$script_content);} // save script after restructuring for testing

// otherwise prep for normal execution by commenting out test assertions as they will kill the script
Expand Down Expand Up @@ -185,6 +190,7 @@ function is_code($raw_intent) {
if ((substr($raw_intent,0,7)=="switch ") or (substr($raw_intent,0,5)=="case ")) return true;
if ((substr($raw_intent,0,6)=="break;") or (substr($raw_intent,0,9)=="function ")) return true;
if ((substr($raw_intent,0,7)=="casper.") or (substr($raw_intent,0,5)=="this.")) return true;
if (substr($raw_intent,0,7)=="chrome.") return true; // chrome object for chrome integration
if (substr($raw_intent,0,5)=="test.") {$GLOBALS['test_automation']++; return true;}
if ((substr($raw_intent,0,2)=="//") or (substr($raw_intent,-1)==";")) return true; return false;}

Expand Down Expand Up @@ -221,7 +227,7 @@ function add_concat($source_string) { // parse string and add missing + concaten
else if (strpos($source_string,"'")!==false) $quote_type = "'"; // derive quote type used
else if (strpos($source_string,"\"")!==false) $quote_type = "\""; else $quote_type = "none";
$within_quote = false; $source_string = trim($source_string); // trim for future proof
for ($srcpos=0; $srcpos<strlen($source_string); $srcpos++){
for ($srcpos=0; $srcpos<strlen($source_string); $srcpos++) {
if ($source_string[$srcpos] == $quote_type) $within_quote = !$within_quote;
if (($within_quote == false) and ($source_string[$srcpos]==" ")) $source_string[$srcpos] = "+";}
$source_string = str_replace("+++++","+",$source_string); $source_string = str_replace("++++","+",$source_string);
Expand Down Expand Up @@ -441,7 +447,7 @@ function parse_condition($logic) { // natural language handling for conditions
$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
$logic = str_replace("{","\n// start of code block\n{casper.then(function() {",$logic);
$logic = str_replace("}","})}; // end of code block\n",$logic);}
$logic = str_replace("}","})} // end of code block\n",$logic);}

// section 2 - natural language handling for conditions and loops
if ((substr($logic,0,3)=="if ") or (substr($logic,0,8)=="else if ")
Expand Down Expand Up @@ -478,7 +484,7 @@ function parse_condition($logic) { // natural language handling for conditions
if ($pos_double_quote < $pos_single_quote) {$pos_quote_start = $pos_double_quote; $quote_type = "\"";}
else if ($pos_single_quote < $pos_double_quote) {$pos_quote_start = $pos_single_quote; $quote_type = "'";}
else {echo "ERROR - " . current_line() . " no quoted text - " . $logic . "\n"; $quote_type = "missing";}
if ($quote_type != "missing"){$pos_quote_end = strpos($logic,$quote_type,$pos_quote_start+1);
if ($quote_type != "missing") {$pos_quote_end = strpos($logic,$quote_type,$pos_quote_start+1);
$pos_variable_start = strrpos($logic," ",$pos_keyword-strlen($logic)-2); $contain_operator = "<0";
if (($contain_type == " contains ") or ($contain_type == " contain ")) $contain_operator = ">-1";
$logic = substr($logic,0,$pos_variable_start+1)."(".
Expand Down

0 comments on commit b65d582

Please sign in to comment.