The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Playwright - Perl client for Playwright

VERSION

version 0.007

SYNOPSIS

    use Playwright;

    my $handle = Playwright->new();
    my $browser = $handle->launch( headless => 0, type => 'chrome' );
    my $page = $browser->newPage();
    my $res = $page->goto('http://somewebsite.test', { waitUntil => 'networkidle' });
    my $frameset = $page->mainFrame();
    my $kidframes = $frameset->childFrames();

    # Grab us some elements
    my $body = $page->select('body');

    # You can also get the innerText
    my $text = $body->textContent();
    $body->click();
    $body->screenshot();

    my $kids = $body->selectMulti('*');

DESCRIPTION

Perl interface to a lightweight node.js webserver that proxies commands runnable by Playwright. Checks and automatically installs a copy of the node dependencies in the local folder if needed.

Currently understands commands you can send to all the playwright classes defined in api.json (installed wherever your OS puts shared files for CPAN distributions).

See https://playwright.dev/versions and drill down into your relevant version (run `npm list playwright` ) for what the classes do, and their usage.

All the classes mentioned there will correspond to a subclass of the Playwright namespace. For example:

    # ISA Playwright
    my $playwright = Playwright->new();
    # ISA Playwright::BrowserContext
    my $ctx = $playwright->newContext(...);
    # ISA Playwright::Page
    my $page = $ctx->newPage(...);
    # ISA Playwright::ElementHandle
    my $element = $ctx->select('body');

See example.pl for a more thoroughly fleshed-out display on how to use this module.

Questions?

Feel free to join the Playwright slack server, as there is a dedicated #playwright-perl channel which I, the module author, await your requests in. https://aka.ms/playwright-slack

Why this documentation does not list all available subclasses and their methods

The documentation and names for the subclasses of Playwright follow the spec strictly:

Playwright::BrowserContext => https://playwright.dev/docs/api/class-browsercontext Playwright::Page => https://playwright.dev/docs/api/class-page Playwright::ElementHandle => https://playwright.dev/docs/api/class-elementhandle

...And so on. 100% of the spec is accessible regardless of the Playwright version installed due to these classes & their methods being built dynamically at run time based on the specification which is shipped with Playwright itself.

You can check what methods are installed for each subclass by doing the following:

    use Data::Dumper;
    print Dumper($instance->{spec});

There are two major exceptions in how things work versus the upstream Playwright documentation, detailed below in the Selectors section.

Selectors

The selector functions have to be renamed from starting with $ for obvious reasons. The renamed functions are as follows:

$ => select
$$ => selectMulti
$eval => eval
$$eval => evalMulti

These functions are present as part of the Page, Frame and ElementHandle classes.

Scripts

The evaluate() and evaluateHandle() functions can only be run in string mode. To maximize the usefulness of these, I have wrapped the string passed with the following function:

    const fun = new Function (toEval);
    args = [
        fun,
        ...args
    ];

As such you can effectively treat the script string as a function body. The same restriction on only being able to pass one arg remains from the upstream: https://playwright.dev/docs/api/class-page#pageevalselector-pagefunction-arg

You will have to refer to the arguments array as described here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

example of evaluate()

    # Read the console
    $page->on('console',"return [...arguments]");

    my $promise = $page->waitForEvent('console');
    #TODO This request can race, the server framework I use to host the playwright spec is *not* FIFO (YET)
    sleep 1;
    $page->evaluate("console.log('hug')");
    my $console_log = $handle->await( $promise );

    print "Logged to console: '".$console_log->text()."'\n";

Asynchronous operations

The waitFor* methods defined on various classes will return an instance of AsyncData, a part of the Async module. You will then need to wait on the result of the backgrounded action with the await() method documented below.

    # Assuming $handle is a Playwright object
    my $async = $page->waitForEvent('console');
    $page->evaluate('console.log("whee")');
    my $result = $handle->await( $async );
    my $logged = $result->text();

INSTALLATION NOTE

If you install this module from CPAN, you will likely encounter a croak() telling you to install node module dependencies. Follow the instructions and things should be just fine.

If you aren't, please file a bug!

CONSTRUCTOR

new(HASH) = (Playwright)

Creates a new browser and returns a handle to interact with it.

INPUT

    debug (BOOL) : Print extra messages from the Playwright server process
    timeout (INTEGER) : Seconds to wait for the playwright server to spin up and down.  Default: 30s

METHODS

launch(HASH) = Playwright::Browser

The Argument hash here is essentially those you'd see from browserType.launch(). See: https://playwright.dev/docs/api/class-browsertype#browsertypelaunchoptions

There is an additional "special" argument, that of 'type', which is used to specify what type of browser to use, e.g. 'firefox'.

await (AsyncData) = Object

Waits for an asynchronous operation returned by the waitFor* methods to complete and returns the value.

quit, DESTROY

Terminate the browser session and wait for the Playwright server to terminate.

Automatically called when the Playwright object goes out of scope.

BUGS

Please report any bugs or feature requests on the bugtracker website https://github.com/teodesian/playwright-perl/issues

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

AUTHORS

Current Maintainers:

  • George S. Baugh <teodesian@gmail.com>

COPYRIGHT AND LICENSE

Copyright (c) 2020 Troglodyne LLC

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.