refactoring and bumped version
This commit is contained in:
parent
fd5e36103d
commit
7af5552722
@ -1,3 +1,3 @@
|
|||||||
from xvfbwrapper import *
|
from xvfbwrapper import *
|
||||||
|
|
||||||
__version__ = '0.1.3'
|
__version__ = '0.1.4'
|
||||||
|
@ -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)
|
|
@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
#
|
#
|
||||||
# * Corey Goldberg, 2012
|
# * Corey Goldberg, 2012, 2013
|
||||||
#
|
#
|
||||||
# * inspired by:
|
# * inspired by:
|
||||||
# PyVirtualDisplay: http://pypi.python.org/pypi/PyVirtualDisplay
|
# PyVirtualDisplay: http://pypi.python.org/pypi/PyVirtualDisplay
|
||||||
@ -16,41 +16,37 @@ import subprocess
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
||||||
class Xvfb(object):
|
class Xvfb:
|
||||||
|
|
||||||
def __init__(self, width=800, height=680, colordepth=24):
|
def __init__(self, width=800, height=680, colordepth=24):
|
||||||
self.width = width
|
self.width = width
|
||||||
self.height = height
|
self.height = height
|
||||||
self.colordepth = colordepth
|
self.colordepth = colordepth
|
||||||
|
|
||||||
self.xvfb_proc = None
|
self.proc = None
|
||||||
self.old_display_num = \
|
if 'DISPLAY' in os.environ:
|
||||||
os.environ['DISPLAY'].split(':')[1] \
|
self.old_display_num = os.environ['DISPLAY'].split(':')[1]
|
||||||
if 'DISPLAY' in os.environ else 0
|
else:
|
||||||
|
self.old_display_num = 0
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
self.vdisplay_num = self.search_for_free_display()
|
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.xvfb_cmd = [
|
||||||
self.vdisplay_num,
|
'Xvfb', ':%d' % (self.vdisplay_num,), '-screen', '0',
|
||||||
self.width,
|
'%dx%dx%d' % (self.width, self.height, self.colordepth)
|
||||||
self.height,
|
]
|
||||||
self.colordepth,
|
self.proc = subprocess.Popen(self.xvfb_cmd,
|
||||||
os.devnull
|
stdout=open(os.devnull),
|
||||||
)
|
stderr=open(os.devnull))
|
||||||
self.xvfb_proc = subprocess.Popen(self.xvfb_cmd,
|
|
||||||
stdout=open(os.devnull),
|
|
||||||
stderr=open(os.devnull),
|
|
||||||
shell=True
|
|
||||||
)
|
|
||||||
time.sleep(0.1) # give Xvfb time to start
|
time.sleep(0.1) # give Xvfb time to start
|
||||||
self._redirect_display(self.vdisplay_num)
|
self._redirect_display(self.vdisplay_num)
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self._redirect_display(self.old_display_num)
|
self._redirect_display(self.old_display_num)
|
||||||
if self.xvfb_proc is not None:
|
if self.proc is not None:
|
||||||
self.xvfb_proc.kill()
|
self.proc.kill()
|
||||||
self.xvfb_proc.wait()
|
self.proc.wait()
|
||||||
self.xvfb_proc = None
|
self.proc = None
|
||||||
|
|
||||||
def search_for_free_display(self):
|
def search_for_free_display(self):
|
||||||
ls = [int(x.split('X')[1].split('-')[0]) for x in self._lock_files()]
|
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):
|
def _redirect_display(self, display_num):
|
||||||
os.environ['DISPLAY'] = ':%s' % display_num
|
os.environ['DISPLAY'] = ':%s' % display_num
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user