[PATCH 50/51] tests: Add a couple of Selenium tests

Damien Lespiau damien.lespiau at intel.com
Sat Sep 12 01:55:23 AEST 2015


While developing the new series UI, several bug crept in but weren't
discovered until later. All because we don't have in-browser tests to go
along the lower level tests we already have.

In particular, behaviours that need javascript to run cannot be tested
outside of a full environment with the pages being served to an actual
browser.

This commit introduces selenium to the test suite and starts with 2
simple tests to give a taste of what it looks like.

test_default_focus: make sure we do focus the username field on the
                    login page

test_login: shows how to chain actions to test the full login phase.
            This is quite similar the lower level test, except it also
            checks we display the username once logged in.

Signed-off-by: Damien Lespiau <damien.lespiau at intel.com>
---
 docs/requirements-dev.txt            |   1 +
 patchwork/tests/browser.py           | 155 +++++++++++++++++++++++++++++++++++
 patchwork/tests/test_user_browser.py |  38 +++++++++
 3 files changed, 194 insertions(+)
 create mode 100644 patchwork/tests/browser.py
 create mode 100644 patchwork/tests/test_user_browser.py

diff --git a/docs/requirements-dev.txt b/docs/requirements-dev.txt
index eee6463..12b8bef 100644
--- a/docs/requirements-dev.txt
+++ b/docs/requirements-dev.txt
@@ -1 +1,2 @@
 -r requirements-base.txt
+selenium
diff --git a/patchwork/tests/browser.py b/patchwork/tests/browser.py
new file mode 100644
index 0000000..d13c770
--- /dev/null
+++ b/patchwork/tests/browser.py
@@ -0,0 +1,155 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2015 Intel Corporation
+#
+# This file is part of the Patchwork package.
+#
+# Patchwork is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# Patchwork is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Patchwork; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+import errno
+import os
+import time
+
+from django.contrib.staticfiles.testing import StaticLiveServerTestCase
+from selenium import webdriver
+from selenium.webdriver.support.ui import WebDriverWait
+from selenium.common.exceptions import NoSuchElementException, \
+        StaleElementReferenceException, TimeoutException
+
+class Wait(WebDriverWait):
+    """Subclass of WebDriverWait with predetermined timeout and poll
+    frequency.  Also deals with a wider variety of exceptions."""
+    _TIMEOUT = 10
+    _POLL_FREQUENCY = 0.5
+
+    def __init__(self, driver):
+        super(Wait, self).__init__(driver, self._TIMEOUT, self._POLL_FREQUENCY)
+
+    def until(self, method, message=''):
+        """Calls the method provided with the driver as an argument until the
+        return value is not False."""
+
+        end_time = time.time() + self._timeout
+        while True:
+            try:
+                value = method(self._driver)
+                if value:
+                    return value
+            except NoSuchElementException:
+                pass
+            except StaleElementReferenceException:
+                pass
+
+            time.sleep(self._poll)
+            if(time.time() > end_time):
+                break
+
+        raise TimeoutException(message)
+
+    def until_not(self, method, message=''):
+        """Calls the method provided with the driver as an argument until the
+        return value is False."""
+
+        end_time = time.time() + self._timeout
+        while(True):
+            try:
+                value = method(self._driver)
+                if not value:
+                    return value
+            except NoSuchElementException:
+                return True
+            except StaleElementReferenceException:
+                pass
+
+            time.sleep(self._poll)
+            if(time.time() > end_time):
+                break
+
+        raise TimeoutException(message)
+
+def mkdir(path):
+    try:
+        os.makedirs(path)
+    except OSError as error: # Python >2.5
+        if error.errno == errno.EEXIST and os.path.isdir(path):
+            pass
+        else:
+            raise
+
+class SeleniumTestCase(StaticLiveServerTestCase):
+    _SCREENSHOT_DIR = os.path.dirname(__file__) + '/../../test_screenshots'
+
+    def setUp(self):
+        super(SeleniumTestCase, self).setUp()
+
+        self.browser = os.getenv('SELENIUM_BROWSER', 'chrome')
+        if self.browser == 'firefox':
+            self.selenium = webdriver.Firefox()
+        if self.browser == 'chrome':
+            self.selenium = webdriver.Chrome()
+
+        mkdir(self._SCREENSHOT_DIR)
+        self._screenshot_number = 1
+
+    def tearDown(self):
+        self.selenium.quit()
+        super(SeleniumTestCase, self).tearDown()
+
+    def screenshot(self):
+        name = "%s_%d.png" % (self._testMethodName, self._screenshot_number)
+        path = os.path.join(self._SCREENSHOT_DIR, name)
+        self.selenium.get_screenshot_as_file(path)
+        self._screenshot_number += 1
+
+    def get(self, relative_url):
+        self.selenium.get('%s%s' % (self.live_server_url, relative_url))
+        self.screenshot()
+
+    def find(self, selector):
+        return self.selenium.find_element_by_css_selector(selector)
+
+    def focused_element(self):
+        return self.selenium.switch_to.active_element
+
+    def wait_until_present(self, name):
+        is_present = lambda driver: driver.find_element_by_name(name)
+        msg = "An element named '%s' should be on the page" % name
+        element = Wait(self.selenium).until(is_present, msg)
+        self.screenshot()
+        return element
+
+    def wait_until_visible(self, selector):
+        is_visible = lambda driver: self.find(selector).is_displayed()
+        msg = "The element matching '%s' should be visible" % selector
+        Wait(self.selenium).until(is_visible, msg)
+        self.screenshot()
+        return self.find(selector)
+
+    def wait_until_focused(self, selector):
+        is_focused  = \
+                lambda driver: self.find(selector) == self.focused_element()
+        msg = "The element matching '%s' should be focused" % selector
+        Wait(self.selenium).until(is_focused, msg)
+        self.screenshot()
+        return self.find(selector)
+
+    def enter_text(self, name, value):
+        field = self.wait_until_present(name)
+        field.send_keys(value)
+        return field
+
+    def click(self, selector):
+        element = self.wait_until_visible(selector)
+        element.click()
+        return element
diff --git a/patchwork/tests/test_user_browser.py b/patchwork/tests/test_user_browser.py
new file mode 100644
index 0000000..2b9ed2e
--- /dev/null
+++ b/patchwork/tests/test_user_browser.py
@@ -0,0 +1,38 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2015 Intel Corporation
+#
+# This file is part of the Patchwork package.
+#
+# Patchwork is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# Patchwork is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Patchwork; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+from patchwork.tests.browser import SeleniumTestCase
+from patchwork.tests.test_user import TestUser
+
+class LoginTestCase(SeleniumTestCase):
+    def setUp(self):
+        super(LoginTestCase, self).setUp()
+        self.user = TestUser()
+
+    def test_default_focus(self):
+        self.get('/user/login/')
+        self.wait_until_focused('#id_username')
+
+    def test_login(self):
+        self.get('/user/login/')
+        self.enter_text('username', self.user.username)
+        self.enter_text('password', self.user.password)
+        self.click('input[value="Login"]')
+        dropdown = self.wait_until_visible('a.dropdown-toggle strong')
+        self.assertEquals(dropdown.text, 'testuser')
-- 
2.1.0



More information about the Patchwork mailing list