UPD: more tests.

This commit is contained in:
2019-03-21 17:59:35 +01:00
parent ff32ffbc1e
commit 1bcd479304
2 changed files with 592 additions and 26 deletions

View File

@@ -1,12 +1,14 @@
import datetime
import os
import sys
import time
import urllib
from unittest import skip
from unittest import skip, SkipTest
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.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
@@ -157,14 +159,27 @@ class FormsTestCase(TestCase):
class SeleniumTestCase(StaticLiveServerTestCase):
def setUp(self):
super(SeleniumTestCase, self).setUp()
self.selenium = webdriver.Firefox()
if not hasattr(self, 'quit_selenium'):
self.quit_selenium = True
window_width = 1024
window_height = 768
def __init__(self, *args, **kwargs):
super(SeleniumTestCase, self).__init__(*args, **kwargs)
self._driver = None
self._driver_options = webdriver.FirefoxOptions()
self.quit_selenium = None
@property
def selenium(self):
if self._driver is None:
self._driver = webdriver.Firefox(options=self._driver_options)
if self.quit_selenium is None:
self.quit_selenium = True
if self.window_width and self.window_height:
self._driver.set_window_size(self.window_width, self.window_height)
return self._driver
def tearDown(self):
if hasattr(self, 'quit_selenium') and self.quit_selenium:
if self.quit_selenium:
self.selenium.quit()
super(SeleniumTestCase, self).tearDown()
@@ -172,37 +187,37 @@ class SeleniumTestCase(StaticLiveServerTestCase):
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 get(self, location):
return self.selenium.get(self.complete_url(location))
def wait_on(self, driver, ec_name, ec_argument, timeout=30):
ec = getattr(EC, ec_name)
return WebDriverWait(driver, timeout).until(ec(ec_argument))
def wait_on_presence(self, driver, locator, timeout=30):
return WebDriverWait(driver, timeout).until(EC.presence_of_element_located(locator))
ec_name = 'presence_of_element_located'
return self.wait_on(driver, ec_name, locator, timeout)
def wait_until_stale(self, driver, element, timeout=30):
ec_name = 'staleness_of'
return self.wait_on(driver, ec_name, element, timeout)
class ScreenshotTestCase(SeleniumTestCase):
screenshot_prefix = ''
window_width = 1024
window_height = 768
locations = ()
@classmethod
def setUpClass(cls):
super(ScreenshotTestCase, cls).setUpClass()
def __init__(self, *args, **kwargs):
super(ScreenshotTestCase, self).__init__(*args, **kwargs)
# 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()
if self.window_width and self.window_height:
self.selenium.set_window_size(self.window_width, self.window_height)
self.screenshot_path = screenshot_base_dir
self.screenshot_sequences = {}
def sanitize_filename(self, location):
return urllib.quote(location).replace('/', '--')
def save_screenshot(self, title=None, sequence=None):
def save_screenshot(self, title=None, sequence=None, resize=True):
if sequence is None:
sequence = ''
else:
@@ -230,10 +245,20 @@ class ScreenshotTestCase(SeleniumTestCase):
path = os.path.join(self.screenshot_path, self.sanitize_filename(base_name))
if not os.path.isdir(self.screenshot_path):
os.makedirs(self.screenshot_path, 0700)
restore_size = False
if resize:
window_size = self.selenium.get_window_size()
deco_height = self.selenium.execute_script('return window.outerHeight - window.innerHeight')
doc_height = self.selenium.execute_script('return document.body.scrollHeight')
if (window_size['height'] - deco_height) < doc_height:
self.selenium.set_window_size(window_size['width'], doc_height + deco_height)
restore_size = True
self.selenium.save_screenshot(path)
def get(self, location):
return self.selenium.get(self.complete_url(location))
if restore_size:
self.selenium.set_window_size(window_size['width'], window_size['height'])
@skip_unless_tag_option()
@tag('screenshots')