Pcore-WebDriver

 view release on metacpan or  search on metacpan

lib/Pcore/WebDriver/Session.pm  view on Meta::CPAN

    };

    my $useragent = !exists $args->{useragent} ? $DEFAULT_USERAGENT : $args->{useragent};

    # chrome options
    if ( $self->{wdh}->{is_chrome} ) {
        $caps->{chromeOptions} = {
            args => [    #

                # https://bugs.chromium.org/p/chromium/issues/detail?id=721739
                # NOTE headless chrome ignoring --ignore-certificate-errors
                $args->{proxy} ? ( "--proxy-server=$args->{proxy}", '--ignore-certificate-errors', '--allow-insecure-localhost' ) : (),

                # https://bugs.chromium.org/p/chromedriver/issues/detail?id=878
                # NOTE remote debuggind not works with chromedriver
                # '--remote-debugging-address=192.168.175.10',
                # '--remote-debugging-port=9222',

                '--disable-background-networking',
                '--disable-client-side-phishing-detection',
                '--disable-component-update',
                '--disable-hang-monitor',
                '--disable-prompt-on-repost',
                '--disable-sync',
                '--disable-web-resources',

                '--start-maximized',
                '--window-size=1280x720',

                '--disable-default-apps',
                '--no-first-run',
                '--disable-infobars',
                '--disable-popup-blocking',
                '--disable-default-apps',
                '--disable-web-security',
                '--allow-running-insecure-content',

                # logging
                # '--disable-logging',
                # '--log-level=0',

                # set user profile dir
                qq[--user-data-dir=$ENV->{TEMP_DIR}chrome-wds-@{[P->uuid->str]}],

                # required for run under docker
                '--no-sandbox',

                # user agent
                defined $useragent ? qq[--user-agent=$useragent] : (),

                # headless mode
                ( $args->{headless} || !$MSWIN ) && !$self->{wdh}->{xvfb} ? ( '--headless', '--disable-gpu' ) : (),

                # user args
                $args->{args} ? $args->{args}->@* : (),

                # open "about:blank" by default
                'about:blank',
            ],
            binary => $args->{bin} // q[],
            prefs => {

                #         ( $self->disable_images ? ( 'profile.default_content_setting_values.images' => 2 ) : () ),
                #         ( $self->enable_notifications ? () : ( 'profile.default_content_setting_values.notifications' => 2 ) ),
                #         ( $self->enable_flash         ? () : ( 'profile.default_content_setting_values.plugins'       => 2 ) ),
            }
        };
    }

    # phantomjs options
    elsif ( $self->{wdh}->{is_phantomjs} ) {
        if ( $args->{proxy} ) {
            $caps->{proxy} = {
                proxyType => 'manual',
                httpProxy => $args->{proxy},
                sslProxy  => $args->{proxy},
            };
        }

        $caps->{'phantomjs.page.settings.userAgent'} = $useragent if defined $useragent;
    }

    return { desiredCapabilities => $caps };
}

around new => sub ( $orig, $self, @args ) {
    my $cb = is_plain_coderef $args[-1] ? pop @args : undef;

    P->http->request(
        method    => 'POST',
        url       => "http://$self->{wdh}->{host}:$self->{wdh}->{port}/session",
        timeout   => 0,
        body      => to_json( $self->_get_desired_caps( {@args} ) ),
        on_finish => sub ($res) {
            if ( !$res ) {
                $cb->( undef, result 500 );
            }
            else {
                my $data = from_json $res->body;

                if ( $data->{status} ) {
                    $cb->( undef, result [ 500, $data->{value}->{message} ] );
                }
                else {
                    my $wds = bless {
                        wdh                => $self->{wdh},
                        is_chrome          => $self->{wdh}->{is_chrome} ? 1 : 0,
                        is_phantomjs       => $self->{wdh}->{is_phantomjs} ? 1 : 0,
                        id                 => $data->{sessionId},
                        _chrome_scoped_dir => $data->{value}->{chrome}->{userDataDir},
                      },
                      __PACKAGE__;

                    $cb->( $wds, result 200 );
                }
            }

            return;
        },
    );



( run in 1.479 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )