Skip to content

Commit

Permalink
test: added scenarios for the drag-and-drop feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nekhvoya authored and supakeen committed Aug 7, 2023
1 parent 8ed2c3c commit 98ee323
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
22 changes: 22 additions & 0 deletions test/e2e/pageobjects/create_paste_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import string
from test.e2e.env_config import BASE_URL
from test.e2e.pageobjects.base_page import BasePage
from test.e2e.utils.file_utils import extract_file_name

from playwright.sync_api import Page, expect

Expand All @@ -19,6 +20,7 @@ def __init__(self, page: Page) -> None:
self.submit_button = page.locator(".paste-submit button[type=submit]")
self.add_another_paste_button = page.locator("button.add")
self.file_input = page.locator("#file-input")
self.file_drop_section = "#file-drop"

def open(self):
log.info(f"Opening Pinnwand at {self.url}")
Expand All @@ -40,6 +42,26 @@ def add_file_to_file_input(self, file_paths):
log.info("Adding file for uploading")
self.file_input.set_input_files(file_paths)

def drag_and_drop_file(self, *files):
def get_file_contents(file):
file.seek(0)
return {
"file_name": extract_file_name(file.name),
"file_content": file.read().decode(),
}

file_contents = list(map(get_file_contents, files))

self.page.evaluate(
"""([files, dropAreaSelector]) => {
var dataTransfer = new DataTransfer();
files.forEach(file => dataTransfer.items.add(new File([file.file_content.toString('hex')], file.file_name, { type: 'text/plain' })));
var event = new DragEvent("drop", { dataTransfer: dataTransfer });
document.querySelector(dropAreaSelector).dispatchEvent(event);
}""",
[file_contents, self.file_drop_section],
)

# Step sequences
def paste_random_text(self, paste_number=0):
paste_text = string.ascii_letters + string.digits
Expand Down
49 changes: 49 additions & 0 deletions test/e2e/testscenarios/test_drag_and_drop_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from test.e2e.pageobjects.create_paste_page import CreatePastePage
from test.e2e.pageobjects.view_paste_page import ViewPastePage
from test.e2e.utils.file_utils import create_random_file
import pytest
from playwright.sync_api import Page


@pytest.mark.e2e
def test_drag_and_drop_single_file(
page: Page, create_paste_page: CreatePastePage
):
with create_random_file() as temp:
file_content = temp.read().decode()
create_paste_page.drag_and_drop_file(temp)
create_paste_page.should_have_value_in_paste_input(file_content)
create_paste_page.click_submit()

view_paste_page = ViewPastePage(page)
view_paste_page.should_be_opened()
view_paste_page.should_have_pasted_text(file_content)


@pytest.mark.e2e
def test_drag_and_drop_multiple_files(
page: Page, create_paste_page: CreatePastePage
):
with create_random_file() as first_temp, create_random_file() as second_temp, create_random_file() as third_temp:
first_file_content = first_temp.read().decode()
second_file_content = second_temp.read().decode()
third_file_content = third_temp.read().decode()
create_paste_page.drag_and_drop_file(
first_temp, second_temp, third_temp
)
create_paste_page.should_have_value_in_paste_input(
first_file_content, paste_number=0
)
create_paste_page.should_have_value_in_paste_input(
second_file_content, paste_number=1
)
create_paste_page.should_have_value_in_paste_input(
third_file_content, paste_number=2
)
create_paste_page.click_submit()

view_paste_page = ViewPastePage(page)
view_paste_page.should_be_opened()
view_paste_page.should_have_pasted_text(first_file_content, paste_number=0)
view_paste_page.should_have_pasted_text(second_file_content, paste_number=1)
view_paste_page.should_have_pasted_text(third_file_content, paste_number=2)
5 changes: 5 additions & 0 deletions test/e2e/utils/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import tempfile
import os
from contextlib import contextmanager
from pathlib import Path


@contextmanager
Expand All @@ -14,3 +15,7 @@ def create_random_file():
finally:
temp.close()
os.remove(temp.name)


def extract_file_name(file_path):
return Path(file_path).name

0 comments on commit 98ee323

Please sign in to comment.