Skip to content

Commit c9dacd2

Browse files
committed
Update an example test (downloading and verifying files)
1 parent c904940 commit c9dacd2

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

examples/test_download_files.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import math
12
from seleniumbase import BaseCase
23

34

@@ -8,8 +9,45 @@ def test_download_files(self):
89
pkg_header = self.get_text("h1.package-header__name")
910
pkg_name = pkg_header.replace(" ", "-")
1011
whl_file = pkg_name + "-py2.py3-none-any.whl"
11-
self.click('div#files a[href$="%s"]' % whl_file)
12-
self.assert_downloaded_file(whl_file)
1312
tar_gz_file = pkg_name + ".tar.gz"
13+
14+
# Click the links to download the files
15+
self.click('div#files a[href$="%s"]' % whl_file)
1416
self.click('div#files a[href$="%s"]' % tar_gz_file)
17+
18+
# Verify that the downloaded files appear in the [Downloads Folder]
19+
# (This only guarantees that the exact file name is in the folder.)
20+
# (This does not guarantee that the downloaded files are complete.)
21+
# (Later, we'll check that the files were downloaded successfully.)
22+
self.assert_downloaded_file(whl_file)
1523
self.assert_downloaded_file(tar_gz_file)
24+
25+
self.sleep(1) # Add more time to make sure downloads have completed
26+
27+
# Get the actual size of the downloaded files (in bytes)
28+
whl_path = self.get_path_of_downloaded_file(whl_file)
29+
with open(whl_path, 'rb') as f:
30+
whl_file_bytes = len(f.read())
31+
print("\n%s | Download = %s bytes." % (whl_file, whl_file_bytes))
32+
tar_gz_path = self.get_path_of_downloaded_file(tar_gz_file)
33+
with open(tar_gz_path, 'rb') as f:
34+
tar_gz_file_bytes = len(f.read())
35+
print("%s | Download = %s bytes." % (tar_gz_file, tar_gz_file_bytes))
36+
37+
# Check to make sure the downloaded files are not empty or too small
38+
self.assert_true(whl_file_bytes > 5000)
39+
self.assert_true(tar_gz_file_bytes > 5000)
40+
41+
# Get file sizes in kB to compare actual values with displayed values
42+
whl_file_kb = whl_file_bytes / 1000.0
43+
whl_line = self.get_text("tbody tr:nth-of-type(1) th")
44+
whl_displayed_kb = float(whl_line.split("(")[1].split(" ")[0])
45+
tar_gz_file_kb = tar_gz_file_bytes / 1000.0
46+
tar_gz_line = self.get_text("tbody tr:nth-of-type(2) th")
47+
tar_gz_displayed_kb = float(tar_gz_line.split("(")[1].split(" ")[0])
48+
49+
# Verify downloaded files are the correct size (account for rounding)
50+
self.assert_true(abs(
51+
math.floor(whl_file_kb) - math.floor(whl_displayed_kb)) < 2)
52+
self.assert_true(abs(
53+
math.floor(tar_gz_file_kb) - math.floor(tar_gz_displayed_kb)) < 2)

0 commit comments

Comments
 (0)