7
7
import os
8
8
from nose .plugins import Plugin
9
9
from selenium import webdriver
10
+ from pyvirtualdisplay import Display
10
11
from seleniumbase .core import selenium_launcher
11
12
from seleniumbase .core import browser_launcher
12
13
from seleniumbase .fixtures import constants
@@ -20,7 +21,10 @@ class SeleniumBrowser(Plugin):
20
21
The following variables are made to the tests:
21
22
self.options.browser -- the browser to use (--browser)
22
23
self.options.server -- the server used by the test (--server)
23
- self.options.port -- the port used by thest (--port)
24
+ self.options.port -- the port used by the test (--port)
25
+ self.options.headless -- the option to run headlessly (--headless)
26
+ self.options.demo_mode -- the option to slow down Selenium (--demo_mode)
27
+ self.options.demo_sleep -- Selenium action delay in DemoMode (--demo_sleep)
24
28
"""
25
29
name = 'selenium' # Usage: --with-selenium
26
30
@@ -46,6 +50,11 @@ def options(self, parser, env):
46
50
default = '4444' ,
47
51
help = """Designates the port used by the test.
48
52
Default: 4444.""" )
53
+ parser .add_option ('--headless' , action = "store_true" ,
54
+ dest = 'headless' ,
55
+ default = False ,
56
+ help = """Using this makes Webdriver run headlessly,
57
+ which is useful inside a Linux Docker.""" )
49
58
parser .add_option ('--demo_mode' , action = "store_true" ,
50
59
dest = 'demo_mode' ,
51
60
default = False ,
@@ -84,6 +93,7 @@ def configure(self, options, conf):
84
93
self .browser_settings ["version" ] = options .browser_version
85
94
86
95
self .options = options
96
+ self .headless_active = False
87
97
88
98
if (self .options .servername == "localhost" and
89
99
self .options .browser == constants .Browser .HTML_UNIT ):
@@ -95,6 +105,10 @@ def beforeTest(self, test):
95
105
""" Running Selenium locally will be handled differently
96
106
from how Selenium is run remotely, such as from Jenkins. """
97
107
108
+ if self .options .headless :
109
+ self .display = Display (visible = 0 , size = (1200 , 800 ))
110
+ self .display .start ()
111
+ self .headless_active = True
98
112
if self .options .servername == "localhost" :
99
113
try :
100
114
self .driver = self .__select_browser (self .options .browser )
@@ -127,19 +141,22 @@ def beforeTest(self, test):
127
141
except Exception as err :
128
142
print "Attempt #%s to connect to Selenium failed" % i
129
143
if i < 3 :
130
- print "Retrying in 15 seconds..."
131
- time .sleep (15 )
144
+ print "Retrying in 3 seconds..."
145
+ time .sleep (3 )
132
146
if not connected :
133
147
print "Error starting/connecting to Selenium:"
134
148
print err
135
- print "\n \n \n "
149
+ print "\n \n "
136
150
os .kill (os .getpid (), 9 )
137
151
138
152
def afterTest (self , test ):
139
153
try :
140
154
self .driver .quit ()
141
155
except :
142
156
print "No driver to quit."
157
+ if self .options .headless :
158
+ if self .headless_active :
159
+ self .display .stop ()
143
160
144
161
def __select_browser (self , browser_name ):
145
162
if (self .options .servername != "localhost" or
0 commit comments