20

I have started running Selenium tests via PhantomJS from Python. Whenever a test raises an exception I see a traceback followed by the enigmatic phrase

Screenshot: available via screen

It would be nice to be able to view such screenshots, but I have no idea where they are being saved, nor what program (or other) is intended by screen.

(The only "screen" I am familiar with is the terminal multiplexer, which will not show screenshots)

So - what "screen" are they talking about? How do I use it to view the screenshots?

jalanb
  • 1,097
  • 2
  • 11
  • 37
  • 1
    Possible duplicate of [Automatic screenshots when test fail by Selenium Webdriver in Python](http://stackoverflow.com/questions/12024848/automatic-screenshots-when-test-fail-by-selenium-webdriver-in-python) – Florent B. May 27 '16 at 13:40
  • 2
    The solution to that question is the same, but I think the question is significantly different in that this question is asking about the specific error message. One reason I asked this question was I found it impossible to find any explanation of the error message, all searching just found it as part of an error log. I think this question should be retained for anyone else who finds themselves asking, like I did, "what does that error even mean???" – jalanb May 31 '16 at 13:08

3 Answers3

26

Run the program in a try block and when the error occur take the screenshot using save_screenshot

Eg :

driver = webdriver.PhantomJS()
driver.set_window_size(1920,1080)
try:
    driver.get('http://whatsmyuseragent.com/')

except Exception,e:
    driver.save_screenshot('screenshot.png')

driver.close()

This will give you the screenshot during that moment Image will be saved at the working of your script

Rafael Almeida
  • 5,142
  • 2
  • 20
  • 33
Aby Abraham
  • 354
  • 3
  • 9
  • 1
    Interesting approach with the exception handling. As a side note you can aslo take a screenshot with `driver.get_screenshot_as_file('screenshot.png')` – Rafael Almeida May 27 '16 at 11:11
7

So - what "screen" are they talking about?

My exception looks like this:

  File "/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
WebDriverException: Message: {"errorMessage":"Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: \"script-src 'self' connect.facebook.net cdn.ravenjs.com www.google-analytics.com banhang.shopee.vn chat.shopee.vn cdn.shopee.vn\".\n","request":{"objectName":"","statusCode":200,"headers":{"Cache":"no-cache","Content-Type":"application/json;charset=UTF-8"}}}
Screenshot: available via screen

Take a look at the line: raise exception_class(message, screen, stacktrace), so the screen here means the variable screen:

>>> screen
u'iVBORw0KGgoAAAANSUhEUgAABVYAAAMACAYAAADPPjzCAAAACXBIWXMAAAsTAAALEwEAmpwYAAAgAElE ...'

I don't know a quick way to show the screenshot, but screen looks like the image data that you can save to a file then view it.

Hieu
  • 7,138
  • 2
  • 42
  • 34
5

I found that I can get the actual screenshot returned by the exception using the following (python3).

try:
    ...
except ElementNotVisibleException as e:
    with open("imageToSave.png", "wb") as fh:
        fh.write(base64.decodebytes(e.screen.encode()))

The driver.save_screenshot() function creates a new screenshot at a time later than when the exception occurred.

KuleRucket
  • 93
  • 2
  • 6