Code

Ticket #2879: doh.diff

File doh.diff, 3.0 KB (added by tomchristie, 3 years ago)

django.contrib.selenium for previous diff

Line 
1diff --git a/django/contrib/selenium/__init__.py b/django/contrib/selenium/__init__.py
2index e69de29..0946a37 100644
3--- a/django/contrib/selenium/__init__.py
4+++ b/django/contrib/selenium/__init__.py
5@@ -0,0 +1 @@
6+from django.contrib.selenium.testcases import SeleniumRCTestCase
7diff --git a/django/contrib/selenium/testcases.py b/django/contrib/selenium/testcases.py
8index e69de29..39ce209 100644
9--- a/django/contrib/selenium/testcases.py
10+++ b/django/contrib/selenium/testcases.py
11@@ -0,0 +1,73 @@
12+import httplib
13+import socket
14+
15+from django.conf import settings
16+from django.test.testcases import LiveServerTestCase
17+from django.utils.unittest import skipUnless, SkipTest
18+
19+try:
20+    from selenium import selenium
21+except ImportError:
22+    selenium_installed = False
23+else:
24+    selenium_installed = True
25+
26+_NOT_INSTALLED_MSG = "The 'selenium' package isn't installed"
27+_CANT_CONNECT_MSG = ("Can't connect to the Selenium server using address"
28+                     " %s and port %s")
29+
30+
31+class SeleniumRCTestCase(LiveServerTestCase):
32+    """
33+    Does basically the same as TestServerTestCase but also connects to the
34+    Selenium RC server. The selenium client is then available with
35+    'self.selenium'. The requirements are to have the 'selenium' installed in
36+    the python path and to have the Selenium RC server running. If those
37+    requirements are not filled then the tests will be skipped.
38+    """
39+    _selenium_server_running = None
40+
41+    @property
42+    def selenium_server_running(self):
43+        """
44+        Determine if we can connect to the Selenium RC server.
45+        Only evaluated once for performance reasons.
46+        """
47+        if SeleniumRCTestCase._selenium_server_running is not None:
48+            return SeleniumRCTestCase._selenium_server_running
49+
50+        try:
51+            conn = httplib.HTTPConnection(settings.SELENIUM_RC_HOST,
52+                                          settings.SELENIUM_RC_PORT)
53+            try:
54+                conn.request("GET", "/selenium-server/driver/", '', {})
55+            finally:
56+                conn.close()
57+            SeleniumRCTestCase._selenium_server_running = True
58+        except socket.error:
59+            SeleniumRCTestCase._selenium_server_running = False
60+
61+        return SeleniumRCTestCase._selenium_server_running
62+
63+    def setUp(self):
64+        if not self.selenium_server_running:
65+            message = (_CANT_CONNECT_MSG % (settings.SELENIUM_RC_HOST,
66+                                            settings.SELENIUM_RC_PORT))
67+            raise SkipTest(message)
68+
69+        super(SeleniumRCTestCase, self).setUp()
70+
71+        # Launch the Selenium server
72+        self.selenium = selenium(
73+            settings.SELENIUM_RC_HOST,
74+            int(settings.SELENIUM_RC_PORT),
75+            settings.SELENIUM_RC_BROWSER,
76+            self.live_test_server_url)
77+        self.selenium.start()
78+
79+    def tearDown(self):
80+        super(SeleniumRCTestCase, self).tearDown()
81+        self.selenium.stop()
82+
83+SeleniumRCTestCase = skipUnless(selenium_installed,
84+    _NOT_INSTALLED_MSG)(SeleniumRCTestCase)