Skip to content

Commit

Permalink
Capture page_ready state and current scenario in chrome logs
Browse files Browse the repository at this point in the history
Make it easier to then split them apart and explore the diff
between success and failure scenarios and see how any state
may chain between scenarios.
  • Loading branch information
acoulton committed Sep 15, 2022
1 parent fe5d95b commit a2d0749
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/ChromeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ public function reset()
STDOUT,
"ERROR: Could not reset - ".$e->getMessage()."\n".$e->getTraceAsString()
);
$l = new ChromeDriverDebugLogger;
$l->logDriverException($e, 'Failed to reset');
ChromeDriverDebugLogger::instance()->logDriverException($e, 'Failed to reset');
// What happens if we just allow the scenario to continue here????
}
}
Expand Down
43 changes: 40 additions & 3 deletions src/ChromeDriverDebugLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@

class ChromeDriverDebugLogger
{
private $scenario_id = 0;

private $page_ready = NULL;

public static function instance(): ChromeDriverDebugLogger
{
static $instance;
if ( ! $instance) {
$instance = new static;
}

return $instance;
}

public function __construct()
{
Expand Down Expand Up @@ -74,10 +87,12 @@ private function writeLog(array $vars)
}
$now = new \DateTimeImmutable;

$vars = array_merge(
$vars = array_merge(
[
'@' => ($now)->format('H:i:s.u'),
'+ms' => round(DateTimeDiff::microsBetween($last_logged, $now) / 1000, 3),
'@' => ($now)->format('H:i:s.u'),
'+ms' => round(DateTimeDiff::microsBetween($last_logged, $now) / 1000, 3),
'scenario' => $this->scenario_id,
'page_ready' => $this->page_ready,
],
$vars
);
Expand Down Expand Up @@ -113,6 +128,28 @@ public function logPageReadyStateChange(bool $state, string $change_trigger)
'trigger' => $change_trigger,
]
);
$this->page_ready = $state;
}

public function beginScenario(string $file, string $line)
{
$this->scenario_id++;
$this->writeLog(
[
'action' => 'beginScenario',
'scenarioName' => $file.':'.$line,
]
);
}

public function endScenario(bool $is_passed)
{
$this->writeLog(
[
'action' => 'endScenario',
'result' => $is_passed,
]
);
}

private function nameConnection(DevToolsConnection $connection)
Expand Down
2 changes: 1 addition & 1 deletion src/DevToolsConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct($url, $socket_timeout = null)
{
$this->url = $url;
$this->socket_timeout = $socket_timeout;
$this->logger = new ChromeDriverDebugLogger;
$this->logger = ChromeDriverDebugLogger::instance();
}

public function canDevToolsConnectionBeEstablished()
Expand Down

0 comments on commit a2d0749

Please sign in to comment.