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

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

Related

How to run something after every step in Robot Framework

I'd like to check something after every step in a test case in Robot Framework. I am including a dummy example to represent what I am referring for:
*** Test Cases ***
Order From Somewhere
[Tags] whatever tags here
Step1
#Grab Exception
Step2
#Grab Exception
Step3
#Grab Exception
Step4
#Grab Exception
Step5
#Grab Exception
Step6
#Grab Exception
Step7
I assume there is some way to have '#Grab Exception' executed after every Step, but in a nice way.
Thanks for your help in advance.
You can define your own keyword to do that, for example:
*** Test Cases ***
Test Special Keywords
Special Keyword Sleep 5 seconds
Special Keyword Log <b>This is another keyword</b> HTML
*** Keywords ***
Special Keyword
[Arguments] ${kw} #{arguments}
Run Keyword ${kw} #{arguments}
Grab Exception
Grab Exception
Log Called <i>Grab Exception</i> HTML
You can add something like below in your settings area with Test Teardown option:
Run Keyword If Test Failed or Passed <name of the kw>
--- you can use one of it pass or fail OR you can use Unless a condition met.
e.g. as below:
*** Settings ***
Test Setup Open Application App A
Test Teardown Close Application
*** Test Cases ***
Default values
[Documentation] Setup and teardown from setting section
Do Something
Overridden setup
[Documentation] Own setup, teardown from setting section
[Setup] Open Application App B
Do Something
No teardown
[Documentation] Default setup, no teardown at all
Do Something
[Teardown]
No teardown 2
[Documentation] Setup and teardown can be disabled also with special value NONE
Do Something
[Teardown] NONE
Using variables
[Documentation] Setup and teardown specified using variables
[Setup] ${SETUP}
Do Something
[Teardown] ${TEARDOWN}
Refer more at - https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#test-setup-and-teardown

robot-framework error:Message: element not interactable: ElementNotInteractableException

I am a new in python. Trying to delete a row from a table automatically using robot-framework. Table contained 3 visible rows. Here is my code
*** Settings ***
#Test Teardown Close Browser
Library SeleniumLibrary
Library Stringpip
*** Variables ***
${Reps_Menu_Button} xpath=//div[#data-testid='carrier-reps-list']//div[#data-cellheader='Menu']/button
${reps_Delete_Button} xpath=//reach-portal[13]//div[.='Delete']
${Save_Carrier_Button} xpath=//button[.='Save Carrier']
${Success_Toast} xpath=//div[#class='Toastify__toast Toastify__toast--success']
*** Test Cases ***
#Delete Reps :
wait until element is visible ${reps_menu_button}
click element ${reps_menu_button}
click element ${reps_delete_button}
handle alert ACCEPT
click element ${save_carrier_button}
wait until element is not visible ${success_toast}
Please guide me here. Whether I am missing anything? or doing anything wrong? Thanks in advance :)
Check the variables you created. Variable names are case sensitive.

Robotframework - get failing keyword / stack trace of failure

I have a keyword called "debug teardown" which prints the test status and then runs the debuglibrary Debug keyword, if the test has failed.
I would like to be able to log to console which keyword has caused the failure, so I can more effectively debug my test.
Is it possible to get the stack trace or most recent test keyword, and log it to the console?
Here is my Debug Teardown keyword:
Debug Teardown
Run Keyword If Test Failed Log ${TEST STATUS}: ${TEST MESSAGE} ERROR
Run Keyword If Test Failed Debug
You can get a bit more information if you create a listener and also set the log level to DEBUG. Inside the listener you can save the results of log commands, and then when a keyword fails you can print it out or do whatever else you want.
For example, here's a listener that will print to stdout the last log message when a keyword fails:
from __future__ import print_function
class my_listener():
ROBOT_LISTENER_API_VERSION = 2
def __init__(self):
self.ROBOT_LIBRARY_LISTENER = self
self.last_log = None
def _log_message(self, message):
self.last_log = message
def _end_keyword(self, name, attrs):
if attrs['status'] == 'FAIL':
print("\n******\n", self.last_log['message'])
You would use it by importing the listener like a normal library, and also setting the log level to DEBUG (otherwise you'll get the error but no stack trace).
Example:
*** Settings ***
Library my_listener
Suite Setup set log level DEBUG
*** Test cases ***
Example
some keyword
You might be able to utilize set suite variable to update a "global" variable as you go. The last set variable value would be the value that 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

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.