Robot Framework: Continue FOR loop if any keyword fails inside the loop - selenium

In the robot framework, I want to continue For Loop even if any keyword fails inside the Loop. For example, I have the code as shown below:
FOR ${member} IN #{all data members}
Keyword 1 ${member}
Keyword 2 ${member}
..............
Keyword n ${member}
END
If any keyword (e.g. Keyword 2) fails, the FOR loop execution should continue.

You could use keyword Run Keyword And Continue On Failure before each keyword or use keyword Run Keyword And Return Status and manually handle errors.

Related

make SQL query fail on condition

For XYZ reason I need a query to explicitly fail (return error code to connection) if some condition is met (on Snowflake).
Can someone recommend an approach?
Some illustration in pseudo-code:
IF 0= ( SELECT COUNT(*) FROM XYZ) THEN FAIL
I like Simeon's approach, but you may want a custom error message if this is running in a long script. Throwing an error in a JavaScript UDF will allow custom (if untidy) error messages:
create or replace function RAISE_ERROR(MESSAGE string)
returns string
language javascript
as
$$
throw "-->" + MESSAGE + "<--";
$$;
select
case (select count(*) from XYZ)
when 0 then raise_error('My custom error.')
else 'There are rows in the table'
end
;
If there are no rows in XYZ, it will generate an error message that reads:
JavaScript execution error: Uncaught --> My custom error <--. in RAISE_ERROR at '
throw MESSAGE;' position 4 stackstrace: RAISE_ERROR line: 2
It's not the tidiest of error messages, but it will allow you to embed a custom error message if you need help identifying the error. The arrows should help direct people to the real error message thrown in the stack.
SELECT IFF(true, 1::number, (1/0)::number);
then:
IFF(TRUE, 1::NUMBER, (1/0)::NUMBER)
1
where-as
SELECT IFF(false, 1::number, (1/0)::number);
gives:
Division by zero

How to run if if else condition based on element visibility in selenium robot framework?

I am automating a user registration form flow, where successful registration shows a success message and any validation error throws alert text. For this, I am writing an if-else flow based on the visibility or presence of an element on the page. I will pass the control to a specific keyword with that condition
${SuccessBreadcrumb} is the element which is visible when the registration is successful
Code Snippet
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${SuccessBreadcrumb} = xpath=//a[contains(text(),'Success')]
${SuccessMsgLocator} = xpath=//p[contains(text(), 'successfully created')]
${AlertMsg} = xpath=//div[#class='text-danger']
*** Keywords ***
Verify Validation Message
[Arguments] ${UserDetails}
sleep 2s
run keyword if ${SuccessBreadcrumb} is visible Keyword 1
# ... else Keyword 2
Keyword 1
[Arguments] ${UserDetails}
${AccountCreatedText} = get text ${SuccessMsgLocator}
should contain ${AccountCreatedText} ${UserDetails.ValidateMsg} ignore_case=true
# Keyword 2
Error Log
Run Keyword If ${SuccessBreadcrumb}, is visible, VerifySuccessText
Documentation:
Runs the given keyword with the given arguments, if condition is true.
Start / End / Elapsed: 20200213 12:27:52.882 / 20200213 12:27:52.882 / 00:00:00.000
12:27:52.882 FAIL Evaluating expression 'xpath=//a[contains(text(),'Success')]' failed: SyntaxError: invalid syntax (<string>, line 1)
In the documentation for Run Keyword If there does not exist an example with an object. However, using a combination of Run Keyword If with Run Keyword And Return Status will allow you to create a way to handle pass and fail situations within the same test case or keyword.
*** Settings ***
Library SeleniumLibrary
*** Test Cases ***
Check Element Visible
Open Browser
... url=https://www.google.com
... browser=chrome
${passed} Run Keyword And Return Status
... Element Should Be Visible xpath://*[#id="hplogo"]
Run Keyword If ${passed} Keyword Passed
... ELSE Keyword Failed
[Teardown] Close Browser
Check Element Not Visible
Open Browser
... url=https://www.google.com
... browser=chrome
${passed} Run Keyword And Return Status
... Element Should Be Visible xpath://*[#id="xxxx"]
Run Keyword If ${passed} Keyword Passed
... ELSE Keyword Failed
[Teardown] Close Browser
*** Keywords ***
Keyword Passed
Log To Console Passed
Keyword Failed
Log To Console Failed

How to ignore Get Table Text from Cell, if xpath of cell not match

