Skip to content

Commit

Permalink
Make browser tests work with new editor
Browse files Browse the repository at this point in the history
  • Loading branch information
roadsideseb committed Jul 22, 2014
1 parent 1f57d39 commit 1465ee2
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 147 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
language: python

python:
- '2.7'

sudo: false

cache:
directories:
- $HOME/.pip-cache

addons:
postgresql: "9.3"
firefox: "30.0"

env:
global:
Expand Down Expand Up @@ -37,8 +39,7 @@ env:
- DJANGO="Django>=1.6,<1.7" MODEL_UTILS_VERSION=">2.0" TARGET='test-standalone-fancypages' PYTEST_OPTS="-m browser"

before_install:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
- "/sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_99.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :99 -ac -screen 0 1280x1024x16"

install:
- pip install $PIP_OPTS wheel setuptools -U
Expand Down
17 changes: 7 additions & 10 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import
import os
import pytest
import django
Expand All @@ -18,16 +20,6 @@ def pytest_configure():
django.setup()


@pytest.mark.tryfirst
def pytest_runtest_makereport(item, call, __multicall__):
# execute all other hooks to obtain the report object
rep = __multicall__.execute()
# set an report attribute for each phase of a call, which can
# be "setup", "call", "teardown"
setattr(item, "{}_report".format(rep.when), rep)
return rep


@pytest.fixture
def webtest_csrf_checks():
return True
Expand All @@ -51,3 +43,8 @@ def webtest(request, webtest_csrf_checks, transactional_db):
request.addfinalizer(wtm._unpatch_settings)

return DjangoTestApp()


@pytest.fixture
def splinter_webdriver():
return 'firefox'
22 changes: 22 additions & 0 deletions fancypages/test/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import
import pytest

from django.core.urlresolvers import reverse

from . import factories


@pytest.fixture
def admin_user(live_server, browser):
username = 'peter.griffin'
email = 'peter@griffin.com'
password = 'lazyonthecouch'

user = factories.UserFactory(
username=username, email=email, password=password, is_staff=True)

browser.visit(live_server.url + reverse('admin:index'))
browser.fill_form({'username': username, 'password': password})
browser.find_by_css("input[type='submit']").first.click()
return user
2 changes: 2 additions & 0 deletions fancypages/test/testcases.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import
import os
import mock
import time
Expand Down
259 changes: 146 additions & 113 deletions tests/browser/test_editor.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import
import os
import time
import pytest
import tempfile

from django.db.models import get_model

from PIL import Image
from splinter.element_list import ElementList
from selenium.webdriver.common.keys import Keys

from django.db.models import get_model

from fancypages.test.fixtures import admin_user # noqa
from fancypages.test import TEMP_MEDIA_ROOT, factories
from fancypages.test.testcases import SplinterTestCase

Expand Down Expand Up @@ -64,117 +70,6 @@ def test_remains_opened_when_reloading_the_page(self):
self.assertFalse(body_tag.has_class('editor-hidden'))


class TestATextBlock(SplinterTestCase):
is_staff = True
is_logged_in = True
home_page_url = '/'

def setUp(self):
super(TestATextBlock, self).setUp()
self.page = factories.FancyPageFactory()

@pytest.mark.skipif(
True, reason=("there's an issue with splinter/selenium not "
"putting text into the WYSIWYG editor textarea"))
def test_can_be_added_to_container(self):
self.goto(self.page.get_absolute_url())

self.find_and_click_by_css(self.browser, '#editor-handle')
self.find_and_click_by_css(
self.browser, "div[class=block-add-control]>a")
self.wait_for_editor_reload()

self.find_and_click_by_css(self.browser, "a[href='#content']")

self.find_and_click_by_css(
self.browser, "button[data-block-code=text]")
self.wait_for_editor_reload()

default_text = 'Your text goes here'
if not self.browser.is_text_present(default_text, 2):
self.fail("Could not find text block on page")

if not self.browser.is_element_present_by_css('.edit-button', 2):
self.fail("Could not find edit button for block")

self.find_and_click_by_css(self.browser, '.edit-button')
self.wait_for_editor_reload()

text_sel = 'textarea[name=text]'
if not self.browser.is_element_present_by_css(text_sel, 2):
self.fail("Could not find input area for 'text' field")

new_text = "The new text for this block"
with self.browser.get_iframe(0) as iframe:
ibody = iframe.find_by_css('body')
ibody.type('\b' * (len(default_text) + 1))
ibody.type(new_text)

self.find_and_click_by_css(self.browser, 'button[type=submit]')
self.wait_for_editor_reload()

if not self.browser.is_text_present(new_text, 2):
self.fail("Could not find updated text")

self.assertEquals(TextBlock.objects.count(), 1)
# check for the text with an appended <br> because typing text into
# the input box via Selenium causes a linebreak at the end.
self.assertEquals(TextBlock.objects.all()[0].text, new_text + '<br>')

def test_is_live_updated_when_editing(self):
title = "Sample Title"
text = "This is an amazing piece of text"
factories.TitleTextBlockFactory(
container=self.page.containers.all()[0], title=title, text=text)

self.goto(self.page.get_absolute_url())

self.browser.is_text_present(title)
self.browser.is_text_present(text)

self.open_editor_panel()

self.find_and_click_by_css(self.browser, '.edit-button')
self.wait_for_editor_reload()

title_update = " Is Now New"
self.browser.find_by_css('input[name=title]').type(title_update)
self.browser.is_text_present(title + title_update)
self.browser.is_text_present(text)

