We have a following code in Python
def function1()
...........
raise Exception ..
...............
return 0
Robot script:
${STATUS}= function1
Can anyone please let me know how in Robot script we can catch the return code / exception and branch accordingly?
Run Keyword And Return Status will return a boolean true/false did the enclose keyword succeed.
Run Keyword And Ignore Error returns a tuple of two values - the 1st is the string "PASS" or "FAIL" depending did your keyword succeed or not; the second - the keyword's return value if it passed, or any error messages if not.
Thus surround your keyword with one of these 2 - it really boils down to do you care about the returned value in success or the error in failure - and work with the returned values.
${passed}= Run Keyword And Return Status function1
Run Keyword If ${passed} Action When Passed ELSE Different Action
${rc} ${msg} Run Keyword And Ignore Error function1
Run Keyword If "${rc}" == 'PASS' Log The keyword returned the value: ${msg}
... ELSE Log The keyword failed with the message: ${msg}
Related
when I am running a Tcl script that contains the following lines:
set V [exec bjobs ]
puts "bjobs= ${V}"
When jobs are present it's working properly but, no jobs are running it is showing an error like this:
No unfinished job found
while executing
"exec bjobs "
invoked from within
"set V [exec bjobs ]"
How to avoid this error? Please let me know how to avoid this kind of errors.
It sounds to me like the bjobs program has a non-zero exit code in this case. The exec manual page includes this example in a subsection WORKING WITH NON-ZERO RESULTS:
To execute a program that can return a non-zero result, you should wrap
the call to exec in catch and check the contents of the -errorcode
return option if you have an error:
set status 0
if {[catch {exec grep foo bar.txt} results options]} {
set details [dict get $options -errorcode]
if {[lindex $details 0] eq "CHILDSTATUS"} {
set status [lindex $details 2]
} else {
# Some other error; regenerate it to let caller handle
return -options $options -level 0 $results
}
}
This is more easily written using the try command, as that makes it
simpler to trap specific types of errors. This is done using code like
this:
try {
set results [exec grep foo bar.txt]
set status 0
} trap CHILDSTATUS {results options} {
set status [lindex [dict get $options -errorcode] 2]
}
I think you could write this as:
try {
set V [exec bjobs ]
} trap CHILDSTATUS {message} {
# Not sure how you want to handle the case where there's nothing...
set V $message
}
puts "bjobs= ${V}"
if {[catch {exec bjobs} result]} {
puts "bjobs have some issues. Reason : $result"
} else {
puts "bjobs executed successfully. Result : $result"
}
Reference : catch
Note carefully in the exec man
page:
If any of the commands in the pipeline exit abnormally or are killed or
suspended, then exec will return an error [...]
If any of the commands
writes to its standard error file and that standard error is not
redirected and
-ignorestderr is not specified, then exec will return an
error.
So if bjobs returns non-zero or prints to stderr when there are no jobs, exec needs catch or try as Donal writes.
I am new to this run keyword if method.
I wanted to enter different number based on specific page.
e.g. if page1 element is detected then input number 1, if page2 then input number 2.
*** Settings ***
Library Selenium2Library
Library Collections
Resource ../Resources/nine-res-work.robot
*** Variables ***
${LOGIN-BUTTON-NUMBER-1} ${ANDROID-WIDGET-TEXT-VIEW}\[#resource-id="com.test.abc.work.cac:id/btn_number" and #text="1"]
${LOGIN-BUTTON-NUMBER-2} ${ANDROID-WIDGET-TEXT-VIEW}\[#resource-id="com.test.abc.work.cac:id/btn_number" and #text="2"]
${LOGIN-BUTTON-NUMBER-3} ${ANDROID-WIDGET-TEXT-VIEW}\[#resource-id="com.test.abc.work.cac:id/btn_number" and #text="3"]
${LOGIN-PAGE-HEARDER-page1} ${ANDROID-WIDGET-TEXT-VIEW}\[#resource-id="com.test.abc.work.cac:id/headerText" and #text="Enter your PIN."]
${LOGIN-PAGE-HEARDER-page2} ${ANDROID-WIDGET-TEXT-VIEW}\[#resource-id="com.test.abc.work.cac:id/headerText" and #text="Enter your passcode."]
*** Keywords ***
Smart Card Login
Run Keyword If ${LOGIN-PAGE-HEARDER-page1} == 'PASS' Tap ${LOGIN-BUTTON-NUMBER-1}
Run Keyword If ${LOGIN-PAGE-HEARDER-page2} == 'PASS' Tap ${LOGIN-BUTTON-NUMBER-2}
*** Test Cases ***
Test 1
Launch Application
Smart Card Login
error
Test 1 | FAIL |
Evaluating expression '//android.widget.TextView[#resource-id="com.test.abc.work.cac:id/headerText" and #text="Enter your PIN."] == 'PASS'' failed: SyntaxError: invalid syntax (<string>, line 1)
I have tried another way, this time no error but the tap action isn't execute.
*** Variables ***
${LOGIN-BUTTON-NUMBER-1} ${ANDROID-WIDGET-TEXT-VIEW}\[#resource-id="com.test.abc.work.cac:id/btn_number" and #text="1"]
${LOGIN-BUTTON-NUMBER-2} ${ANDROID-WIDGET-TEXT-VIEW}\[#resource-id="com.test.abc.work.cac:id/btn_number" and #text="2"]
${LOGIN-BUTTON-NUMBER-3} ${ANDROID-WIDGET-TEXT-VIEW}\[#resource-id="com.test.abc.work.cac:id/btn_number" and #text="3"]
${LOGIN-PAGE-HEARDER-page1} ${ANDROID-WIDGET-TEXT-VIEW}\[#text="Enter your PIN."]
${LOGIN-PAGE-HEARDER-page2} ${ANDROID-WIDGET-TEXT-VIEW}\[#text="Enter your passcode."]
*** Keywords ***
Input App Passcode
Tap ${LOGIN-BUTTON-NUMBER-2}
*** Test Cases ***
Launch App
Open Nine Folders Application
Sleep 5s
Input Password
${Page1} = Page Should Contain Element ${LOGIN-PAGE-HEARDER-page1}
Run Keyword If '${Page1}' == 'PASS' Input App Passcode
You got syntax error because Run Keyword If expects a valid Python condition as first argument. This is not the case in your code. In your case this is what happens, assuming ${ANDROID-WIDGET-TEXT-VIEW} is just view in this example:
Run Keyword If ${LOGIN-PAGE-HEARDER-page1} == 'PASS' Tap ${LOGIN-BUTTON-NUMBER-1}
which is
Run Keyword If view\[#resource-id="com.test.abc.work.cac:id/headerText" and #text="Enter your PIN."] == 'PASS' Tap ${LOGIN-BUTTON-NUMBER-1}
this is equivalent with the following Python code:
if view\[#resource-id="com.test.abc.work.cac:id/headerText" and #text="Enter your PIN."] == 'PASS':
call_tap_function(LOGIN_BUTTON_NUMBER_1)
There are a bunch of invalid characters there because the string is not enclosed in '. So correctly it should be:
Run Keyword If '${LOGIN-PAGE-HEARDER-page1}' == 'PASS' Tap ${LOGIN-BUTTON-NUMBER-1}
which will translate to:
if 'view\[#resource-id="com.test.abc.work.cac:id/headerText" and #text="Enter your PIN."]' == 'PASS':
call_tap_function(LOGIN_BUTTON_NUMBER_1)
Note that this will never be equal with PASS.
As for your second approach, Page Should Contain Element does not have a return value, it will fail or the execution continues as usual. To achieve what you want you should use the Run Keyword And Return Status that will return if the keyword called has passed or failed.
Input Password
${Page1} = Run Keyword And Return Status Page Should Contain Element ${LOGIN-PAGE-HEARDER-page1}
Run Keyword If ${Page1} Input App Passcode
Here ${Page1} variable will be true if Page Should Contain Element passed, aka if login page header page1 was on the page.
i too have same problem.
please provide a clear solution for me...
following error is obtained when i run blackhole.tcl file.
ns: _o108 blackhole:
(_o108 cmd line 1)
invoked from within
"_o108 cmd blackhole"
invoked from within
"catch "$self cmd $args" ret"
invoked from within
"if [catch "$self cmd $args" ret] {
set cls [$self info class]
global errorInfo
set savedInfo $errorInfo
error "error when calling class $cls: $args" $..."
(procedure "_o108" line 2)
(SplitObject unknown line 2)
invoked from within
"_o108 blackhole"
Valid examples for the "2005-2013 blackholeaodv code" are : blackholeaodv2005-examples-3-Aug2015.tar.gz
Link : https://drive.google.com/file/d/0B7S255p3kFXNUmJGd0ZyNUhvVFU/view?usp=sharing
When invoking a custom command, I noticed that only the logs are displayed. For example, if my Custom Comand script contains a retrun statement return "great custom command", I can't find it in the result. Both in API Java client or shell execution cases.
What can I do to be able to retrieve that result at the end of an execution?
Thanks.
Command definition in service description file:
customCommands ([
"getText" : "getText.groovy"
])
getText.groovy file content:
def text = "great custom command"
println "trying to get a text"
return text
Assuming that you service file contains the following :
customCommands ([
"printA" : {
println "111111"
return "222222"
},
"printB" : "fileB.groovy"
])
And fileB.groovy contains the following code :
println "AAAAAA"
return "BBBBBB"
Then if you run the following command : invoke yourService printA
You will get this :
Invocation results:
1: OK from instance #1..., Result: 222222
invocation completed successfully.
and if you run the following command : invoke yourService printB
You will get this :
Invocation results:
1: OK from instance #1..., Result: AAAAAA
invocation completed successfully.
So if your custom command's implementation is a Groovy closure, then its result is its return value.
And if your custom command's implementation is an external Groovy file, then its result is its last statement output.
HTH,
Tamir.
I'm trying to abort a task in ada program but I get this error during compilation:
expect task name or task interface class-wide object for "abort"
The code looks like this:
task type Sending_Message;
type Send_Message is access Sending_Message;
declare
send : Send_Message;
begin
send := new Sending_Message;
...
abort send; -- this line throws error
end;
And again when I try line like this:
abort Sending_Message;
I get error:
invalid use of subtype mark in expression or call
Any idea what is wrong?
You have to explicitly dereference the access type:
abort send.all;