Skip to content

Commit 5a8ac16

Browse files
committed
Ability to run shot-craper multi --har and not take shots at all
Refs #166 (comment)
1 parent 8b83b59 commit 5a8ac16

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

shot_scraper/cli.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,13 @@ def multi(
549549

550550
scale_factor = normalize_scale_factor(retina, scale_factor)
551551
shots = yaml.safe_load(config)
552+
553+
# Special case: if we are recording a har_file output can be blank to skip a shot
554+
if har_file:
555+
for shot in shots:
556+
if not shot.get("output"):
557+
shot["skip_shot"] = True
558+
552559
server_processes = []
553560
if shots is None:
554561
shots = []
@@ -1370,12 +1377,16 @@ def on_response(response):
13701377
", ".join(list(selectors) + list(selectors_all)), url, output
13711378
)
13721379
else:
1373-
# Whole page
1374-
if return_bytes:
1375-
return page.screenshot(**screenshot_args)
1380+
if shot.get("skip_shot"):
1381+
message = "Skipping screenshot of '{}'".format(url)
13761382
else:
1377-
page.screenshot(**screenshot_args)
1378-
message = "Screenshot of '{}' written to '{}'".format(url, output)
1383+
# Whole page
1384+
if return_bytes:
1385+
return page.screenshot(**screenshot_args)
1386+
else:
1387+
page.screenshot(**screenshot_args)
1388+
message = "Screenshot of '{}' written to '{}'".format(url, output)
1389+
13791390
if not silent:
13801391
click.echo(message, err=True)
13811392

tests/test_shot_scraper.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -262,31 +262,36 @@ def test_har(http_server, args, expect_zip):
262262

263263

264264
@pytest.mark.parametrize(
265-
"args,expect_zip",
265+
"args,expect_zip,record_shots",
266266
(
267-
(["--har"], False),
268-
(["--har-zip"], True),
269-
(["--har-file", "output.har"], False),
270-
(["--har-file", "output.har.zip"], True),
267+
(["--har"], False, True),
268+
(["--har-zip"], True, True),
269+
(["--har-file", "output.har"], False, True),
270+
(["--har-file", "output.har.zip"], True, True),
271+
# And one where we don't record the shots:
272+
(["--har"], False, False),
271273
),
272274
)
273-
def test_multi_har(http_server, args, expect_zip):
275+
def test_multi_har(http_server, args, expect_zip, record_shots):
274276
runner = CliRunner()
275277
(http_server.base_dir / "two.html").write_text("<h1>Two</h1>")
276278
with runner.isolated_filesystem():
277279
pathlib.Path("shots.yml").write_text(
278280
f"- url: {http_server.base_url}/\n"
279-
f" output: index.png\n"
280-
f"- url: {http_server.base_url}/two.html\n"
281-
f" output: two.png\n"
281+
+ (f" output: index.png\n" if record_shots else "")
282+
+ f"- url: {http_server.base_url}/two.html\n"
283+
+ (f" output: two.png\n" if record_shots else "")
282284
)
283285
# Should be no files
284286
here = pathlib.Path(".")
285287
files = [str(p) for p in here.glob("*.*")]
286288
assert files == ["shots.yml"]
287289
result = runner.invoke(cli, ["multi", "shots.yml"] + args)
288290
assert result.exit_code == 0
289-
assert result.output.startswith("Screenshot of 'http://localhost")
291+
if record_shots:
292+
assert result.output.startswith("Screenshot of 'http://localhost")
293+
else:
294+
assert result.output.startswith("Skipping screenshot of 'http://localhost")
290295
assert "Wrote to HAR file:" in result.output
291296
assert (".har.zip" in result.output) == expect_zip
292297
# HAR file should have been created
@@ -298,5 +303,9 @@ def test_multi_har(http_server, args, expect_zip):
298303
# Should have created exactly one .har file
299304
assert len(har_files) == 1
300305
assert bool(zipfile.is_zipfile(har_files[0])) == expect_zip
301-
# Should have taken shots
302-
assert len(list(here.glob("*.png"))) == 2
306+
shot_files = list(here.glob("*.png"))
307+
num_shots = len(shot_files)
308+
if record_shots:
309+
assert num_shots == 2
310+
else:
311+
assert num_shots == 0

0 commit comments

Comments
 (0)