Skip to content

Commit

Permalink
Add dnd system test
Browse files Browse the repository at this point in the history
  • Loading branch information
mbraak committed Oct 10, 2024
1 parent a8c2fa0 commit b7023e5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
12 changes: 12 additions & 0 deletions testproject/django_file_form_example/tests/live/test_with_ajax.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,3 +947,15 @@ def test_disabled_field(self):
self.assertTrue(file_input.get_attribute("disabled"))

self.assertEqual(len(page.find_elements_by_text("Drop your files here")), 0)

def test_drop_file(self):
page = self.page
temp_file = page.create_temp_file("content1")

page.open("/")
drop_area = page.selenium.find_element(By.CSS_SELECTOR, ".dff-files")
page.drop_file(drop_area, temp_file)

page.find_upload_success(temp_file)
page.assert_page_contains_text("8 Bytes")
self.assertEqual(TemporaryUploadedFile.objects.count(), 1)
35 changes: 35 additions & 0 deletions testproject/django_file_form_example/tests/utils/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,38 @@ def get_percentage(selenium):
WebDriverWait(self.selenium, timeout=10, poll_frequency=0.1).until(
lambda selenium: get_percentage(selenium) > 5
)

def drop_file(self, drop_area, temp_file):
ATTACH_FILE = """
var input = document.createElement('INPUT');
input.type = "file";
input.id = "_capybara_drop_file";
input.multiple = true;
document.body.appendChild(input);
return input;
"""

DROP_FILE = """
var el = arguments[0],
input = arguments[1],
files = input.files,
dt = new DataTransfer(),
opts = { cancelable: true, bubbles: true, dataTransfer: dt };
input.parentElement.removeChild(input);
if (dt.items){
for (var i=0; i<files.length; i++){
dt.items.add(files[i]);
}
} else {
Object.defineProperty(dt, "files", {
value: files,
writable: false
});
}
var dropEvent = new DragEvent('drop', opts);
el.dispatchEvent(dropEvent);
"""

input_element = self.selenium.execute_script(ATTACH_FILE)
input_element.send_keys(temp_file.path())
self.selenium.execute_script(DROP_FILE, drop_area, input_element)

0 comments on commit b7023e5

Please sign in to comment.