Skip to content

Instantly share code, notes, and snippets.

@lucaswiman
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucaswiman/9788422 to your computer and use it in GitHub Desktop.
Save lucaswiman/9788422 to your computer and use it in GitHub Desktop.
nose==1.1.2
selenium==2.35.0
wsgiref==0.1.2
import unittest
import logging
from selenium.webdriver import phantomjs
import sys
class SeleniumTestCase(unittest.TestCase):
def setUp(self):
self.selenium = phantomjs.webdriver.WebDriver()
def tearDown(self):
self.selenium.quit()
def test_that_something_fails(self):
self.selenium.get('https://www.google.com')
print repr(self.selenium.get_log('browser'))
print repr(self.selenium.get_log('har'))
raise AssertionError()
class QuitEarlySeleniumTestCase(unittest.TestCase):
def setUp(self):
self.selenium = phantomjs.webdriver.WebDriver()
def test_that_something_fails(self):
self.selenium.get('https://www.google.com')
s1 = repr(self.selenium.get_log('browser'))
s2 = repr(self.selenium.get_log('har'))
self.selenium.quit()
print s1
print s2
raise AssertionError()
import fcntl
import unittest
import logging
from selenium.webdriver import phantomjs
import sys
import os
def _make_fd_blocking(file_obj):
"""
Updates the flags of the file descriptor to make it blocking.
file_obj is a `file` object, which has a `.fileno()` method.
See http://trac.edgewall.org/ticket/2066#comment:1
"""
fd = file_obj.fileno()
flags = fcntl.fcntl(fd, fcntl.F_GETFL)
if flags & os.O_NONBLOCK:
fcntl.fcntl(fd, fcntl.F_SETFL, flags & ~os.O_NONBLOCK)
def make_stdio_blocking():
"""
Makes stdout and stderr blocking.
This prevents resource contention issues with Selenium subprocesses.
See https://github.com/cobrateam/splinter/issues/257
"""
# Use __stderr__ attribute since sys.stderr can be patched by the test
# runner.
_make_fd_blocking(sys.__stderr__)
_make_fd_blocking(sys.__stdout__)
class SeleniumTestCase(unittest.TestCase):
def setUp(self):
self.selenium = phantomjs.webdriver.WebDriver()
make_stdio_blocking()
def tearDown(self):
self.selenium.quit()
def test_that_something_fails(self):
self.selenium.get('https://www.google.com')
print repr(self.selenium.get_log('browser'))
print repr(self.selenium.get_log('har'))
raise AssertionError()
class QuitEarlySeleniumTestCase(unittest.TestCase):
def setUp(self):
self.selenium = phantomjs.webdriver.WebDriver()
make_stdio_blocking()
def test_that_something_fails(self):
self.selenium.get('https://www.google.com')
s1 = repr(self.selenium.get_log('browser'))
s2 = repr(self.selenium.get_log('har'))
self.selenium.quit()
print s1
print s2
raise AssertionError()
@lucaswiman
Copy link
Author

Breaks with the following:

