Skip to content

Commit

Permalink
Write clipboard test
Browse files Browse the repository at this point in the history
  • Loading branch information
hansegucker committed Jan 15, 2024
1 parent a3384d1 commit a0bb516
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 28 deletions.
Binary file added evap/copy_header.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions evap/evaluation/tests/test_live.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def test_contact_modal(self):
test_user = self._default_login()

self.selenium.get(self.live_server_url + reverse("evaluation:index"))
self._screenshot("feedback_modal_login")
self.selenium.find_element(By.ID, "feedbackModalShowButton").click()
self._screenshot("feedback_modal_")

Expand Down
43 changes: 27 additions & 16 deletions evap/evaluation/tests/tools.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import functools
import os
import time
from collections.abc import Sequence
from contextlib import contextmanager
from datetime import timedelta
Expand Down Expand Up @@ -253,15 +254,8 @@ def assert_no_database_modifications(*args, **kwargs):
raise AssertionError("Unexpected modifying query found: " + query["sql"])


class CustomSeleniumTestCaseBase(SeleniumTestCaseBase):
def create_options(self): # pylint: disable=bad-mcs-method-argument
# Note: This workaround is probably no longer necessary with Django 5.0
options = super().create_options()
options.add_argument("--headless")
return options


class LiveServerTest(SeleniumTestCase, metaclass=CustomSeleniumTestCaseBase):
class LiveServerTest(SeleniumTestCase):
external_host = os.environ.get("TEST_HOST", "") or None
browser = "firefox"
selenium_hub = os.environ.get("TEST_SELENIUM_HUB", "") or None
Expand All @@ -286,26 +280,43 @@ def _default_login(self):
self._login(user)
return user

def _login(self, user):
"""Login a test user by setting the session cookie."""
self.selenium.get(self.live_server_url)

# Create fake session to do user login workflow
def _create_session(self):
request = HttpRequest()
engine = import_module(settings.SESSION_ENGINE)
request.session = engine.SessionStore()
login(request, user, "django.contrib.auth.backends.ModelBackend")
request.session.save()

self.request = request

def _update_session(self):
self.request.session.save()
# Create session cookie
cookie_data = {
"name": settings.SESSION_COOKIE_NAME,
"value": request.session.session_key,
"value": self.request.session.session_key,
"path": "/",
"secure": settings.SESSION_COOKIE_SECURE or False,
}
self.selenium.add_cookie(cookie_data)


def _login(self, user):
"""Login a test user by setting the session cookie."""
self.selenium.get(self.live_server_url)

# Create fake session to do user login workflow
self._create_session()
login(self.request, user, "django.contrib.auth.backends.ModelBackend")
self._update_session()

def _enter_staff_mode(self):
self.request.session["staff_mode_start_time"] = time.time()
self._update_session()

def _exit_staff_mode(self):
if "staff_mode_start_time" in self.request.session:
del self.request.session["staff_mode_start_time"]
self._update_session()

@classmethod
def tearDownClass(cls):
cls.selenium.quit()
Binary file added evap/feedback_modal_.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added evap/feedback_modal_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added evap/feedback_modal_login.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added evap/feedback_modal_success.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added evap/feedback_modal_typed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions evap/staff/tests/test_live.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from django.urls import reverse
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from evap.evaluation.tests.tools import LiveServerTest


class StaffLiveTests(LiveServerTest):
def test_copy_header(self):
self._default_login()

self._enter_staff_mode()

self.selenium.get(self.live_server_url + reverse("staff:user_import"))

WebDriverWait(self.selenium, 10).until(
expected_conditions.visibility_of_element_located((By.CLASS_NAME, "btn-link"))
)

# Patch clipboard functions to test functionality
self.selenium.execute_script("navigator.clipboardExecuted = false;")
self.selenium.execute_script("navigator.clipboard.writeText = (c) => navigator.clipboardExecuted = c;")

self.selenium.find_element(By.CLASS_NAME, "btn-link").click()

copied_text = self.selenium.execute_script("return navigator.clipboardExecuted;")
self.assertEqual(copied_text, "Title\tFirst name\tLast name\tEmail")



13 changes: 1 addition & 12 deletions evap/static/ts/src/copy-to-clipboard.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
function copyToClipboard(text: string) {
const selection = document.getSelection()!;
const el = document.createElement("textarea");
el.value = text;
document.body.appendChild(el);
const selected = selection.rangeCount > 0 ? selection.getRangeAt(0) : false;
el.select();
document.execCommand("copy");
el.remove();
if (selected) {
selection.removeAllRanges();
selection.addRange(selected);
}
navigator.clipboard.writeText(text);
}

function copyHeaders(headers: string[]) {
Expand Down

0 comments on commit a0bb516

Please sign in to comment.