david-dick / firefox-marionette

This is a client module to automate the Mozilla Firefox browser via the Marionette protocol
https://metacpan.org/dist/Firefox-Marionette
Other
12 stars 3 forks source link

how to get http status code? #31

Closed 532910 closed 9 months ago

532910 commented 9 months ago

I'd like to make pdf, but only if 200 was returned. If server returns 404 or 503 for example, pdf just prints page with "404" or "503" text.

david-dick commented 9 months ago

do you mean with the $firefox->pdf() method, or by going directly to a pdf url, like $firefox->go("https://$hostname/$path.pdf);

532910 commented 9 months ago

yep, $firefix->pdf method

david-dick commented 9 months ago

okay, probably the best option is to retrieve the page and then use the $firefox->har() method to extract the session and search for the url(s) that make up the page. Does that work?

532910 commented 9 months ago

It sounds hard, there are no libarchive-har-perl in debian, trying to Dumper( $firefox->pdf ) gives: Perl: javascript error: ReferenceError: HAR is not defined

Is it possible to get http status code from firefox after Navigate (and provide it as $firefox->status_code())?

david-dick commented 9 months ago

libarchive-har-perl makes things a bit nicer, but what you are trying to achieve can be done with


use strict;
use warnings;
use Firefox::Marionette();

my $url = 'https://www.google.com/';
my $firefox = Firefox::Marionette->new(har => 1);
$firefox->go($url);
foreach my $entry (@{$firefox->har()->{log}->{entries}}) {
        if ($entry->{request}->{url} eq $url) {
                if ($entry->{response}->{status} == 200) {
                        warn "Printing successful page";
                        $firefox->pdf();
                }
        }
}

does that look ok?

532910 commented 9 months ago

yes, though I'd checked all statues:

my @statuses = map { $_->{response}->{status} } @{$firefox->har->{log}->{entries}};
if( @statuses == grep { $_ eq 200 } @statuses ) ...

and https://github.com/david-dick/firefox-marionette/issues/32 ):

Thank you, @david-dick!