Skip to content

Commit

Permalink
Merge pull request #4958 from DrGFreeman/4561_crop_screenshots
Browse files Browse the repository at this point in the history
Docs - Auto-crop screenshots (#4561)
  • Loading branch information
redshiftzero authored Oct 28, 2019
2 parents 3617ddf + 203215e commit b9011dc
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions securedrop/requirements/python3/test-requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ blinker
Flask-Testing
flaky
mock
pillow
pip-tools>=4.0.0
py
pytest
Expand Down
33 changes: 32 additions & 1 deletion securedrop/requirements/python3/test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile --generate-hashes --allow-unsafe --output-file=requirements/python3/test-requirements.txt requirements/python3/test-requirements.in
# pip-compile --allow-unsafe --generate-hashes --output-file=requirements/python3/test-requirements.txt requirements/python3/test-requirements.in
#
atomicwrites==1.3.0 \
--hash=sha256:03472c30eb2c5d1ba9227e4c2ca66ab8287fbfbbda3888aa93dc2e28fc6811b4 \
Expand Down Expand Up @@ -113,6 +113,37 @@ pbr==3.1.1 \
--hash=sha256:05f61c71aaefc02d8e37c0a3eeb9815ff526ea28b3b76324769e6158d7f95be1 \
--hash=sha256:60c25b7dfd054ef9bb0ae327af949dd4676aa09ac3a9471cdc871d8a9213f9ac \
# via mock
pillow==6.2.1 \
--hash=sha256:047d9473cf68af50ac85f8ee5d5f21a60f849bc17d348da7fc85711287a75031 \
--hash=sha256:0f66dc6c8a3cc319561a633b6aa82c44107f12594643efa37210d8c924fc1c71 \
--hash=sha256:12c9169c4e8fe0a7329e8658c7e488001f6b4c8e88740e76292c2b857af2e94c \
--hash=sha256:248cffc168896982f125f5c13e9317c059f74fffdb4152893339f3be62a01340 \
--hash=sha256:27faf0552bf8c260a5cee21a76e031acaea68babb64daf7e8f2e2540745082aa \
--hash=sha256:285edafad9bc60d96978ed24d77cdc0b91dace88e5da8c548ba5937c425bca8b \
--hash=sha256:384b12c9aa8ef95558abdcb50aada56d74bc7cc131dd62d28c2d0e4d3aadd573 \
--hash=sha256:38950b3a707f6cef09cd3cbb142474357ad1a985ceb44d921bdf7b4647b3e13e \
--hash=sha256:4aad1b88933fd6dc2846552b89ad0c74ddbba2f0884e2c162aa368374bf5abab \
--hash=sha256:4ac6148008c169603070c092e81f88738f1a0c511e07bd2bb0f9ef542d375da9 \
--hash=sha256:4deb1d2a45861ae6f0b12ea0a786a03d19d29edcc7e05775b85ec2877cb54c5e \
--hash=sha256:59aa2c124df72cc75ed72c8d6005c442d4685691a30c55321e00ed915ad1a291 \
--hash=sha256:5a47d2123a9ec86660fe0e8d0ebf0aa6bc6a17edc63f338b73ea20ba11713f12 \
--hash=sha256:5cc901c2ab9409b4b7ac7b5bcc3e86ac14548627062463da0af3b6b7c555a871 \
--hash=sha256:6c1db03e8dff7b9f955a0fb9907eb9ca5da75b5ce056c0c93d33100a35050281 \
--hash=sha256:7ce80c0a65a6ea90ef9c1f63c8593fcd2929448613fc8da0adf3e6bfad669d08 \
--hash=sha256:809c19241c14433c5d6135e1b6c72da4e3b56d5c865ad5736ab99af8896b8f41 \
--hash=sha256:83792cb4e0b5af480588601467c0764242b9a483caea71ef12d22a0d0d6bdce2 \
--hash=sha256:846fa202bd7ee0f6215c897a1d33238ef071b50766339186687bd9b7a6d26ac5 \
--hash=sha256:9f5529fc02009f96ba95bea48870173426879dc19eec49ca8e08cd63ecd82ddb \
--hash=sha256:a423c2ea001c6265ed28700df056f75e26215fd28c001e93ef4380b0f05f9547 \
--hash=sha256:ac4428094b42907aba5879c7c000d01c8278d451a3b7cccd2103e21f6397ea75 \
--hash=sha256:b1ae48d87f10d1384e5beecd169c77502fcc04a2c00a4c02b85f0a94b419e5f9 \
--hash=sha256:bf4e972a88f8841d8fdc6db1a75e0f8d763e66e3754b03006cbc3854d89f1cb1 \
--hash=sha256:c6414f6aad598364aaf81068cabb077894eb88fed99c6a65e6e8217bab62ae7a \
--hash=sha256:c710fcb7ee32f67baf25aa9ffede4795fd5d93b163ce95fdc724383e38c9df96 \
--hash=sha256:c7be4b8a09852291c3c48d3c25d1b876d2494a0a674980089ac9d5e0d78bd132 \
--hash=sha256:c9e5ffb910b14f090ac9c38599063e354887a5f6d7e6d26795e916b4514f2c1a \
--hash=sha256:e0697b826da6c2472bb6488db4c0a7fa8af0d52fa08833ceb3681358914b14e5 \
--hash=sha256:e9a3edd5f714229d41057d56ac0f39ad9bdba6767e8c888c951869f0bdd129b0
pip-tools==4.0.0 \
--hash=sha256:3b9fb8948340eff5869ac83dc85e3a7c62b837cec33609c45c48c2e5aa740ba5 \
--hash=sha256:44469037863c3587b4c565caf258e2c752d4235c508cf8410a69164bb65ffc78
Expand Down
2 changes: 1 addition & 1 deletion securedrop/tests/functional/functional_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def create_firefox_driver(self):
firefox_binary=FIREFOX_PATH, firefox_profile=profile
)
self.firefox_driver.set_window_position(0, 0)
self.firefox_driver.set_window_size(1024, 768)
self.firefox_driver.set_window_size(1024, 1200)
logging.info("Created Firefox web driver")
break
except Exception as e:
Expand Down
26 changes: 25 additions & 1 deletion securedrop/tests/pageslayout/functional_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import io
import logging
import os
from os.path import abspath
Expand All @@ -25,6 +26,8 @@