How to ignore Get Table Text from Cell, if xpath of cell not match? Becuase i want my test case still continues testing.
${tableFinal} Set Variable xpath=/html/body/div[2]/div[3]/div/form/table[3]
${totalPayAmount} Get Table Text from Cell ${tableFinal} 1 2
Using either Run Keyword And Continue On Failure or Run Keyword And Ignore Error can help with this. In the documentation the entire family of Run Keyword .... keywords.
The difference between the two is that one just returns the value, whereas the other also provides the status of the Keyword execution.
*** Test Cases ***
Test Case
${CoF_Pass_1} Run Keyword And Continue On Failure KW Pass
${CoF_Fail} Run Keyword And Continue On Failure KW Fail
${CoF_Pass_2} Run Keyword And Continue On Failure KW Pass
${IE_Pass_1} Run Keyword And Ignore Error KW Pass
${IE_Fail} Run Keyword And Ignore Error KW Fail
${IE_Pass_2} Run Keyword And Ignore Error KW Pass
*** Keywords ***
KW Pass
[Return] SomeRandomValue
KW Fail
Fail SomeFaileMessage
This then results into:
Starting test: Test Case
INFO : ${CoF_Pass_1} = SomeRandomValue
FAIL : SomeFaileMessage
INFO : ${CoF_Fail} = None
INFO : ${CoF_Pass_2} = SomeRandomValue
INFO : ${IE_Pass_1} = ('PASS', u'SomeRandomValue')
FAIL : SomeFaileMessage
INFO : ${IE_Fail} = ('FAIL', u'SomeFaileMessage')
INFO : ${IE_Pass_2} = ('PASS', u'SomeRandomValue')
Ending test: Test Case

robot framework: exception handling

Is it possible to handle exceptions from the test case? I have 2 kinds of failure I want to track: a test failed to run, and a test ran but received the wrong output. If I need to raise an exception to fail my test, how can I distinguish between the two failure types? So say I have the following:
*** Test Cases ***
Case 1
Login 1.2.3.4 user pass
Check Log For this log line
If I can't log in, then the Login Keyword would raise an ExecutionError. If the log file doesn't exist, I would also get an ExecutionError. But if the log file does exist and the line isn't in the log, I should get an OutputError.
I may want to immediately fail the test on an ExecutionError, since it means my test did not run and there is some issue that needs to be fixed in the environment or with the test case. But on an OutputError, I may want to continue the test. It may only refer to a single piece of output and the test may be valuable to continue to check the rest of the output.
How can this be done?
Robot has several keywords for dealing with errors, such as Run keyword and ignore error which can be used to run another keyword that might fail. From the documentation:
This keyword returns two values, so that the first is either string
PASS or FAIL, depending on the status of the executed keyword. The
second value is either the return value of the keyword or the received
error message. See Run Keyword And Return Status If you are only
interested in the execution status.
That being said, it might be easier to write a python-based keyword which calls your Login keyword, since it will be easier to deal with multiple exceptions.
You can use something like this
${err_msg}= Run Keyword And Expect Error * <Your keyword>
Should Not Be Empty ${err_msg}
There are couple of different variations you could try like
Run Keyword And Continue On Failure, Run Keyword And Expect Error, Run Keyword And Ignore Error for the first statement above.
Option for the second statement above are Should Be Equal As Strings, Should Contain, Should Match.
You can explore more on Robot keywords

How to make multi-lines test setup or teardown in RobotFramework without creating new keyword?

I need to call two teardown keywords in test case but must not create new keyword for that.
I am interesting if there is a such syntax for keywords as for documentation or loops for example:
[Documentation] line1
... line2
... line3
Use the "Run Keywords" keyword.
From doc "This keyword is mainly useful in setups and teardowns when they need to take care of multiple actions and creating a new higher level user keyword would be an overkill"
Would look like that:
Test Case
[Teardown] Run Keywords Teardown 1 Teardown 2
or also
Test Case
[Teardown] Run Keywords Teardown 1
... Teardown 2
and with arguments
Test Case
[Teardown] Run Keywords Teardown 1 arg1 arg2
... AND Teardown 2 arg1
For executing multiple keywords in Test Teardown method use the following trick:
Firstly, define a new keyword containing the set of keywords you want to execute.
E.g. here Failed Case Handle is a new definition of the other two keywords take screenshot and close application. Consider this is to take a screenshot and then close the running application.
*** Keywords ***
Failed Case Handle
take screenshot
close application
Basically, when you call the Failed Case Handle keyword, take screenshot and close application will be executed respectively.
Then, in the ***Settings*** section define the Test Teardown procedure by the following example.
*** Settings ***
Test Teardown run keyword if test failed Failed Case Handle
or,
*** Settings ***
Test Teardown run keyword Failed Case Handle
So, in the first case Failed Case Handle keyword will be called if any test case fails. On the other-hand in the second case Failed Case Handle keyword will be called after each test cases.