From 73f2d001384e36e31881b1a31eff7b9a5c47c5ab Mon Sep 17 00:00:00 2001 From: JacekZ <119878576+jacekziembla@users.noreply.github.com> Date: Fri, 9 Feb 2024 20:39:05 +0100 Subject: [PATCH] FIX: Fix error message thrown by selenium exceptions upon wait Currently we were casting entire exception to the string (including not readable stacktrace) from selenium to error message. Instead, we should pass just the error message --- selene/core/wait.py | 4 +--- tests/integration/browser__actions_test.py | 2 +- tests/integration/element__type_test.py | 2 +- tests/integration/error_messages_test.py | 20 ++++++++++++-------- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/selene/core/wait.py b/selene/core/wait.py index 20d9d99ed..5fc53eeae 100644 --- a/selene/core/wait.py +++ b/selene/core/wait.py @@ -119,11 +119,9 @@ def logic(fn: Callable[[E], R]) -> R: return fn(self.entity) except Exception as reason: if time.time() > finish_time: - reason_message = str(reason) - reason_string = '{name}: {message}'.format( name=reason.__class__.__name__, - message=reason_message, + message=getattr(reason, "msg", str(reason)), ) # TODO: think on how can we improve logging failures in selene, e.g. reverse msg and stacktrace # stacktrace = getattr(reason, 'stacktrace', None) diff --git a/tests/integration/browser__actions_test.py b/tests/integration/browser__actions_test.py index 4cb2f2af1..1643c9285 100644 --- a/tests/integration/browser__actions_test.py +++ b/tests/integration/browser__actions_test.py @@ -148,7 +148,7 @@ def test_browser_actions_fails_to_wait_for_drag_and_drop_before_perform( assert ( "browser.element(('css selector', '#draggable')).locate webelement\n" "\n" - "Reason: NoSuchElementException: Message: " + "Reason: NoSuchElementException: " "no such element: Unable to locate element: " "{\"method\":\"css selector\",\"selector\":\"#draggable\"}\n" ) in str(error) diff --git a/tests/integration/element__type_test.py b/tests/integration/element__type_test.py index c685f9699..9f71be8f5 100644 --- a/tests/integration/element__type_test.py +++ b/tests/integration/element__type_test.py @@ -161,7 +161,7 @@ def test_type_failure_when_invisible(session_browser): assert time_spent >= 1 browser.element('#text-field').should(have.value('before')) assert ( - 'Reason: JavascriptException: Message: javascript error: ' + 'Reason: JavascriptException: javascript error: ' 'element ' ' ' 'is not visible\n' diff --git a/tests/integration/error_messages_test.py b/tests/integration/error_messages_test.py index 46b991095..750d64ee0 100644 --- a/tests/integration/error_messages_test.py +++ b/tests/integration/error_messages_test.py @@ -92,9 +92,10 @@ def test_element_search_fails_with_message_when_implicitly_waits_for_condition( 'Timed out after 0.1s, while waiting for:', "browser.element(('css selector', '#hidden-button')).click", '', - 'Reason: ElementNotInteractableException: Message: element not interactable', + 'Reason: ElementNotInteractableException: element not interactable', '(Session info: *)', - 'Stacktrace: *', + 'Screenshot: *.png', + 'PageSource: *.html', ] @@ -119,9 +120,10 @@ def test_inner_element_search_fails_with_message_when_implicitly_waits_for_condi "browser.element(('css selector', '#container')).element(('css selector', " "'#hidden-button')).click", '', - 'Reason: ElementNotInteractableException: Message: element not interactable', + 'Reason: ElementNotInteractableException: element not interactable', '(Session info: *)', - 'Stacktrace: *', + 'Screenshot: *.png', + 'PageSource: *.html', ] @@ -146,9 +148,10 @@ def test_inner_element_search_fails_with_message_when_implicitly_waits_for_condi "browser.element(('css selector', '#hidden-container')).element(('css " "selector', '#button')).click", '', - 'Reason: ElementNotInteractableException: Message: element not interactable', + 'Reason: ElementNotInteractableException: element not interactable', '(Session info: *)', - 'Stacktrace: *', + 'Screenshot: *.png', + 'PageSource: *.html', ] @@ -173,11 +176,12 @@ def test_inner_element_search_fails_with_message_when_implicitly_waits_for_condi "browser.element(('css selector', '#not-existing')).element(('css selector', " "'#button')).click", '', - 'Reason: NoSuchElementException: Message: no such element: Unable to locate ' + 'Reason: NoSuchElementException: no such element: Unable to locate ' 'element: {"method":"css selector","selector":"#not-existing"}', '(Session info: *); For documentation on this error, please visit: ' 'https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception', - 'Stacktrace: *', + 'Screenshot: *.png', + 'PageSource: *.html', ]