Skip to content
Merged
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
6 changes: 6 additions & 0 deletions src/Pages/CommonPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use HeadlessChromium\Exception\ElementNotFoundException;
use HeadlessChromium\Exception\OperationTimedOut;
use HeadlessChromium\Exception\TargetDestroyed;
use HeadlessChromium\Page as DomPage;
use PrestaFlow\Library\Exceptions\TimeoutException;
use PrestaFlow\Library\Expects\Expect;
Expand Down Expand Up @@ -824,6 +825,11 @@ public function elementIsVisible($selector, $timeout = 1000)
if (get_class($elem->dom()) instanceof \HeadlessChromium\Dom) {
return true;
}
} catch (TargetDestroyed $e) {
// The browser or the page went away (closed by another process,
// crashed). That is not "the element is missing": answering false
// would turn a lost browser into a failed assertion on the shop.
throw $e;
} catch (ElementNotFoundException | OperationTimedOut | Exception $e) {
return false;
}
Expand Down
56 changes: 56 additions & 0 deletions tests/Unit/Pages/VisibilityOnLostBrowserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace PrestaFlow\Tests\Unit\Pages;

use HeadlessChromium\Exception\OperationTimedOut;
use HeadlessChromium\Exception\TargetDestroyed;
use PHPUnit\Framework\TestCase;
use PrestaFlow\Library\Pages\FrontOfficePage;

/**
* isVisible() answers "is this element on the page?", not "is the browser
* still there?".
*
* When the Chrome a run drives is closed under it (another PrestaFlow process
* releasing a shared browser, a crash), chrome-php throws TargetDestroyed
* ("The session is destroyed."). elementIsVisible() used to catch every
* Exception and return false, so the lost browser read as a missing element:
* a suite asserting a block on the home page failed with "false must be the
* same as true", which looks like a real regression of the shop, instead of
* reporting the browser that went away.
*/
final class VisibilityOnLostBrowserTest extends TestCase
{
private function pageThrowing(\Throwable $error): FrontOfficePage
{
$fakePage = new class($error) {
public function __construct(private \Throwable $error) {}
public function waitUntilContainsElement($selector, $timeout = 30000)
{
throw $this->error;
}
};

return new class($fakePage) extends FrontOfficePage {
public function __construct(private $fakePage) {}
public function getPage() { return $this->fakePage; }
};
}

public function testAMissingElementIsNotVisible(): void
{
$page = $this->pageThrowing(new OperationTimedOut('Operation timed out after 1000ms.'));

$this->assertFalse($page->isVisible('#psflowdemo-block', 1000));
}

public function testALostBrowserIsReportedNotReadAsAMissingElement(): void
{
$page = $this->pageThrowing(new TargetDestroyed('The session is destroyed.'));

$this->expectException(TargetDestroyed::class);
$this->expectExceptionMessage('The session is destroyed.');

$page->isVisible('#psflowdemo-block', 1000);
}
}
Loading