yashaka / selene

User-oriented Web UI browser tests in Python
https://yashaka.github.io/selene/
MIT License
676 stars 145 forks source link

[#513] FIX: Fix error message thrown by selenium exceptions upon wait #512

Closed jacekziembla closed 4 months ago

jacekziembla commented 4 months ago

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.

Before:

.venv/lib/python3.8/site-packages/selene/core/wait.py:143: in for_
     return self._decorator(self)(logic)(fn)
 .venv/lib/python3.8/site-packages/selene/core/wait.py:141: in logic
     raise self._hook_failure(failure)
 E   selene.core.exceptions.TimeoutException: Message: 
 E   
 E   Timed out after 1s, while waiting for:
 E   browser.element(('id', 'UserName')).is present in DOM
 E   
 E   Reason: NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="UserName"]"}
 E     (Session info: chrome=121.0.6167.160); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
 E   Stacktrace:
 E   0   chromedriver                        0x00000001009c0168 chromedriver + 4673896
 E   1   chromedriver                        0x00000001009b79c3 chromedriver + 4639171
 E   2   chromedriver                        0x00000001005abfdd chromedriver + 397277
 E   3   chromedriver                        0x00000001005f7bfc chromedriver + 707580
 E   4   chromedriver                        0x00000001005f7dd1 chromedriver + 708049
 E   5   chromedriver                        0x000000010063c284 chromedriver + 987780
 E   6   chromedriver                        0x000000010061a8ed chromedriver + 850157
 E   7   chromedriver                        0x0000000100639796 chromedriver + 976790
 E   8   chromedriver                        0x000000010061a663 chromedriver + 849507
 E   9   chromedriver                        0x00000001005ea1cf chromedriver + 651727
 E   10  chromedriver                        0x00000001005eb1ae chromedriver + 655790
 E   11  chromedriver                        0x0000000100980380 chromedriver + 4412288
 E   12  chromedriver                        0x0000000100985798 chromedriver + 4433816
 E   13  chromedriver                        0x0000000100964d71 chromedriver + 4300145
 E   14  chromedriver                        0x00000001009864e6 chromedriver + 4437222
 E   15  chromedriver                        0x0000000100956d3c chromedriver + 4242748
 E   16  chromedriver                        0x00000001009a6208 chromedriver + 4567560
 E   17  chromedriver                        0x00000001009a63be chromedriver + 4567998
 E   18  chromedriver                        0x00000001009b7603 chromedriver + 4638211
 E   19  libsystem_pthread.dylib             0x00007ff805e53202 _pthread_start + 99
 E   20  libsystem_pthread.dylib             0x00007ff805e4ebab thread_start + 15

After:

.venv/lib/python3.8/site-packages/selene/core/wait.py:141: in for_
     return self._decorator(self)(logic)(fn)
 .venv/lib/python3.8/site-packages/selene/core/wait.py:139: in logic
     raise self._hook_failure(failure)
 E selene.core.exceptions.TimeoutException: Message: 
 E   
 E Timed out after 1s, while waiting for:
 E browser.element(('id', 'UserName')).is present in DOM
 E   
 E Reason: NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"[id="UserName"]"}
 E (Session info: chrome=121.0.6167.160); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
jacekziembla commented 4 months ago

@yashaka apologizes that I did not create issue first, should be linked now. I edited it via GitHub UI without cloning and did not run mypy check as well - although the change is really tiny (I've checked if it's working by editing directly source files in my venv)

jacekziembla commented 4 months ago

based on the linters and tests, I guess it should be smth like

message=getattr(reason, "msg", None)
jacekziembla commented 4 months ago

I have run black locally, this check should be passing now. There are ~5 tests that needs to be fixed, as the reason string changed and we do not have 'Stacktrace: *', now, but I was unable to kick these tests locally without browserstack key

jacekziembla commented 4 months ago

I have run black locally, this check should be passing now. There are ~5 tests that needs to be fixed, as the reason string changed and we do not have 'Stacktrace: *', now, but I was unable to kick these tests locally without browserstack key

Modified error_messages tests, also in case there is no original error message we fallback to previous behavior of casting the error to string

yashaka commented 4 months ago

Modified error_messages tests, also in case there is no original error message we fallback to previous behavior of casting the error to string

yes, this was something that I felt was absent... great that you found it!

jacekziembla commented 4 months ago

Great, thanks! Waiting for next release 🤞

yashaka commented 4 months ago

https://github.com/yashaka/selene/releases/tag/2.0.0rc8 ;)