forked from django-cms/django-cms
/
frontend.py
151 lines (134 loc) · 5.86 KB
/
frontend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-
from cms.api import create_page
from cms.models import Page
from cms.test_utils.util.context_managers import SettingsOverride
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.utils import unittest
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, NoAlertPresentException
from django.test import LiveServerTestCase
import os
class CMSLiveTests(LiveServerTestCase):
@classmethod
def setUpClass(cls):
if os.environ.get('SELENIUM', '1') == '0':
#skip selenium tests
raise unittest.SkipTest("Selenium env is set to 0")
capabilities = webdriver.DesiredCapabilities.CHROME
capabilities['version'] = '31'
capabilities['platform'] = 'OS X 10.9'
capabilities['name'] = 'django CMS'
if os.environ.get("TRAVIS_BUILD_NUMBER"):
capabilities['build'] = os.environ.get("TRAVIS_BUILD_NUMBER")
capabilities['tags'] = [os.environ.get("TRAVIS_PYTHON_VERSION"), "CI"]
username = os.environ.get("SAUCE_USERNAME")
access_key = os.environ.get("SAUCE_ACCESS_KEY")
capabilities["tunnel-identifier"] = os.environ.get("TRAVIS_JOB_NUMBER")
hub_url = "http://%s:%s@ondemand.saucelabs.com/wd/hub" % (username, access_key)
cls.driver = webdriver.Remote(desired_capabilities=capabilities, command_executor=hub_url)
cls.driver.implicitly_wait(30)
else:
cls.driver = webdriver.Firefox()
cls.accept_next_alert = True
super(CMSLiveTests, cls).setUpClass()
def stop_server(self):
if hasattr(self, 'server_thread'):
self.server_thread.join()
@classmethod
def tearDownClass(cls):
cls.driver.quit()
super(CMSLiveTests, cls).tearDownClass()
def wait_until(self, callback, timeout=10):
"""
Helper function that blocks the execution of the tests until the
specified callback returns a value that is not falsy. This function can
be called, for example, after clicking a link or submitting a form.
See the other public methods that call this function for more details.
"""
from selenium.webdriver.support.wait import WebDriverWait
WebDriverWait(self.driver, timeout).until(callback)
def wait_loaded_tag(self, tag_name, timeout=10):
"""
Helper function that blocks until the element with the given tag name
is found on the page.
"""
self.wait_until(
lambda driver: driver.find_element_by_tag_name(tag_name),
timeout
)
def wait_page_loaded(self):
"""
Block until page has started to load.
"""
from selenium.common.exceptions import TimeoutException
try:
# Wait for the next page to be loaded
self.wait_loaded_tag('body')
except TimeoutException:
# IE7 occasionnally returns an error "Internet Explorer cannot
# display the webpage" and doesn't load the next page. We just
# ignore it.
pass
def is_element_present(self, how, what):
try:
self.driver.find_element(by=how, value=what)
except NoSuchElementException:
return False
return True
def is_alert_present(self):
try:
self.driver.switch_to_alert()
except NoAlertPresentException:
return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally:
self.accept_next_alert = True
class ToolbarBasicTests(CMSLiveTests):
def setUp(self):
Site.objects.create(domain='example.org', name='example.org')
self.base_url = self.live_server_url
self.driver.implicitly_wait(2)
user = User()
user.username = 'admin'
user.set_password('admin')
user.is_superuser = user.is_staff = user.is_active = True
user.save()
super(ToolbarBasicTests, self).setUp()
def test_toolbar_login(self):
create_page('Home', 'simple.html', 'en', published=True).publish()
url = '%s/?edit' % self.live_server_url
self.driver.get(url)
self.assertRaises(NoSuchElementException, self.driver.find_element_by_class_name, 'cms_toolbar-item_logout')
username_input = self.driver.find_element_by_id("id_cms-username")
username_input.send_keys('admin')
password_input = self.driver.find_element_by_id("id_cms-password")
password_input.send_keys('admin')
password_input.submit()
self.wait_page_loaded()
self.assertTrue(self.driver.find_element_by_class_name('cms_toolbar-item-navigation'))
def test_basic_add_pages(self):
with SettingsOverride(DEBUG=True):
self.assertEqual(Page.objects.all().count(), 0)
driver = self.driver
driver.get(self.base_url + "/de/")
driver.find_element_by_id("add-page").click()
driver.find_element_by_id("id_username").clear()
driver.find_element_by_id("id_username").send_keys("admin")
driver.find_element_by_id("id_password").clear()
driver.find_element_by_id("id_password").send_keys("admin")
driver.find_element_by_css_selector("input[type=\"submit\"]").click()
driver.find_element_by_name("_save").click()
driver.find_element_by_link_text(u"Seite hinzufügen").click()
driver.find_element_by_id("id_title").clear()
driver.find_element_by_id("id_title").send_keys("SubPage")
driver.find_element_by_name("_save").click()