from tests.functional import functional_test

from PIL import Image


def list_locales():
if "PAGE_LAYOUT_LOCALES" in os.environ:
Expand All @@ -34,6 +37,25 @@ def list_locales():
return locales


def autocrop_btm(img, bottom_padding=12):
"""Automatically crop the bottom of a screenshot."""
# Get the grayscale of img
gray = img.convert('L')
# We start one row above the bottom since the "modal" windows screenshots
# have a bottom line color different than the background
btm = img.height - 2
# Get the background luminance value from the bottom-leftmost pixel
bg = gray.getpixel((0, btm))

# Move up until the full row is not of the background luminance
while all([gray.getpixel((col, btm)) == bg for col in range(gray.width)]):
btm -= 1

btm = min(btm + bottom_padding, img.height)

return img.crop((0, 0, img.width, btm))


class FunctionalTest(functional_test.FunctionalTest):
default_driver_name = functional_test.FIREFOX

Expand All @@ -55,4 +77,6 @@ def _screenshot(self, filename):
if not os.path.exists(log_dir):
os.makedirs(log_dir)

self.driver.save_screenshot(os.path.join(log_dir, filename))
img = Image.open(io.BytesIO(self.driver.get_screenshot_as_png()))
cropped = autocrop_btm(img)
cropped.save(os.path.join(log_dir, filename))

0 comments on commit b9011dc

Please sign in to comment.