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

Q: How to set a general variable? #30

Closed
thim81 opened this issue Jul 26, 2017 · 4 comments
Closed

Q: How to set a general variable? #30

thim81 opened this issue Jul 26, 2017 · 4 comments
Labels

Comments

@thim81
Copy link

thim81 commented Jul 26, 2017

Hi Ken,

Another question, is it possible to set a general variable at the beginning of a flow, that can be used everywhere in the flow?

Example:

js var project_name = "MarcoPolo" + Math.floor(Math.random()*200);

// Step 1 create project
enter //input[@placeholder="Project name"] as '+project_name+'
click submit

// Validate
echo project_name
test.assertSelectorHasText(tx('body'), project_name,'Project '+project_name+' created successfully');
@kensoh
Copy link
Member

kensoh commented Jul 26, 2017

Hi @thim81 yes, you can use below statement directly as TagUI will recognize the ; at the end of the line and interpret it as JavaScript. Using var is not wrong but in many cases, defining as local variable (for example in a popup step or frame step) will make it inaccessible in later parts of your automation. In general, defining directly using variable_name = value; is easier to make it accessible throughout.

project_name = "MarcoPolo" + Math.floor(Math.random()*200);

Sample automation flow 9 has some more examples of using variables -
https://github.com/tebelorg/TagUI/blob/master/src/samples/9_misc

@thim81
Copy link
Author

thim81 commented Jul 27, 2017

Hi @kensoh,

You were right removing the var helped with the strange behaviour that sometimes the variable became undefined

Thanks for the great help.

@thim81 thim81 closed this as completed Jul 27, 2017
@kensoh
Copy link
Member

kensoh commented Jul 27, 2017

Adding on for future reference by other users the technical reason for that -

TagUI converts the natural-language-like syntax into CasperJS syntax JavaScript code to run automation. So a simple 1 line step in many cases will be converted into a more elaborate code block which does auto-waiting of the web element to appear, error handling and timeout when it does not appear, and taking the action when it appears.

Using var declares a local variable within the context of that particular code block, which may allow the variable to be accessed directly if the reference happens within the same code block. But that will most likely fail in subsequent steps, unless the next steps are simple steps such as echo which does not really do all those handling of web element.

Declaring directly using variable_name = value; is recommended and should make it accessible throughout the automation steps.

kensoh added a commit that referenced this issue Aug 4, 2017
https://github.com/tebelorg/TagUI/issues/41 & https://github.com/tebelorg/TagUI/issues/37 - include http(s):// step to make it clearer the step keyword that is detected for url intention
https://github.com/tebelorg/TagUI/issues/40 - make it even clearer api_result is a text variable and api_json is a JSON object
https://github.com/tebelorg/TagUI/issues/39 - small adjustment to make api_config more obvious as a user missed that part
https://github.com/tebelorg/TagUI/issues/36 - update to further emphasize CasperJS assertions don't work with Chrome
https://github.com/tebelorg/TagUI/issues/30 - add variable_name step to show how to define a variable manually for use
https://github.com/tebelorg/TagUI/issues/29 - minor update to emphasize test option to use CasperJS test framework

- add (on new line) to // step to emphasize entering // on a new line. using click abc // user comment will fail as the comment part is being treated as part of the selector. XPath selector has // and there is no way to distinguish if accept // as denoting comment
kensoh added a commit that referenced this issue Aug 4, 2017
; is used to detect that it is a valid JavaScript code
variable assignments are so often used that detecting = should be
acceptable, since that check is done at the very end of checking for
TagUI steps and other JavaScript keywords.

So the following will now work and be more convenient. Update readme
only when making new release and packaged installations.

a = 1
b = ‘b’
c = ”c”

Adding on, reason for JavaScript checking instead of taking anything
that is not TagUI step as JavaScript is to try to catch as much user
error as possible before even running an automation flow. This speeds
up development time rapidly without going through all the steps during
automation just to run into a user code error.
@kensoh
Copy link
Member

kensoh commented Aug 4, 2017

Added 2 commits related to this issue. Readme improvement to show how to define variable. And a new commit to simply recognize a = 1 as a valid statement to define variable. So that ; is not needed at the end. Commit comments below with more info, especially the note why where possible TagUI tries to throw error if it is not valid JavaScript code.

#30 - add variable_name step to show how to define a variable manually for use

#30 - set variable without typing ; at the end
; is used to detect that it is a valid JavaScript code
variable assignments are so often used that detecting = should be
acceptable, since that check is done at the very end of checking for
TagUI steps and other JavaScript keywords.

So the following will now work and be more convenient. Update readme
only when making new release and packaged installations.

a = 1
b = ‘b’
c = ”c”

Adding on, reason for JavaScript checking instead of taking anything
that is not TagUI step as JavaScript is to try to catch as much user
error as possible before even running an automation flow. This speeds
up development time rapidly without going through all the steps during
automation just to run into a user code error.

@kensoh kensoh removed their assignment Aug 31, 2017
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