I would like to have selenium output a much simpler and straight forward output for my selenium tests that I am currently having saved to an output log
======================================================================
ERROR: test3_AnswerTest (__main__.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test.py", line 98, in test3_FashionTest
driver.find_element_by_xpath("//a[contains(.,'Answer')]").click()
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 230, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 662, in find_element
{'using': by, 'value': value})['value']
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 173, in execute
self.error_handler.check_response(response)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 166, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: Error Message => 'Unable to find element with xpath '//a[contains(.,'Ass')]''
caused by Request => {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"106","Content-Type":"application/json;charset=UTF-8","Host":"10.0.0.100:49044","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"xpath\", \"sessionId\": \"fd4e8db0-a0c6-11e4-9624-f33660aa508e\", \"value\": \"//a[contains(.,'Ass')]\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/fd4e8db0-a0c6-11e4-9624-f33660aa508e/element"}
Screenshot: available via screen
Is it possible to simplify this output into something like this?
NoSuchElementException: Message: Error Message => 'Unable to find element with xpath '//a[contains(.,'Answer')]''
Current error message is
Traceback (most recent call last):
NoSuchElementException: Message: Error Message => 'Unable to find element with xpath '//a[contains(.,'Answer')]''
caused by Request => {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"106","Content-Type":"application/json;charset=UTF-8","Host":"10.0.0.100:49044","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"xpath\", \"sessionId\": \"12e538b0-a0e3-11e4-af45-ff6a851f3fb0\", \"value\": \"//a[contains(.,'Ass')]\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/12e538b0-a0e3-11e4-af45-ff6a851f3fb0/element"}
Screenshot: available via screen
There was an error thrown during the tests and this is an error traceback.
What you can do is:
sys.tracebacklimit = 0When this variable is set to an integer value, it determines the maximum number of levels of traceback information printed when an unhandled exception occurs. The default is 1000. When set to 0 or less, all traceback information is suppressed and only the exception type and value are printed.
tracebacklimit doesn't affect it - you need to do it manually like this)Working example:
import unittest
import sys
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.remote.errorhandler import ErrorHandler
class MyHandler(ErrorHandler):
def check_response(self, response):
try:
super(MyHandler, self).check_response(response)
except NoSuchElementException as e:
e.stacktrace = None
# PhantomJS specific line:
e.msg = json.loads(e.msg)['errorMessage']
raise
class MyTestCase(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.error_handler = MyHandler()
def test(self):
self.driver.get("http://google.com")
self.driver.find_element_by_id('illegal')
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
sys.tracebacklimit = 0
unittest.main()
And here is what is on the console:
$ python test.py
E
======================================================================
ERROR: test (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
NoSuchElementException: Message: u'Unable to locate element: {"method":"id","selector":"illegal"}'
----------------------------------------------------------------------
Ran 1 test in 5.401s
FAILED (errors=1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With