diff --git a/src/tagui_header.js b/src/tagui_header.js index 202c04a1..54a5ed65 100644 --- a/src/tagui_header.js +++ b/src/tagui_header.js @@ -516,7 +516,7 @@ chrome.captureSelector = function(filename,selector) { // capture screenshot of // first capture entire screen, then use casperjs / phantomjs browser to crop image base on selector dimensions chrome.capture(filename); var selector_rect = chrome.getRect(selector); // so that there is no extra dependency if (selector_rect.width > 0 && selector_rect.height > 0) // from using other libraries or creating html canvas -casper.thenOpen(filename, function() {casper . capture(filename, // spaces around . intentional to avoid replacing +casper.thenOpen(file_url(filename), function() {casper . capture(filename, // spaces around . to avoid replacing {top: selector_rect.top, left: selector_rect.left, width: selector_rect.width, height: selector_rect.height}); casper.thenOpen('about:blank');});}; // reset phantomjs browser state @@ -760,6 +760,11 @@ if ((raw_intent.substr(0,2) == '//') || (raw_intent.charAt(raw_intent.length-1) // assume = is assignment statement, kinda acceptable as this is checked at the very end if (raw_intent.indexOf('=') > -1) return true; return false;} +function file_url(absolute_filename) { // helper function to append file:// according for opening local files +if (!absolute_filename || absolute_filename == '') return ''; +if (absolute_filename.substr(0,1) == '/') return 'file://' + absolute_filename; +if (absolute_filename.substr(1,1) == ':') return 'file:///' + absolute_filename; return absolute_filename;} + function abs_file(filename) { // helper function to return absolute filename if (filename == '') return ''; // unlike tagui_parse.php not deriving path from script variable if (filename.substr(0,1) == '/') return filename; // return mac/linux absolute filename directly diff --git a/src/test/positive_test.signature b/src/test/positive_test.signature index d885ac72..9604c5cd 100644 --- a/src/test/positive_test.signature +++ b/src/test/positive_test.signature @@ -224,6 +224,10 @@ if (casper.exists(x('//*[@href="'+locator+'"]'))) return true; if (casper.exists(x('//*[contains(@href,"'+locator+'")]'))) return true; return false;} +/** + * Extra TagUI helper methods + */ + // friendlier name to use check_tx() in if condition in flow function present(element_locator) {if (!element_locator) return false; else return check_tx(element_locator);} @@ -255,6 +259,44 @@ function sleep(ms) { // helper to add delay during loops var time_now = new Date().getTime(); var time_end = time_now + ms; while(time_now < time_end) {time_now = new Date().getTime();}} +/** + * string cell data sanitiser, returns a CSV formatted string + * @param {string} cell_data + */ +function sanitise_csv_cell(cell_data) { + // Replace all double quotes with 2 double quotes + cell_data = cell_data.replace(/"/g, '\"\"') + var whitespaceCheckRegex = /\s/ + // if cell_data has a comma, or new line, or its first or last character is a + // whitespace, then wrap the entire expression in double quotes + if ( + cell_data.indexOf(',') >= 0 + || cell_data.indexOf('\n') >= 0 + || whitespaceCheckRegex.test(cell_data.charAt(0)) + || whitespaceCheckRegex.test(cell_data.charAt(cell_data.length - 1)) + ) { + cell_data = '\"' + cell_data + '\"' + } + return cell_data +} + +/** + * Returns a CSV-formatted string that denotes a row in a CSV file + * @param {string[]} row_data a 1-D array of strings denoting data to + * encode as a CSV row + */ +function csv_row(row_data) { + // if row_data has at least 1 element, extract and sanitise first element + // else start_element is empty string + var start_element = (row_data.length > 0) + ? sanitise_csv_cell(row_data.shift()) + : '' + // concat each row_data with a comma + return row_data.reduce(function(accumulator, currentValue) { + return accumulator + ',' + sanitise_csv_cell(currentValue) + }, start_element) +} + // for initialising integration with sikuli visual automation function sikuli_handshake() { // techo('[connecting to sikuli process]'); var ds; if (flow_path.indexOf('/') !== -1) ds = '/'; else ds = '\\'; clear_sikuli_text(); @@ -501,7 +543,7 @@ chrome.captureSelector = function(filename,selector) { // capture screenshot of // first capture entire screen, then use casperjs / phantomjs browser to crop image base on selector dimensions chrome.capture(filename); var selector_rect = chrome.getRect(selector); // so that there is no extra dependency if (selector_rect.width > 0 && selector_rect.height > 0) // from using other libraries or creating html canvas -casper.thenOpen(filename, function() {casper . capture(filename, // spaces around . intentional to avoid replacing +casper.thenOpen(file_url(filename), function() {casper . capture(filename, // spaces around . to avoid replacing {top: selector_rect.top, left: selector_rect.left, width: selector_rect.width, height: selector_rect.height}); casper.thenOpen('about:blank');});}; // reset phantomjs browser state @@ -745,6 +787,11 @@ if ((raw_intent.substr(0,2) == '//') || (raw_intent.charAt(raw_intent.length-1) // assume = is assignment statement, kinda acceptable as this is checked at the very end if (raw_intent.indexOf('=') > -1) return true; return false;} +function file_url(absolute_filename) { // helper function to append file:// according for opening local files +if (!absolute_filename || absolute_filename == '') return ''; +if (absolute_filename.substr(0,1) == '/') return 'file://' + absolute_filename; +if (absolute_filename.substr(1,1) == ':') return 'file:///' + absolute_filename; return absolute_filename;} + function abs_file(filename) { // helper function to return absolute filename if (filename == '') return ''; // unlike tagui_parse.php not deriving path from script variable if (filename.substr(0,1) == '/') return filename; // return mac/linux absolute filename directly