I use latest Robot Framework.
I need to assign a value to my variable depending on value of an argument. That's how it would be in JavaScript:
ITEM_SELECTOR = RECENT_ITEM_SELECTOR + (
position === 'last' ? ':last-child' : ':nth-child' + '(' + position + ')'
)
This is how I try to write it in Robot Framework:
${ITEM_SELECTOR} = Run Keyword If ${position} == 'last' ${RECENT_ITEM_SELECTOR}:last-child
... ELSE ${RECENT_ITEM_SELECTOR}:nth-child(${position})
but this way ${RECENT_ITEM_SELECTOR}:nth-child(${position}) is considered a keyword, not assigned to ITEM_SELECTOR.
Then I try to preprend it with No Operation, but then my return value is considered its argument and I get Keyword 'BuiltIn.No Operation' expected 0 arguments, got 1.
How can I write it?
Since you are calling run keyword if, you have to give it a keyword to run. You can use set variable to make your code work:
${ITEM_SELECTOR} = Run Keyword If ${position} == 'last'
... Set variable ${RECENT_ITEM_SELECTOR}:last-child
... ELSE
... Set variable ${RECENT_ITEM_SELECTOR}:nth-child(${position})
However, you can also use set variable if for a slightly more compact and readable solution:
${ITEM_SELECTOR} = Set variable if ${position} == 'last'
... ${RECENT_ITEM_SELECTOR}:last-child
... ${RECENT_ITEM_SELECTOR}:nth-child(${position})
Related
Here is the pseudo code which I would like to write using Robot Framework. If it cannot be done using the framework is there any alternative:
${balMethodID}= Set Variable If ${balMethodID} == None ${newBalMethodID}
Basically if the value of variable is None then I want to assign a new value. The value of the variable is becoming None when its initial value is not None.
Set Value If can be given two values; the first will be used if the condition is true, the second is if the condition is false. If you want to keep the original value if the condition is false, use the original value as the last argument:
${balMethodID}= Set Variable If ${balMethodID} == None
... # value if true # value if false
... ${newBalMethodID} ${balMethodID}
I have a scenario where I am trying to access a separate element in my custom helper from within a nested for loop. When I use root outside my for loop I don't have any issues, but I can't seem to use #root within my custom helper. I thought maybe ../ would work, but it appears that is only be moving up to the parent element, not a one that is separate
Here are my two objects:
category //Object being looped through
categoryQuery //Query object being compared to looped values
Here is my view (looping through ID's and then apply selected to the ID attached to categoryQuery:
{{#category}}
<option value="{{this.categoryId}}"{{selected this.categoryId #root.categoryQuery}}>{{this.categoryName}}</option>
{{/category}}
Preselected value if the values match:
/Preselect option value that is associated with edited record
hbs.registerHelper('selected', function(option, value){
if (option === value) {
return 'selected';
} else {
return '';
}
});
Updated:
when adding console.log('Option : ' + option + ' Value : ' + value); into the else statement of my registered helper, I receive the following, which shows that it isn't an issue that #root.category isn't pulling in the value, but it isn't equating correctly.
For example:
Option : 1 Value : 2
Option : 2 Value : 2
Option : 1 Value : undefined
I determined that the root cause of the issue was the strictness in the comparison operator. When changed to == I was able to correctly identify the ID's that matched
I am trying to create a function which will look at two vectors of character labels, and print the appropriate label based on an If statement. I am running into an issue when one of the vectors is populated by NA.
I'll truncate my function:
eventTypepriority=function(a,b) {
if(is.na(a)) {print(b)}
if(is.na(b)) {print(a)}
if(a=="BW"& b=="BW",) {print("BW")}
if(a=="?BW"& b=="BW") {print("?BW")}
...#and so on
}
Some data:
a=c("Pm", "BW", "?BW")
b=c("PmDP","?BW",NA)
c=mapply(eventTypepriority, a,b, USE.NAMES = TRUE)
The function works fine for the first two, selecting the label I've designated in my if statements. However, when it gets to the third pair I receive this error:
Error in if (a == "?BW" & b == "BW") { :
missing value where TRUE/FALSE needed
I'm guessing this is because at that place, b=NA, and this is the first if statement, outside of the 'is.na' statements, that need it to ignore missing values.
Is there a way to handle this? I'd really rather not add conditional statements for every label and NA. I've also tried:
-is.null (same error message)
-Regular Expressions:
if(a==grepl([:print:]) & b==NA) {print(a)}
In various formats, including if(a==grepl(:print:)... No avail. I receive an 'Error: unexpected '[' or whatever character R didn't like first to tell me this is wrong.
All comments and thoughts would be appreciated. ^_^
if all your if conditions are exclusives, just call return() to avoid checking other conditions when one is met:
eventTypepriority=function(a,b) {
if(is.na(a)) {print(b);return()}
if(is.na(b)) {print(a);return()}
if(a=="BW"& b=="BW",) {print("BW");return()}
if(a=="?BW"& b=="BW") {print("?BW");return()}
...#and so on
}
You need to use if .. else statements instead of simply if; otherwise, your function will evaluate the 3rd and 4th lines even when one of the values is n/a.
Given you mapply statement, I also assume you want the function to output the corresponding label, not just print it?
In that case
eventTypepriority<-function(a,b) {
if(is.na(a)) b
else if(is.na(b)) a
else if(a=="BW"& b=="BW") "BW"
else if(a=="?BW"& b=="BW") "?BW"
else "..."
}
a=c("Pm", "BW", "?BW")
b=c("PmDP","?BW",NA)
c=mapply(eventTypepriority, a,b, USE.NAMES = T)
c
returns
Pm BW ?BW
"..." "..." "?BW"
If you actually want to just print the label and have your function return something else, you should be able to figure it out from here.
I am learning from the project angular2-rxjs-chat application ong github. In the code here there is a line of code given below:
threads[message.thread.id] = threads[message.thread.id] ||
message.thread;
where threads has earlier been defined on line 29 in the code as shown below:
let threads: {[key: string]: Thread} = {};
The comments in the code states that "store the message's thread in our acuuculator 'threads'. I need a little bit explanation of how does the assignment works on line 31 as on both sides of the assignment operator we have the same thing i.e., threads[message.thread.id]. If the statement on line 31 was like
(threads[message.thread.id] = message.thread;)
then I would explain it as a value is being assigned to a key in the map "threads". But I don't understand the full line.
This means if threads[message.thread.id] already has a value then keep it, otherwise set the value to meassage.thread.
If the part before || evaluates to a value that is truthy (not null, undefined, false, ...)then the part after||is not evaluated and the result from the part before||is returned otherwise the result from the expression after||` is returned.
You could also write it as
if(!threads[message.thread.id]) {
threads[message.thread.id] = message.thread;
}
How can you test if a variable is empty or not defined in a qmake .pro file? I want to
be able to set up a default value if the variable is not defined.
I tried
eval("VARIABLE" = ""){
VARIABLE = test
}
eval("VARIABLE" = ""){
message(variable is empty)
}
but I still get the message "variable is empty".
there is already the function isEmpty I didn't spot:
isEmpty(VARIABLE){
VARIABLE = test
}
isEmpty(VARIABLE ){
message(variable is empty)
}
I don't understand why eval didnt work thought...
Like your own answer says, isEmpty(VARIABLE) does what you want:
isEmpty(VARIABLE) {
...
}
The qmake language has no equivalent of an equals operator (==), but you can compare things like this:
equals(VARIABLE, foo) {
...
}
You can also check if a variable contains a substring, using a regular expression:
contains(VARIABLE, .*foo.*) {
...
}
The reason why eval() didn't work, is that it executes the statement within it and returns true if the statement succeeded.
So by doing this:
eval(VARIABLE = "") {
...
}
...you are actually assigning "" to VARIABLE, making the variable empty and entering the block.
More about test functions: http://qt-project.org/doc/qt-5/qmake-test-function-reference.html