Skip to content

Commit

Permalink
test: added scenarios for downloading separate files and an archive
Browse files Browse the repository at this point in the history
  • Loading branch information
nekhvoya committed Aug 14, 2023
1 parent 3bc7d21 commit 49475f4
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 3 deletions.
4 changes: 2 additions & 2 deletions test/e2e/pageobjects/create_paste_page.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging
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 test.e2e.utils.string_utils import random_string
from playwright.sync_api import Page, expect

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -75,7 +75,7 @@ def reload(self):

# Step sequences
def paste_random_text(self, paste_number=0):
paste_text = string.ascii_letters + string.digits
paste_text = random_string()
self.type_paste(paste_text, paste_number)
self.should_have_value_in_paste_input(paste_text, paste_number)
return paste_text
Expand Down
26 changes: 26 additions & 0 deletions test/e2e/pageobjects/view_paste_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ def __init__(self, page: Page) -> None:
self.copy_button = page.locator(".copy-button")
self.raw_button = page.get_by_role("link", name="raw")
self.hex_button = page.get_by_role("link", name="hex")
self.download_file_button = self.page_locator.get_by_role(
"link", name="download"
)
self.download_archive_button = self.page.locator(
".paste-meta"
).get_by_role("link", name="download")

def click_remove_now_button(self):
log.info("Clicking Remove Now Button")
Expand All @@ -30,6 +36,26 @@ def click_hex_button(self):
log.info("Clicking Hex Button")
self.hex_button.click()

def click_download_file_button(self, paste_number=0):
log.info("Clicking Download File Button")
self.download_file_button.nth(paste_number).click()

def click_download_archive_button(self):
log.info("Clicking Download Archive Button")
self.download_archive_button.click()

def download_file(self, paste_number=0):
log.info("Clicking Download File Button")
with self.page.expect_download() as downloaded_info:
self.click_download_file_button(paste_number)
return downloaded_info.value

def download_archive(self):
log.info("Clicking Download Archive Button")
with self.page.expect_download() as downloaded_info:
self.click_download_archive_button()
return downloaded_info.value

# Expectations
def should_have_pasted_text(self, text, paste_number=0):
expect(
Expand Down
44 changes: 44 additions & 0 deletions test/e2e/testscenarios/test_download_paste.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from test.e2e.conftest import create_paste_page
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 (
verify_downloaded_file_contents,
verify_downloaded_archive_contents,
)
import pytest
from playwright.sync_api import Page


@pytest.mark.e2e
def test_download_files(page: Page, create_paste_page: CreatePastePage):
first_pasted_text = create_paste_page.paste_random_text(paste_number=0)
create_paste_page.click_add_another_file_button()

second_pasted_text = create_paste_page.paste_random_text(paste_number=1)
create_paste_page.click_submit()

view_paste_page = ViewPastePage(page)
view_paste_page.should_be_opened()

first_download = view_paste_page.download_file(paste_number=0)
verify_downloaded_file_contents(first_download, first_pasted_text)

second_download = view_paste_page.download_file(paste_number=1)
verify_downloaded_file_contents(second_download, second_pasted_text)


@pytest.mark.e2e
def test_download_archive(page: Page, create_paste_page: CreatePastePage):
first_pasted_text = create_paste_page.paste_random_text(paste_number=0)
create_paste_page.click_add_another_file_button()

second_pasted_text = create_paste_page.paste_random_text(paste_number=1)
create_paste_page.click_submit()

view_paste_page = ViewPastePage(page)
view_paste_page.should_be_opened()

zip_download = view_paste_page.download_archive()
verify_downloaded_archive_contents(
zip_download, first_pasted_text, second_pasted_text
)
40 changes: 40 additions & 0 deletions test/e2e/utils/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import os
from contextlib import contextmanager
from pathlib import Path
from zipfile import ZipFile
import shutil


@contextmanager
Expand All @@ -19,3 +21,41 @@ def create_random_file():

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


def verify_downloaded_file_contents(download, text):
dir_path = tempfile.mkdtemp()
file_path = download_path(dir_path, download)
download.save_as(file_path)
try:
with open(file_path) as file:
assert file.read() == text
finally:
shutil.rmtree(dir_path)


def verify_downloaded_archive_contents(
archive_download, *args, match_all_args=True
):
dir_path = tempfile.mkdtemp()
file_path = download_path(dir_path, archive_download)
archive_download.save_as(file_path)
try:
with ZipFile(file_path) as zip_file:
file_names = zip_file.namelist()
if match_all_args:
assert len(file_names) == len(
args
), "Number of files in downloaded archive was incorrect"
for file_name in file_names:
with zip_file.open(file_name) as file:
file_content = file.read().decode()
assert (
file_content in args
), f"Unexpected content of file in archive {file_content}"
finally:
shutil.rmtree(dir_path)


def download_path(download_dir_path, download):
return os.path.join(download_dir_path, download.suggested_filename)
4 changes: 3 additions & 1 deletion test/e2e/utils/string_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
def random_string(size=1000) -> str:
return "".join(
[
random.choice(string.ascii_letters + string.digits)
random.choice(
string.ascii_letters + string.digits + string.punctuation
)
for i in range(size)
]
)

0 comments on commit 49475f4

Please sign in to comment.