(break_selenium)~/break_selenium/break_selenium $ python -m unittest selenium_failure
[]
[{u'timestamp': 1395857161941, u'message': u'{"log":{"version":"1.2","creator":{"name":"PhantomJS","version":"1.9.7"},"pages":[{"startedDateTime":"2014-03-26T18:06:01.606Z","id":"https://www.google.com/","title":"Google","pageTimings":{"onLoad":270}}],"entries":[{"startedDateTime":"2014-03-26T18:06:01.604Z","time":126,"request":{"method":"GET","url":"https://www.google.com/","httpVersion":"HTTP/1.1","cookies":[],"headers":[{"name":"User-Agent","value":"Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.7 Safari/534.34"},{"name":"Accept","value":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"}],"queryString":[],"headersSize":-1,"bodySize":-1},"response":{"status":200,"statusText":"OK","httpVersion":"HTTP/1.1","cookies":[],"headers":[{"name":"Date","value":"Wed, 26 Mar 2014 18:06:00 GMT"},{"name":"Expires","value":"-1"},{"name":"Cache-Control","value":"private, max-age=0"},{"name":"Content-Type","value":"text/html; charset=UTF-8"},{"name":"Set-CookE[]
[{u'timestamp': 1395857164391, u'message': u'{"log":{"version":"1.2","creator":{"name":"PhantomJS","version":"1.9.7"},"pages":[{"startedDateTime":"2014-03-26T18:06:03.973Z","id":"https://www.google.com/","title":"Google","pageTimings":{"onLoad":316}}],"entries":[{"startedDateTime":"2014-03-26T18:06:03.971Z","time":165,"request":{"method":"GET","url":"https://www.google.com/","httpVersion":"HTTP/1.1","cookies":[],"headers":[{"name":"User-Agent","value":"Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.7 Safari/534.34"},{"name":"Accept","value":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"}],"queryString":[],"headersSize":-1,"bodySize":-1},"response":{"status":200,"statusText":"OK","httpVersion":"HTTP/1.1","cookies":[],"headers":[{"name":"Date","value":"Wed, 26 Mar 2014 18:06:03 GMT"},{"name":"Expires","value":"-1"},{"name":"Cache-Control","value":"private, max-age=0"},{"name":"Content-Type","value":"text/html; charset=UTF-8"},{"name":"Set-Cookie",E
======================================================================
ERROR: test_that_something_fails (selenium_failure.QuitEarlySeleniumTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "selenium_failure.py", line 29, in test_that_something_fails
    print s2
IOError: [Errno 35] Resource temporarily unavailable

======================================================================
ERROR: test_that_something_fails (selenium_failure.SeleniumTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "selenium_failure.py", line 16, in test_that_something_fails
    print repr(self.selenium.get_log('har'))
IOError: [Errno 35] Resource temporarily unavailable

----------------------------------------------------------------------
Ran 2 tests in 4.838s

It also breaks using nosetests.

(break_selenium)~/break_selenium/break_selenium $ nosetests selenium_failure:SeleniumTestCase
F
======================================================================
FAIL: test_that_something_fails (selenium_failure.SeleniumTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/lucaswiman/break_selenium/break_selenium/selenium_failure.py", line 18, in test_that_something_fails
    raise AssertionError()
AssertionError: 
-------------------- >> begin captured stdout << ---------------------
[]
[{u'timestamp': 1395856560262, u'message': u'{"log":{"version":"1.2","creator":{"name":"PhantomJS","version":"1.9.7"},"pages":[{"startedDateTime":"2014-03-26T17:55:59.839Z","id":"https://www.google.com/","title":"Google","pageTimings":{"onLoad":328}}],"entries":[{"startedDateTime":"2014-03-26T17:55:59.837Z","time":156,"request":{"method":"GET","url":"https://www.google.com/","httpVersion":"HTTP/1.1","cookies":[],"headers":[{"name":"User-Agent","value":"Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.7 Safari/534.34"},{"name":"Accept","value":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"}],"queryString":[],"headersSize":-1,"bodySize":-1},"response":{"status":200,"statusText"    **extra_args)
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.py", line 95, in __init__
    self.runTests()
  File "/Users/lucaswiman/break_selenium/lib/python2.7/site-packages/nose/core.py", line 197, in runTests
    result = self.testRunner.run(self.test)
  File "/Users/lucaswiman/break_selenium/lib/python2.7/site-packages/nose/core.py", line 63, in run
    result.printErrors()
  File "/Users/lucaswiman/break_selenium/lib/python2.7/site-packages/nose/result.py", line 103, in printErrors
    _TextTestResult.printErrors(self)
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/runner.py", line 109, in printErrors
    self.printErrorList('FAIL', self.failures)
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/runner.py", line 116, in printErrorList
    self.stream.writeln("%s" % err)
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/runner.py", line 24, in writeln
    self.write(arg)
IOError: [Errno 35] Resource temporarily unavailable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment