From 7af55527225ea734f14393b6cdf5785e4e6c1366 Mon Sep 17 00:00:00 2001 From: Corey Goldberg Date: Thu, 4 Apr 2013 09:51:41 -0400 Subject: [PATCH] refactoring and bumped version --- xvfbwrapper/__init__.py | 2 +- xvfbwrapper/test_xvfb_selenium.py | 30 ---------------------- xvfbwrapper/xvfbwrapper.py | 41 ++++++++++++++----------------- 3 files changed, 20 insertions(+), 53 deletions(-) delete mode 100644 xvfbwrapper/test_xvfb_selenium.py diff --git a/xvfbwrapper/__init__.py b/xvfbwrapper/__init__.py index 6815c2d..bb1c6c7 100644 --- a/xvfbwrapper/__init__.py +++ b/xvfbwrapper/__init__.py @@ -1,3 +1,3 @@ from xvfbwrapper import * -__version__ = '0.1.3' +__version__ = '0.1.4' diff --git a/xvfbwrapper/test_xvfb_selenium.py b/xvfbwrapper/test_xvfb_selenium.py deleted file mode 100644 index 423725f..0000000 --- a/xvfbwrapper/test_xvfb_selenium.py +++ /dev/null @@ -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) diff --git a/xvfbwrapper/xvfbwrapper.py b/xvfbwrapper/xvfbwrapper.py index 909d9af..bc0a922 100644 --- a/xvfbwrapper/xvfbwrapper.py +++ b/xvfbwrapper/xvfbwrapper.py @@ -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 +