Skip to content

Commit

Permalink
#42 Element not found and Element found handling restructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
Nepitwin committed Jan 30, 2022
1 parent e511c40 commit 97c50d2
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 64 deletions.
42 changes: 21 additions & 21 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,36 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
### Changed

- Keyword behaviour changed for Element Should Exist and Element Should Not Exist
- Keywords does not throw a xpath not found exception anymore
- Wait Until Keyword Succeeds combination does not work anymore to check if an ui is displaying after amount of time
- Returns now always True or False
- Keyword throws no exception anymore and error msg attribute is removed
- Keywords contains now a use_exceptions flag to decide if an exception should be called or a return value.
- Exception handling will be used in general to check if a ui element is closing or opened
- If you want to check it by your own use_exception flag should be set to ${FALSE}
- Wait Until Keyword Succeeds combination does not work anymore to check if an ui is displaying after amount of time
- Returns now always True or False

#### Old Syntax
```
# This would throw a flaui exception and stop test exeuction
# This throws a flaui exception and stop test exeuction
Element Should Exist /WRONG/XPATH
# Does not work anymire because Element Should Exist
Wait Until Keyword Succeeds Element Should Exist //VALID_XPATH
```

#### New Syntax
```
# Returns now False or True and can be verified afterwards
${RESULT} Element Should Exist /WRONG/XPATH

# Seperate keyword usage fix the old usage style
Wait Until Keyword Succeeds Element Should Be Visible //VALID_XPATH
- You can now decide to use the old syntax or new style by using the flag parameter use_exceptions.
- This is by default ${TRUE} to avoid this breaks for checkups if a ui element is displaying delayed.

# Or to use Wait Until Element Is Visible
Wait Until Element Is Visible //VALID_XPATH
### Keywords
Element Should Be Visible
[Arguments] ${xpath}
${RESULT} Element Should Exist ${xpath}
Run Keyword If ${RESULT}==${False} Fail Element does not exists
```
# This throws a flaui exception and stop test exeuction
Element Should Exist /WRONG/XPATH
Element Should Not Exist /VALID_XPATH
# Usage with Wait Until Keyword Succeeds
Wait Until Keyword Succeeds Element Should Exist /VALID_XPATH
Wait Until Keyword Succeeds Element Should Not Exist /VALID_XPATH
# If you want to check the return value by your own syntax.
# Wait Until Keyword Succeeds will not work anymore because this keyword will always Return True or False now
${RESULT} Element Should Exist /VALID_XPATH ${FALSE}
${RESULT} Element Should Not Exist /WRONG/XPATH ${FALSE}
```

- Wait Until Element Is Hidden does not check anymore if element exists
Expand Down
9 changes: 5 additions & 4 deletions atests/Element.robot
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ Element Should Exist
Should Be Equal ${EXISTS} ${True}
Element Should Exist Xpath Not Exists
${EXISTS} Element Should Exist ${XPATH_NOT_EXISTS}
${EXISTS} Element Should Exist ${XPATH_NOT_EXISTS} ${FALSE}
Should Be Equal ${EXISTS} ${False}
Element Should Not Exist
${NOT_EXISTS} Element Should Not Exist ${XPATH_ELEMENT}
Should Be Equal ${NOT_EXISTS} ${False}
${EXP_ERR_MSG} Format String ${EXP_ERR_MSG_ELEMENT_EXISTS} ${XPATH_ELEMENT}
${ERR_MSG} Run Keyword And Expect Error * Element Should Not Exist ${XPATH_ELEMENT}
Should Be Equal As Strings ${EXP_ERR_MSG} ${ERR_MSG}
Element Should Not Exist Xpath Not Exists
${NOT_EXISTS} Element Should Not Exist ${XPATH_NOT_EXISTS}
${NOT_EXISTS} Element Should Not Exist ${XPATH_NOT_EXISTS} ${FALSE}
Should Be Equal ${NOT_EXISTS} ${TRUE}
Name Should Be
Expand Down
4 changes: 3 additions & 1 deletion atests/ErrorHandling.robot
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Drag And Drop FirstElement Drag And Drop ${EXP_ERR_MSG_XPATH_NOT_FOUND}
Drag And Drop SecondElement Drag And Drop ${EXP_ERR_MSG_XPATH_NOT_FOUND} /Window ${XPATH_NOT_EXISTS}
Double Click Double Click ${EXP_ERR_MSG_XPATH_NOT_FOUND} ${XPATH_NOT_EXISTS}
Element Should Be Visible Element Should Be Visible ${EXP_ERR_MSG_XPATH_NOT_FOUND} ${XPATH_NOT_EXISTS}
Element Should Exist Element Should Exist ${EXP_ERR_MSG_XPATH_NOT_FOUND} ${XPATH_NOT_EXISTS}
Element Should Not Be Visible Element Should Not Be Visible ${EXP_ERR_MSG_XPATH_NOT_FOUND} ${XPATH_NOT_EXISTS}
Expand TreeItem Expand TreeItem ${EXP_ERR_MSG_XPATH_NOT_FOUND} ${XPATH_NOT_EXISTS} No Such Item
Focus Focus ${EXP_ERR_MSG_XPATH_NOT_FOUND} ${XPATH_NOT_EXISTS}
Expand Down Expand Up @@ -72,6 +73,7 @@ Wait Until Element Is Visible Wait Until Element Is Visible ${EXP_ERR_MSG_E
*** Keywords ***
Execute Keyword And Expect Error Message
[Arguments] ${keyword} ${expected_default_error_msg} @{args}
Log ${args}
${EXP_ERR_MSG} Format String ${expected_default_error_msg} ${XPATH_NOT_EXISTS}
Run Keyword And Expect Error ${EXP_ERR_MSG} Run Keyword ${keyword} @{args}
Run Keyword And Expect Error ${EXP_CUSTOM_ERR_MSG} Run Keyword ${keyword} @{args} ${CUSTOM_ERR_MSG}
Run Keyword And Expect Error ${EXP_CUSTOM_ERR_MSG} Run Keyword ${keyword} @{args} msg=${CUSTOM_ERR_MSG}
18 changes: 4 additions & 14 deletions atests/util/Common.robot
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,27 @@ Close Window Application
[Arguments] ${xpath}=${MAIN_WINDOW}
Run Keyword and Ignore Error Close Application
Run Keyword and Ignore Error Close Window ${xpath}
Wait Until Keyword Succeeds 5x 100ms Window Should Be Closed ${xpath}
Wait Until Keyword Succeeds 20x 200ms Element Should Not Exist ${xpath}
Stop Application
[Arguments] ${xpath}=${MAIN_WINDOW}
Wait Until Keyword Succeeds 5x 1s Close Window Application ${xpath}
Wait Until Keyword Succeeds 20x 200ms Close Window Application ${xpath}
Start Application
[Arguments] ${application}=${TEST_APP} ${xpath}=${MAIN_WINDOW}
${PID} Launch Application ${application}
Should Not Be Equal As Integers ${PID} 0
Wait Until Element Is Visible ${xpath}
Wait Until Keyword Succeeds 20x 200ms Element Should Exist ${xpath}
Focus ${xpath}
Start Application With Args
[Arguments] ${application}=${TEST_APP} ${xpath}=${MAIN_WINDOW} ${arguments}=${EXP_WINDOW_TITLE}
${PID} Launch Application With Args ${application} ${arguments}
Should Not Be Equal As Integers ${PID} 0
Wait Until Keyword Succeeds 5x 1s Window Should Be Exist ${xpath}
Wait Until Keyword Succeeds 20x 200ms Element Should Exist ${xpath}
Focus ${xpath}
Open Complex Tab
${XPATH_TAB} Set Variable ${MAIN_WINDOW}/Tab
${TAB_ITEM_LIST_CONTROLS} Set Variable Complex Controls
Select Tab Item By Name ${XPATH_TAB} ${TAB_ITEM_LIST_CONTROLS}
Window Should Be Exist
[Arguments] ${xpath}
${RESULT} Element Should Exist ${xpath}
Run Keyword If ${RESULT}==${False} Fail Element does not exists
Window Should Be Closed
[Arguments] ${xpath}
${RESULT} Element Should Not Exist ${xpath}
Run Keyword If ${RESULT}==${False} Fail Element exists
47 changes: 35 additions & 12 deletions src/FlaUILibrary/flaui/module/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Container(ValueContainer):
"""
xpath: Optional[str]
name: Optional[str]
use_exception: Optional[bool]
retries: Optional[int]

class Action(Enum):
Expand Down Expand Up @@ -52,7 +53,7 @@ def __init__(self, automation: Any, timeout: int = 1000):
self._timeout = timeout

@staticmethod
def create_value_container(name=None, xpath=None, retries=None, msg=None):
def create_value_container(name=None, xpath=None, retries=None, use_exception=None, msg=None):
"""
Helper to create container object.
Expand All @@ -63,10 +64,12 @@ def create_value_container(name=None, xpath=None, retries=None, msg=None):
name (String): Name from element
xpath (String): Searched element as xpath
retries (Number): Retry counter to repeat calls as number
use_exception (Bool) : Indicator to ignore exception handling by flaui
msg (String): Optional error message
"""
return Element.Container(name=Converter.cast_to_string(name),
xpath=Converter.cast_to_string(xpath),
use_exception=Converter.cast_to_bool(use_exception),
retries=Converter.cast_to_int(retries, msg))

def execute_action(self, action: Action, values: Container):
Expand Down Expand Up @@ -112,11 +115,11 @@ def execute_action(self, action: Action, values: Container):
* Returns : None
* Action.ELEMENT_SHOULD_EXIST
* Values ["xpath"]
* Values ["xpath", "use_exception"]
* Returns : True if element exists otherwise False
* Action.ELEMENT_SHOULD_NOT_EXIST
* Values ["xpath"]
* Values ["xpath", "use_exception"]
* Returns : True if element not exists otherwise False
* Action.WAIT_UNTIL_ELEMENT_IS_HIDDEN
Expand Down Expand Up @@ -145,8 +148,10 @@ def execute_action(self, action: Action, values: Container):
self.Action.IS_ELEMENT_VISIBLE: lambda: self._get_element(values["xpath"]).IsOffscreen,
self.Action.ELEMENT_SHOULD_BE_VISIBLE: lambda: self._element_should_be_visible(values["xpath"]),
self.Action.ELEMENT_SHOULD_NOT_BE_VISIBLE: lambda: self._element_should_not_be_visible(values["xpath"]),
self.Action.ELEMENT_SHOULD_EXIST: lambda: not self._element_should_not_exist(values["xpath"]),
self.Action.ELEMENT_SHOULD_NOT_EXIST: lambda: self._element_should_not_exist(values["xpath"]),
self.Action.ELEMENT_SHOULD_EXIST: lambda: self._element_should_exist(values["xpath"],
values["use_exception"]),
self.Action.ELEMENT_SHOULD_NOT_EXIST: lambda: self._element_should_not_exist(values["xpath"],
values["use_exception"]),
self.Action.WAIT_UNTIL_ELEMENT_IS_HIDDEN: lambda: self._wait_until_element_is_hidden(
values["xpath"], values["retries"]),
self.Action.WAIT_UNTIL_ELEMENT_IS_VISIBLE: lambda: self._wait_until_element_is_visible(
Expand Down Expand Up @@ -232,23 +237,41 @@ def _get_element_by_xpath(self, xpath: str):
Args:
xpath (string): XPath identifier from element.
"""
desktop = self._automation.GetDesktop()
return desktop.FindFirstByXPath(xpath)
return self._automation.GetDesktop().FindFirstByXPath(xpath)

def _element_should_exist(self, xpath: str, use_exception: bool):
"""
Checks if element exists.
def _element_should_not_exist(self, xpath: str):
Args:
xpath (string): XPath identifier from element.
use_exception (bool): Indicator if to throw an FlaUI error
Raises:
FlaUiError: If element could not be found
"""
if not self._get_element_by_xpath(xpath):
if use_exception:
raise FlaUiError(FlaUiError.XPathNotFound.format(xpath))

return False

return True

def _element_should_not_exist(self, xpath: str, use_exception: bool):
"""
Try to get element from xpath.
Args:
xpath (string): XPath identifier from element.
Raises:
FlaUiError: If node could found by xpath.
FlaUiError: If element could be found
"""
desktop = self._automation.GetDesktop()
component = desktop.FindFirstByXPath(xpath)
if self._get_element_by_xpath(xpath):
if use_exception:
raise FlaUiError(FlaUiError.ElementExists.format(xpath))

if component:
return False

return True
Expand Down
45 changes: 33 additions & 12 deletions src/FlaUILibrary/keywords/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,63 @@ def __init__(self, module):
self._module = module

@keyword
def element_should_exist(self, identifier):
def element_should_exist(self, identifier, use_exception=True, msg=None):
"""
Checks if element exists. If element exists True will be returned otherwise False.
If element could not be found by xpath False will be returned.
XPath syntax is explained in `XPath locator`.
Arguments:
| Argument | Type | Description |
| identifier | string | XPath identifier from element |
| Argument | Type | Description |
| identifier | string | XPath identifier from element |
| use_exception | bool | Indicator if an FlaUI exception should be called if element
could not be found by xpath |
| msg | string | Custom error message |
Example:
| ${RESULT} Element Should Exist <XPATH> |
Example for custom result handling:
| ${RESULT} Element Should Exist <XPATH> ${FALSE} |
| Run Keyword If ${RESULT} == ${False} |
Example if element will be shown after a click and takes a few seconds to open:
| Click <XPATH> |
| Wait Until Keyword Succeeds 5x 10ms Element Should Exist <XPATH> |
"""
return self._module.action(Element.Action.ELEMENT_SHOULD_EXIST,
Element.create_value_container(xpath=identifier))
Element.create_value_container(xpath=identifier,
use_exception=use_exception,
msg=msg), msg)

@keyword
def element_should_not_exist(self, identifier):
def element_should_not_exist(self, identifier, use_exception=True, msg=None):
"""
Checks if element exists. If element exists False will be returned otherwise True.
If element could not be found by xpath True will be returned.
XPath syntax is explained in `XPath locator`.
Arguments:
| Argument | Type | Description |
| identifier | string | XPath identifier from element |
| Argument | Type | Description |
| identifier | string | XPath identifier from element |
| use_exception | bool | Indicator if an FlaUI exception should be called if element
could not be found by xpath |
| msg | string | Custom error message |
Example:
| ${RESULT} Element Should Not Exist <XPATH> |
Example for custom result handling:
| ${RESULT} Element Should Not Exist <XPATH> ${FALSE} |
| Run Keyword If ${RESULT} == ${False} |
Example if element will be shown after a click and takes a few seconds to open:
| Click <XPATH> |
| Wait Until Keyword Succeeds 5x 10ms Element Should Not Exist <XPATH> |
"""
return self._module.action(Element.Action.ELEMENT_SHOULD_NOT_EXIST,
Element.create_value_container(xpath=identifier))
Element.create_value_container(xpath=identifier,
use_exception=use_exception,
msg=msg), msg)

@keyword
def focus(self, identifier, msg=None):
Expand Down

0 comments on commit 97c50d2

Please sign in to comment.