Skip to content

Commit 66181e9

Browse files
committed
--skip/--fail for html, javascript, pdf, accessibility - closes #102
1 parent ec15d20 commit 66181e9

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

docs/accessibility.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Options:
3838
-j, --javascript TEXT Execute this JS prior to taking the snapshot
3939
--timeout INTEGER Wait this many milliseconds before failing
4040
--log-console Write console.log() to stderr
41+
--fail Fail with an error code if a page returns an HTTP error
42+
--skip Skip pages that return HTTP errors
4143
--help Show this message and exit.
4244
```
4345
<!-- [[[end]]] -->

docs/html.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ Options:
6565
-b, --browser [chromium|firefox|webkit|chrome|chrome-beta]
6666
Which browser to use
6767
--user-agent TEXT User-Agent header to use
68+
--fail Fail with an error code if a page returns an
69+
HTTP error
70+
--skip Skip pages that return HTTP errors
6871
--help Show this message and exit.
6972
```
7073
<!-- [[[end]]] -->

docs/javascript.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ Options:
182182
--user-agent TEXT User-Agent header to use
183183
--reduced-motion Emulate 'prefers-reduced-motion' media feature
184184
--log-console Write console.log() to stderr
185+
--fail Fail with an error code if a page returns an
186+
HTTP error
187+
--skip Skip pages that return HTTP errors
185188
--help Show this message and exit.
186189
```
187190
<!-- [[[end]]] -->

docs/pdf.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ Options:
6060
--scale FLOAT RANGE Scale of the webpage rendering [0.1<=x<=2.0]
6161
--print-background Print background graphics
6262
--log-console Write console.log() to stderr
63+
--fail Fail with an error code if a page returns an
64+
HTTP error
65+
--skip Skip pages that return HTTP errors
6366
--help Show this message and exit.
6467
```
6568
<!-- [[[end]]] -->

shot_scraper/cli.py

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@ def skip_fail_options(fn):
5353
return fn
5454

5555

56+
def skip_or_fail(response, skip, fail):
57+
if skip and fail:
58+
raise click.ClickException("--skip and --fail cannot be used together")
59+
if str(response.status)[0] in ("4", "5"):
60+
if skip:
61+
click.echo(
62+
"{} error for {}, skipping".format(response.status, response.url),
63+
err=True,
64+
)
65+
# Exit with a 0 status code
66+
raise SystemExit
67+
elif fail:
68+
raise click.ClickException(
69+
"{} error for {}".format(response.status, response.url)
70+
)
71+
72+
5673
def reduced_motion_option(fn):
5774
click.option(
5875
"--reduced-motion",
@@ -413,7 +430,11 @@ def multi(
413430
continue
414431
try:
415432
take_shot(
416-
context, shot, log_console=log_console, skip=skip, fail=fail,
433+
context,
434+
shot,
435+
log_console=log_console,
436+
skip=skip,
437+
fail=fail,
417438
)
418439
except TimeoutError as e:
419440
if fail_on_error:
@@ -445,7 +466,8 @@ def multi(
445466
help="Wait this many milliseconds before failing",
446467
)
447468
@log_console_option
448-
def accessibility(url, auth, output, javascript, timeout, log_console):
469+
@skip_fail_options
470+
def accessibility(url, auth, output, javascript, timeout, log_console, skip, fail):
449471
"""
450472
Dump the Chromium accessibility tree for the specifed page
451473
@@ -459,7 +481,8 @@ def accessibility(url, auth, output, javascript, timeout, log_console):
459481
page = context.new_page()
460482
if log_console:
461483
page.on("console", console_log)
462-
page.goto(url)
484+
response = page.goto(url)
485+
skip_or_fail(response, skip, fail)
463486
if javascript:
464487
_evaluate_js(page, javascript)
465488
snapshot = page.accessibility.snapshot()
@@ -501,6 +524,7 @@ def accessibility(url, auth, output, javascript, timeout, log_console):
501524
@user_agent_option
502525
@reduced_motion_option
503526
@log_console_option
527+
@skip_fail_options
504528
def javascript(
505529
url,
506530
javascript,
@@ -512,6 +536,8 @@ def javascript(
512536
user_agent,
513537
reduced_motion,
514538
log_console,
539+
skip,
540+
fail,
515541
):
516542
"""
517543
Execute JavaScript against the page and return the result as JSON
@@ -552,7 +578,8 @@ def javascript(
552578
page = context.new_page()
553579
if log_console:
554580
page.on("console", console_log)
555-
page.goto(url)
581+
response = page.goto(url)
582+
skip_or_fail(response, skip, fail)
556583
result = _evaluate_js(page, javascript)
557584
browser_obj.close()
558585
if raw:
@@ -613,6 +640,7 @@ def javascript(
613640
)
614641
@click.option("--print-background", is_flag=True, help="Print background graphics")
615642
@log_console_option
643+
@skip_fail_options
616644
def pdf(
617645
url,
618646
auth,
@@ -627,6 +655,8 @@ def pdf(
627655
scale,
628656
print_background,
629657
log_console,
658+
skip,
659+
fail,
630660
):
631661
"""
632662
Create a PDF of the specified page
@@ -651,7 +681,8 @@ def pdf(
651681
page = context.new_page()
652682
if log_console:
653683
page.on("console", console_log)
654-
page.goto(url)
684+
response = page.goto(url)
685+
skip_or_fail(response, skip, fail)
655686
if wait:
656687
time.sleep(wait / 1000)
657688
if javascript:
@@ -709,6 +740,7 @@ def pdf(
709740
@log_console_option
710741
@browser_option
711742
@user_agent_option
743+
@skip_fail_options
712744
def html(
713745
url,
714746
auth,
@@ -719,6 +751,8 @@ def html(
719751
log_console,
720752
browser,
721753
user_agent,
754+
skip,
755+
fail,
722756
):
723757
"""
724758
Output the final HTML of the specified page
@@ -741,7 +775,8 @@ def html(
741775
page = context.new_page()
742776
if log_console:
743777
page.on("console", console_log)
744-
page.goto(url)
778+
response = page.goto(url)
779+
skip_or_fail(response, skip, fail)
745780
if wait:
746781
time.sleep(wait / 1000)
747782
if javascript:

0 commit comments

Comments
 (0)