refactoring and bumped version

This commit is contained in:
Corey Goldberg 2013-04-04 09:51:41 -04:00
parent fd5e36103d
commit 7af5552722
3 changed files with 20 additions and 53 deletions

View File

@ -1,3 +1,3 @@
from xvfbwrapper import *
__version__ = '0.1.3'
__version__ = '0.1.4'

View File

@ -1,30 +0,0 @@
#!/usr/bin/env python
from selenium import webdriver
from xvfbwrapper import Xvfb
import unittest
class TestHomepages(unittest.TestCase):
def setUp(self):
self.vdisplay = Xvfb(width=800, height=600)
self.vdisplay.start()
self.browser = webdriver.Firefox()
def testUbuntuHomepage(self):
self.browser.get('http://www.ubuntu.com')
self.assertIn('Ubuntu', self.browser.title)
def testGoogleHomepage(self):
self.browser.get('http://www.google.com')
self.assertIn('Google', self.browser.title)
def tearDown(self):
self.browser.quit()
self.vdisplay.stop()
if __name__ == '__main__':
unittest.main(verbosity=2)

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python
#
# * Corey Goldberg, 2012
# * Corey Goldberg, 2012, 2013
#
# * inspired by:
# PyVirtualDisplay: http://pypi.python.org/pypi/PyVirtualDisplay
@ -16,41 +16,37 @@ import subprocess
import time
class Xvfb(object):
class Xvfb:
def __init__(self, width=800, height=680, colordepth=24):
self.width = width
self.height = height
self.colordepth = colordepth
self.xvfb_proc = None
self.old_display_num = \
os.environ['DISPLAY'].split(':')[1] \
if 'DISPLAY' in os.environ else 0
self.proc = None
if 'DISPLAY' in os.environ:
self.old_display_num = os.environ['DISPLAY'].split(':')[1]
else:
self.old_display_num = 0
def start(self):
self.vdisplay_num = self.search_for_free_display()
self.xvfb_cmd = 'Xvfb :{0} -screen 0 {1}x{2}x{3} 2>&1 >{4}'.format(
self.vdisplay_num,
self.width,
self.height,
self.colordepth,
os.devnull
)
self.xvfb_proc = subprocess.Popen(self.xvfb_cmd,
stdout=open(os.devnull),
stderr=open(os.devnull),
shell=True
)
self.xvfb_cmd = [
'Xvfb', ':%d' % (self.vdisplay_num,), '-screen', '0',
'%dx%dx%d' % (self.width, self.height, self.colordepth)
]
self.proc = subprocess.Popen(self.xvfb_cmd,
stdout=open(os.devnull),
stderr=open(os.devnull))
time.sleep(0.1) # give Xvfb time to start
self._redirect_display(self.vdisplay_num)
def stop(self):
self._redirect_display(self.old_display_num)
if self.xvfb_proc is not None:
self.xvfb_proc.kill()
self.xvfb_proc.wait()
self.xvfb_proc = None
if self.proc is not None:
self.proc.kill()
self.proc.wait()
self.proc = None
def search_for_free_display(self):
ls = [int(x.split('X')[1].split('-')[0]) for x in self._lock_files()]
@ -73,3 +69,4 @@ class Xvfb(object):
def _redirect_display(self, display_num):
os.environ['DISPLAY'] = ':%s' % display_num