text_update = " that I am editing on the fly."
with self.browser.get_iframe(0) as iframe:
ibody = iframe.find_by_css('body')
ibody.type(text_update)
self.browser.is_text_present(text + text_update)
self.browser.is_text_present(title + title_update)

def test_can_be_deleted_from_a_container(self):
title = "Sample Title"
text = "This is an amazing piece of text"
factories.TitleTextBlockFactory(
container=self.page.containers.all()[0], title=title, text=text)

self.goto(self.page.get_absolute_url())

self.browser.is_text_present(title)
self.browser.is_text_present(text)

self.open_editor_panel()

self.find_and_click_by_css(self.browser, 'div.delete')
self.wait_for_editor_reload()

self.assertEqual(
factories.TitleTextBlockFactory._meta.model.objects.count(), 0)

def open_editor_panel(self):
self.find_and_click_by_css(self.browser, '#editor-handle')

if not self.browser.is_element_present_by_css('.edit-button', 2):
self.fail("Could not find edit button for block")


class TestImageBlock(SplinterTestCase):
is_staff = True
is_logged_in = True
Expand Down Expand Up @@ -283,3 +178,141 @@ def test_tabs_can_be_added_and_removed(self):
"[data-block-id='{}']".format(second_tab_id))
if not tab_removed:
self.fail("deleting the second tab failed")


def ensure_element(element_or_list, index=0):
"""
Selects either the element with *index* from the list of elements given
in *element_or_list* or returns the single element if it is not a list.
This make it possible to handle an element and a list of elements where
only a single element is required.
:param element: ``Element`` instance or ``ElementList``.
:parem int index: Index of element to be returned if a list.
(Default: 0)
:rtype: Element
"""
if isinstance(element_or_list, ElementList):
return element_or_list[index]
return element_or_list


def find_and_click_by_css(browser, selector, wait_time=3):
browser.is_element_present_by_css(selector, wait_time)
elem = ensure_element(browser.find_by_css(selector))
return elem.click()


def open_editor_panel(browser):
find_and_click_by_css(browser, '#editor-handle')
if not browser.is_element_present_by_css('.edit-button', 2):
raise AssertionError("Could not find edit button for block")


def wait_for_editor_reload(wait_for=3):
time.sleep(wait_for)


@pytest.mark.browser
def test_text_block_can_be_added_to_a_container(admin_user, browser,
live_server):
fancypage = factories.FancyPageFactory()

title = "Sample Title"
text = "This is an amazing piece of text"
factories.TitleTextBlockFactory(
container=fancypage.containers.all()[0], title=title, text=text)

browser.visit(live_server.url + fancypage.get_absolute_url())

browser.is_text_present(title)
browser.is_text_present(text)

open_editor_panel(browser)

find_and_click_by_css(browser, '.edit-button')
wait_for_editor_reload()

title_update = " Is Now New"
browser.find_by_css('input[name=title]').type(title_update)
browser.is_text_present(title + title_update)
browser.is_text_present(text)

text_update = " that I am editing on the fly."
rte = browser.find_by_css('.trumbowyg-editor')
rte.type(text_update)

browser.is_text_present(text + text_update)
browser.is_text_present(title + title_update)


@pytest.mark.browser
def test_text_block_can_be_deleted_from_a_container(admin_user, browser,
live_server):
fancypage = factories.FancyPageFactory()
title = "Sample Title"
text = "This is an amazing piece of text"
factories.TitleTextBlockFactory(
container=fancypage.containers.all()[0], title=title, text=text)

browser.visit(live_server.url + fancypage.get_absolute_url())

browser.is_text_present(title)
browser.is_text_present(text)

open_editor_panel(browser)

find_and_click_by_css(browser, 'div.delete')
wait_for_editor_reload()

assert factories.TitleTextBlockFactory._meta.model.objects.count() == 0


@pytest.mark.browser
def test_text_block_can_be_added_to_container(admin_user, browser,
live_server):
fancypage = factories.FancyPageFactory()

browser.visit(live_server.url + fancypage.get_absolute_url())

find_and_click_by_css(browser, '#editor-handle')
find_and_click_by_css(browser, "div[class=block-add-control]>a")
wait_for_editor_reload()

find_and_click_by_css(browser, "a[href='#content']")

find_and_click_by_css(browser, "button[data-block-code=text]")
wait_for_editor_reload()

default_text = 'Your text goes here'
if not browser.is_text_present(default_text, 2):
raise AssertionError("Could not find text block on page")

if not browser.is_element_present_by_css('.edit-button', 2):
raise AssertionError("Could not find edit button for block")

find_and_click_by_css(browser, '.edit-button')
wait_for_editor_reload()

text_sel = 'textarea[name=text]'
if not browser.is_element_present_by_css(text_sel, 2):
raise AssertionError("Could not find input area for 'text' field")

new_text = "The new text for this block"
rte = browser.find_by_css('.trumbowyg-editor')
# Jump to the end of the line and remove it using the backspace key
rte.type(Keys.ARROW_RIGHT * (len(default_text) + 1))
rte.type(Keys.BACKSPACE * (len(default_text) + 1))
# Now add the new text
rte.type(new_text)

find_and_click_by_css(browser, 'button[type=submit]')
wait_for_editor_reload()

if not browser.is_text_present(new_text, 2):
raise AssertionError("Could not find updated text")

assert TextBlock.objects.count() == 1
# check for the text with an appended <br> because typing text into
# the input box via Selenium causes a linebreak at the end.
assert TextBlock.objects.all()[0].text == new_text + '<br>'
Loading

0 comments on commit 1465ee2

Please sign in to comment.