Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.11] Add ConsoleMessageEntity::class to store console messages #431

Draft
wants to merge 1 commit into
base: 1.11
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,30 @@ try {
}
```

#### Get console messages

All console messages received since the last navigation can be fetched using the `getConsoleMessages` method. It returns a list of `ConsoleMessageEntity`, one for each response.

```php
// assuming that the page contains just a `console.log('test')` script
$consoleMessages = $page->getConsoleMessages();

// data of the first console message
$message = $consoleMessages[0];

$type = $message->type; // log
$value = $message->args[0]['value']; // test
$timestamp = $message->timestamp; // 1662829292
```

The `ConsoleMessageEntity` contains the following public properties:

| Property | Description |
| ----------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| `type` | Type of the call. |
| `args` | Call arguments, each containing a [RemoteObject](https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-RemoteObject). |
| `timestamp` | Call timestamp. |

#### Evaluate script on the page

Once the page has completed the navigation you can evaluate arbitrary script on this page:
Expand Down
96 changes: 96 additions & 0 deletions src/Entity/ConsoleMessageEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/*
* This file is part of Chrome PHP.
*
* (c) Soufiane Ghzal <[email protected]>
*
* For the full copyright and license information = '';
public const TYPE_please view the LICENSE
* file that was distributed with this source code.
*/

namespace HeadlessChromium\Entity;

/**
* Console message data.
*
* @see https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#event-consoleAPICalled
*/
class ConsoleMessageEntity
{
public const TYPE_log = 'log';
public const TYPE_debug = 'debug';
public const TYPE_info = 'info';
public const TYPE_error = 'error';
public const TYPE_warning = 'warning';
public const TYPE_dir = 'dir';
public const TYPE_dirxml = 'dirxml';
public const TYPE_table = 'table';
public const TYPE_trace = 'trace';
public const TYPE_clear = 'clear';
public const TYPE_startGroup = 'startGroup';
public const TYPE_startGroupCollapsed = 'startGroupCollapsed';
public const TYPE_endGroup = 'endGroup';
public const TYPE_assert = 'assert';
public const TYPE_profile = 'profile';
public const TYPE_profileEnd = 'profileEnd';
public const TYPE_count = 'count';
public const TYPE_timeEnd = 'timeEnd';

/**
* Type of the call.
*
* @var string
*/
public $type;

/**
* Call arguments, each containing a RemoteObject.
*
* `console.log('test')` will result in:
* ```
* [
* 0 => [
* 'type' => 'string',
* 'value => 'test',
* ],
* ]
* ```
*
* @see https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-RemoteObject
*
* @var array
*/
public $args;

/**
* Call timestamp.
*
* @var int
*/
public $timestamp;

/**
* @param ConsoleMessageEntity::TYPE_* $type
*/
public function __construct(
string $type,
array $args,
int $timestamp
) {
$this->type = $type;
$this->args = $args;
$this->timestamp = $timestamp;
}

/**
* Readonly trick for php < 8.1.
*
* @throws \InvalidArgumentException
*/
public function __set($name, $value): void
{
throw new \InvalidArgumentException('Entity properties are readonly.');
}
}
29 changes: 29 additions & 0 deletions src/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use HeadlessChromium\Cookies\CookiesCollection;
use HeadlessChromium\Dom\Dom;
use HeadlessChromium\Dom\Selector\CssSelector;
use HeadlessChromium\Entity\ConsoleMessageEntity;
use HeadlessChromium\Exception\CommunicationException;
use HeadlessChromium\Exception\InvalidTimezoneId;
use HeadlessChromium\Exception\JavascriptException;
Expand Down Expand Up @@ -59,6 +60,13 @@ class Page
*/
protected $keyboard;

/**
* Console messages since the last navigation.
*
* @var ConsoleMessageEntity[]
*/
private $consoleMessages = [];

/**
* Page constructor.
*
Expand All @@ -69,6 +77,17 @@ public function __construct(Target $target, array $frameTree)
{
$this->target = $target;
$this->frameManager = new FrameManager($this, $frameTree);

$this->getSession()->on(
'method:Runtime.consoleAPICalled',
function (array $message): void {
$this->consoleMessages[] = new ConsoleMessageEntity(
$message['type'],
$message['args'],
$message['timestamp']
);
}
);
}

/**
Expand Down Expand Up @@ -852,6 +871,16 @@ public function getCurrentUrl()
return $this->target->getTargetInfo('url');
}

/**
* Get the console messages from all requests since the last navigation.
*
* @return ConsoleMessageEntity[]
*/
public function getConsoleMessages(): array
{
return $this->consoleMessages;
}

/**
* Sets the raw html of the current page.
*
Expand Down
22 changes: 22 additions & 0 deletions tests/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use finfo;
use HeadlessChromium\BrowserFactory;
use HeadlessChromium\Entity\ConsoleMessageEntity;
use HeadlessChromium\Exception\InvalidTimezoneId;

/**
Expand Down Expand Up @@ -461,4 +462,25 @@ public function testFindTarget(): void
$target = $browser->findTarget('page', 'bigLayout.html');
$this->assertSame('bigLayout.html', $target->getTargetInfo('title'));
}

public function testGetConsoleMessages(): void
{
$factory = new BrowserFactory();

$browser = $factory->createBrowser();
$page = $browser->createPage();

$page->addPreScript('console.log("test")');
$page->navigate(self::sitePath('index.html'))->waitForNavigation();

$consoleMessages = $page->getConsoleMessages();
$this->assertCount(1, $consoleMessages);

$firstMessage = $consoleMessages[0];
$this->assertSame(ConsoleMessageEntity::TYPE_log, $firstMessage->type);
$this->assertCount(1, $firstMessage->args);
$this->assertSame('string', $firstMessage->args[0]['type']);
$this->assertSame('test', $firstMessage->args[0]['value']);
$this->assertGreaterThan(0, $firstMessage->timestamp);
}
}