Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the way variables are used while maintaining backward compatibility #241

Closed
kensoh opened this issue Jul 16, 2018 · 4 comments
Closed
Labels

Comments

@kensoh
Copy link
Member

kensoh commented Jul 16, 2018

In TagUI, whenever text is expected and user wants to use a variable in place of static text, the current way to do it is using '+variable_name+'. This works because the first ' essentially escapes the text mode to go into JavaScript programming language mode to reference the variable, followed by the second ' to go back to text mode. The + is JavaScript's way of concatenating strings. See below example -

// old way for '+variable+' backward compatibility
locator = 'email_button'
click '+locator+'
number = 5
click (//*[@id="test"])['+variable+']

The old way above works, but is non-standard, hard to write and hard to read. By using backticks ` (the symbol besides the number 1 on keyboard), it can be made easier to type and read. Of course, backticks may have conflict with object repository or datatable definitions. But as long as there isn't such repository / datatable definition, TagUI can interpret the use of `variable` as '+variable+'

// new way of using `variable` to denote variable
locator = 'email_button'
click `locator`
number = 5
click (//*[@id="test"])[`variable`]

This issue is to make this improvement while keeping existing scripts backward compatible.

kensoh added a commit that referenced this issue Jul 16, 2018
@kensoh
Copy link
Member Author

kensoh commented Jul 16, 2018

Feature usable now in cutting edge version - https://github.com/kelaberetiv/TagUI#set-up

Update of readme & samples after this code enters packaged release, otherwise confusing.

Hey @adegard @Aussiroth @lohvht I thought this is a cool feature and makes variables easier :)

@kensoh
Copy link
Member Author

kensoh commented Jul 17, 2018

following additions can semi-work to throw error gracefully if the variable is not yet defined. but it has execution issues for example in 9_misc workflow. even though for loop only until 6, during execution it goes directly to 7 and throws error. dropping exploration of graceful exit in favor of maintaining automation robustness and compatibility of existing scripts.

tagui_parse.php

function parse_variables($script_line) { // `variable` --> '+variable+'
$quote_token = "'+check_variable('"; // token to alternate replacements for '+variable+'
for ($char_counter = 0; $char_counter < strlen($script_line); $char_counter++) {
        if (substr($script_line,$char_counter,1) == "`") {
                $script_line = substr_replace($script_line,$quote_token,$char_counter,1);
                if ($quote_token == "'+check_variable('") $quote_token = "')+'"; else $quote_token = "'+check_variable('";
        }
} return $script_line;}

tagui_header.js

// variable check for graceful exit when `variable` is used and undefined
function check_variable(variable_to_check) {if (check_variable_exit == true) return 'undefined';
try {return eval(variable_to_check);} catch(e) {check_variable_exit = true; // prevent 2X error messages
casper.echo('ERROR - variable ' + variable_to_check + ' is undefined, define it before using').exit();}}

updated - issue happens with eval(),
it messes up order of execution to be non-synchronous and thus not a reliable way to try to exit gracefully using it to processes a variable name provided. pass graceful exit on variable not defined.

@adegard
Copy link
Contributor

adegard commented Jul 24, 2018

Hi @kensoh
I answer now to your question: For me it is not easier because using Italian keyboard backticks ` is not accessible unfortunatly... (so I need to copy paste to use it). But thanks to ask my opinion.

@kensoh
Copy link
Member Author

kensoh commented Jul 24, 2018

Hi Arnaud, I see... Ok got it, thanks for your feedback!

kensoh added a commit that referenced this issue Aug 19, 2018
various doc improvements, minor updates and introduction of `variable` to denote use of variable where text is expected. backward compatible with old syntax '+variable+'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants