UPD: improved tests.
This commit is contained in:
@@ -7,6 +7,8 @@ from django.contrib.staticfiles.testing import StaticLiveServerTestCase
|
||||
from django.test import SimpleTestCase, TestCase, tag
|
||||
from django.urls import reverse
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.support.ui import WebDriverWait
|
||||
from selenium.webdriver.support import expected_conditions as EC
|
||||
|
||||
|
||||
def skip_unless_tag_option():
|
||||
@@ -126,7 +128,33 @@ class FormsTestCase(TestCase):
|
||||
self.assertIn(code, error_codes)
|
||||
|
||||
|
||||
class ScreenshotTestCase(StaticLiveServerTestCase):
|
||||
class SeleniumTestCase(StaticLiveServerTestCase):
|
||||
def setUp(self):
|
||||
super(SeleniumTestCase, self).setUp()
|
||||
self.selenium = webdriver.Firefox()
|
||||
if not hasattr(self, 'quit_selenium'):
|
||||
self.quit_selenium = True
|
||||
|
||||
def tearDown(self):
|
||||
if hasattr(self, 'quit_selenium') and self.quit_selenium:
|
||||
self.selenium.quit()
|
||||
super(SeleniumTestCase, self).tearDown()
|
||||
|
||||
def complete_url(self, location):
|
||||
base_url = self.live_server_url
|
||||
return '{}/{}'.format(base_url, location.lstrip('/'))
|
||||
|
||||
def wait_until_stale(self, driver, element, timeout=30):
|
||||
return WebDriverWait(driver, timeout).until(EC.staleness_of(element))
|
||||
|
||||
def wait_on_presence(self, driver, locator, timeout=30):
|
||||
return WebDriverWait(driver, timeout).until(EC.presence_of_element_located(locator))
|
||||
|
||||
|
||||
class ScreenshotTestCase(SeleniumTestCase):
|
||||
screenshot_prefix = ''
|
||||
window_width = 1024
|
||||
window_height = 768
|
||||
locations = ()
|
||||
|
||||
@classmethod
|
||||
@@ -136,18 +164,12 @@ class ScreenshotTestCase(StaticLiveServerTestCase):
|
||||
# screenshot_base_dir = os.path.join('/', 'tmp', 'test-screenshots')
|
||||
screenshot_base_dir = 'test-screenshots'
|
||||
cls.screenshot_path = screenshot_base_dir
|
||||
cls.screenshot_sequences = {}
|
||||
|
||||
def setUp(self):
|
||||
super(ScreenshotTestCase, self).setUp()
|
||||
self.selenium = webdriver.Firefox()
|
||||
self.selenium.set_window_size(1024, 768)
|
||||
|
||||
def tearDown(self):
|
||||
self.selenium.quit()
|
||||
super(ScreenshotTestCase, self).tearDown()
|
||||
|
||||
def complete_url(self, location):
|
||||
return '{}/{}'.format(self.live_server_url, location.lstrip('/'))
|
||||
if self.window_width and self.window_height:
|
||||
self.selenium.set_window_size(self.window_width, self.window_height)
|
||||
|
||||
def sanitize_filename(self, location):
|
||||
location = location.lstrip('/')
|
||||
@@ -156,21 +178,32 @@ class ScreenshotTestCase(StaticLiveServerTestCase):
|
||||
r = urllib.quote(location, '')
|
||||
return r
|
||||
|
||||
def save_screenshot(self, name=None):
|
||||
if self.screenshot_path:
|
||||
if name is None:
|
||||
location = self.selenium.current_url
|
||||
if location.startswith(self.live_server_url):
|
||||
location = location[len(self.live_server_url):]
|
||||
name = location
|
||||
def save_screenshot(self, name=None, sequence=None):
|
||||
if name is not None:
|
||||
pass
|
||||
elif sequence is not None:
|
||||
if sequence in self.screenshot_sequences:
|
||||
self.screenshot_sequences[sequence] += 1
|
||||
else:
|
||||
self.screenshot_sequences[sequence] = 1
|
||||
n = self.screenshot_sequences[sequence]
|
||||
name = '%s-%04d' % (sequence, n)
|
||||
else:
|
||||
location = self.selenium.current_url
|
||||
if location.startswith(self.live_server_url):
|
||||
location = location[len(self.live_server_url):]
|
||||
name = location
|
||||
|
||||
now = datetime.datetime.now()
|
||||
base_name = '{timestamp}-{name}.png'.format(name=self.sanitize_filename(name),
|
||||
timestamp=now.strftime('%Y%m%d-%H%M%S'))
|
||||
path = os.path.join(self.screenshot_path, base_name)
|
||||
if not os.path.isdir(self.screenshot_path):
|
||||
os.makedirs(self.screenshot_path, 0700)
|
||||
self.selenium.save_screenshot(path)
|
||||
now = datetime.datetime.now()
|
||||
base_name = '{timestamp}-{prefix}{name}.png'.format(
|
||||
prefix=self.screenshot_prefix,
|
||||
name=self.sanitize_filename(name),
|
||||
timestamp=now.strftime('%Y%m%d-%H%M%S')
|
||||
)
|
||||
path = os.path.join(self.screenshot_path, base_name)
|
||||
if not os.path.isdir(self.screenshot_path):
|
||||
os.makedirs(self.screenshot_path, 0700)
|
||||
self.selenium.save_screenshot(path)
|
||||
|
||||
@skip_unless_tag_option()
|
||||
@tag('screenshots')
|
||||
|
||||
Reference in New Issue
Block a user