From 3307c26b1f578670f8fbaab2aded9f0a12de36e5 Mon Sep 17 00:00:00 2001 From: Leonard Camacho Date: Wed, 18 Feb 2015 18:11:33 -0430 Subject: [PATCH] Fix bug 1134276 - Fix selenium tests on Travis --- .travis.yml | 2 + README.md | 3 +- airmozilla/base/tests/testbase.py | 38 ++++++++++++------- .../main/tests/selenium/test_calendar.py | 22 +---------- bin/travis/setup.sh | 9 +---- 5 files changed, 31 insertions(+), 43 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1d5d35f8..59763def 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,6 +17,8 @@ python: #- 2.7 before_install: + - export DISPLAY=:99.0 + - /sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_99.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :99 -ac -screen 0 1280x1024x16 - git submodule update --init --recursive install: bin/travis/install.sh before_script: diff --git a/README.md b/README.md index be0e4260..c05c57af 100644 --- a/README.md +++ b/README.md @@ -366,8 +366,7 @@ Included is a set of comprehensive tests, which you can run by: ``./manage.py test`` If you want to run selenium tests don't forget to add -`RUN_SELENIUM_TESTS = True` in your `airmozilla/settings/local.py` file -also you need to install [PhantomJS](http://phantomjs.org/). +`RUN_SELENIUM_TESTS = True` in your `airmozilla/settings/local.py` file. To see the tests' code coverage, use: ``./manage.py test --with-coverage --cover-erase --cover-html --cover-package=airmozilla`` diff --git a/airmozilla/base/tests/testbase.py b/airmozilla/base/tests/testbase.py index 6a952661..47a70fef 100644 --- a/airmozilla/base/tests/testbase.py +++ b/airmozilla/base/tests/testbase.py @@ -1,26 +1,15 @@ import os import shutil -import functools from nose.plugins.skip import SkipTest +from selenium import webdriver -from django.test import TestCase +from django.test import TestCase, LiveServerTestCase from django.conf import settings from django.contrib.auth.models import User from django.core.files import File -def optional_selenium(test): - - @functools.wraps(test) - def inner(*a, **k): - if not settings.RUN_SELENIUM_TESTS: - raise SkipTest("Test %s is skipped" % test.__name__) - return test(*a, **k) - - return inner - - class DjangoTestCase(TestCase): def shortDescription(self): @@ -54,3 +43,26 @@ def _upload_media(self, image_path): image_path, settings.MEDIA_ROOT ) + + +class DjangoLiveServerTestCase(LiveServerTestCase): + + @classmethod + def setUpClass(cls): + if settings.RUN_SELENIUM_TESTS: + cls.driver = webdriver.Firefox() + cls.driver.implicitly_wait(30) + cls.base_url = cls.live_server_url + cls.driver.set_window_size(1120, 550) + super(DjangoLiveServerTestCase, cls).setUpClass() + + @classmethod + def tearDownClass(cls): + if settings.RUN_SELENIUM_TESTS: + cls.driver.quit() + super(DjangoLiveServerTestCase, cls).tearDownClass() + + def setUp(self): + if not settings.RUN_SELENIUM_TESTS: + raise SkipTest("settings.RUN_SELENIUM_TESTS is set to False") + super(DjangoLiveServerTestCase, self).setUp() diff --git a/airmozilla/main/tests/selenium/test_calendar.py b/airmozilla/main/tests/selenium/test_calendar.py index c13cc698..b575f5b2 100644 --- a/airmozilla/main/tests/selenium/test_calendar.py +++ b/airmozilla/main/tests/selenium/test_calendar.py @@ -1,31 +1,13 @@ import re from nose.tools import eq_, ok_ -from selenium import webdriver from selenium.webdriver.common.by import By from selenium.common.exceptions import NoSuchElementException -from django.test import LiveServerTestCase +from airmozilla.base.tests.testbase import DjangoLiveServerTestCase -from airmozilla.base.tests.testbase import optional_selenium - -class CalendarLiveServerTestCase(LiveServerTestCase): - - @optional_selenium - def setUp(self): - super(CalendarLiveServerTestCase, self).setUp() - self.driver = webdriver.PhantomJS() - self.driver.implicitly_wait(30) - self.base_url = self.live_server_url - self.driver.set_window_size(1120, 550) - # ugly hack necessary to clear localStorage - self.driver.get(self.base_url) - self.driver.execute_script('localStorage.clear()') - - def tearDown(self): - self.driver.quit() - super(CalendarLiveServerTestCase, self).tearDown() +class CalendarLiveServerTestCase(DjangoLiveServerTestCase): def is_element_present(self, how, what): try: diff --git a/bin/travis/setup.sh b/bin/travis/setup.sh index 2c7ef32b..e8585e3f 100755 --- a/bin/travis/setup.sh +++ b/bin/travis/setup.sh @@ -24,12 +24,5 @@ CACHES = { } } -# RUN_SELENIUM_TESTS = True +RUN_SELENIUM_TESTS = True SETTINGS - -# The reason the `RUN_SELENIUM_TESTS=True` piece is commented out is because -# the selenium tests have become unpredictably broken in Travis and causing -# many test builds to stall or error out due to the addition of running -# the selenium tests. -# But we don't want to entirely abandon this effort. We just need to figure -# out how to make it more stable.