Skip to content
Open
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
31 changes: 31 additions & 0 deletions src/Api/Concerns/HasRoutes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Pest\Browser\Api\Concerns;

use Pest\Browser\Api\Webpage;
use Pest\Browser\Playwright\Route;

/**
* @mixin Webpage
*/
trait HasRoutes
{
/**
* @param callable(Route): bool $handler
*/
public function route(
string $pattern,
callable $handler,
): void {
Route::registerRouteHandler($pattern, $handler);
$this->page->context()->setNetworkInterceptionPatterns([
'patterns' => [
[
'glob' => $pattern,
],
],
]);
}
}
2 changes: 2 additions & 0 deletions src/Api/From.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct(
private Device $device,
private string $url,
private array $options,
private array $routes,
) {
//
}
Expand Down Expand Up @@ -141,6 +142,7 @@ private function city(City $city): PendingAwaitablePage
$this->device,
$this->url,
$this->options,
$this->routes,
))
->geolocation($city->geolocation()['latitude'], $city->geolocation()['longitude'])
->withTimezone($city->timezone())
Expand Down
28 changes: 27 additions & 1 deletion src/Api/On.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function __construct(
private Device $device,
private string $url,
private array $options,
private array $routes,
) {
//
}
Expand All @@ -39,6 +40,7 @@ public function __call(string $name, array $arguments): mixed
$this->device,
$this->url,
$this->options,
$this->routes,
))->{$name}(...$arguments);
}

Expand All @@ -52,6 +54,7 @@ public function desktop(): self
Device::DESKTOP,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -65,6 +68,7 @@ public function mobile(): self
Device::MOBILE,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -78,6 +82,7 @@ public function macbook16(): self
Device::MACBOOK_16,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -91,6 +96,7 @@ public function macbook14(): self
Device::MACBOOK_14,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -104,6 +110,7 @@ public function macbookAir(): self
Device::MACBOOK_AIR,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -117,6 +124,7 @@ public function iPhone15Pro(): self
Device::IPHONE_15_PRO,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -130,6 +138,7 @@ public function iPhone15(): self
Device::IPHONE_15,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -143,6 +152,7 @@ public function iPhone14Pro(): self
Device::IPHONE_14_PRO,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -156,6 +166,7 @@ public function iPhoneSE(): self
Device::IPHONE_SE,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -169,6 +180,7 @@ public function iPadPro(): self
Device::IPAD_PRO,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -182,6 +194,7 @@ public function iPadMini(): self
Device::IPAD_MINI,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -195,6 +208,7 @@ public function pixel8(): self
Device::PIXEL_8,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -208,6 +222,7 @@ public function pixel7(): self
Device::PIXEL_7,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -221,6 +236,7 @@ public function pixel6a(): self
Device::PIXEL_6A,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -234,6 +250,7 @@ public function galaxyS24Ultra(): self
Device::GALAXY_S24_ULTRA,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -247,6 +264,7 @@ public function galaxyS23(): self
Device::GALAXY_S23,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -260,6 +278,7 @@ public function galaxyS22(): self
Device::GALAXY_S22,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -273,6 +292,7 @@ public function galaxyNote20(): self
Device::GALAXY_NOTE_20,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -286,6 +306,7 @@ public function galaxyTabS8(): self
Device::GALAXY_TAB_S8,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -299,6 +320,7 @@ public function surfacePro9(): self
Device::SURFACE_PRO_9,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -312,6 +334,7 @@ public function surfaceLaptop5(): self
Device::SURFACE_LAPTOP_5,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -325,6 +348,7 @@ public function oneplus11(): self
Device::ONEPLUS_11,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -337,7 +361,8 @@ public function xiaomi13(): self
$this->browserType,
Device::XIAOMI_13,
$this->url,
$this->options
$this->options,
$this->routes,
);
}

Expand All @@ -351,6 +376,7 @@ public function huaweiP50(): self
Device::HUAWEI_P50,
$this->url,
$this->options,
$this->routes,
);
}
}
46 changes: 37 additions & 9 deletions src/Api/PendingAwaitablePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function __construct(
private readonly Device $device,
private readonly string $url,
private readonly array $options,
private readonly array $routes = [],
) {
//
}
Expand All @@ -48,6 +49,23 @@ public function __call(string $name, array $arguments): mixed
return $this->waitablePage->{$name}(...$arguments);
}

/**
* @param callable(Route): bool $handler
*/
public function withRoute(string $pattern, callable $handler): self
{
return new self(
$this->browserType,
$this->device,
$this->url,
$this->options,
[
...$this->routes,
$pattern => $handler,
],
);
}

/**
* Sets the color scheme to dark mode.
*/
Expand All @@ -56,7 +74,7 @@ public function inDarkMode(): self
return new self($this->browserType, $this->device, $this->url, [
'colorScheme' => ColorScheme::DARK->value,
...$this->options,
]);
], $this->routes);
}

/**
Expand All @@ -67,7 +85,7 @@ public function inLightMode(): self
return new self($this->browserType, $this->device, $this->url, [
'colorScheme' => ColorScheme::LIGHT->value,
...$this->options,
]);
], $this->routes);
}

/**
Expand All @@ -80,6 +98,7 @@ public function from(): From
$this->device,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -93,6 +112,7 @@ public function on(): On
$this->device,
$this->url,
$this->options,
$this->routes,
);
}

Expand All @@ -104,7 +124,7 @@ public function withLocale(string $locale): self
return new self($this->browserType, $this->device, $this->url, [
'locale' => $locale,
...$this->options,
]);
], $this->routes);
}

/**
Expand All @@ -115,7 +135,7 @@ public function withUserAgent(string $userAgent): self
return new self($this->browserType, $this->device, $this->url, [
'userAgent' => $userAgent,
...$this->options,
]);
], $this->routes);
}

/**
Expand All @@ -126,7 +146,7 @@ public function withHost(string $host): self
return new self($this->browserType, $this->device, $this->url, [
'host' => $host,
...$this->options,
]);
], $this->routes);
}

/**
Expand All @@ -137,7 +157,7 @@ public function withTimezone(string $timezone): self
return new self($this->browserType, $this->device, $this->url, [
'timezoneId' => $timezone,
...$this->options,
]);
], $this->routes);
}

/**
Expand All @@ -151,7 +171,7 @@ public function geolocation(float $latitude, float $longitude): self
'geolocation' => $geolocation,
'permissions' => ['geolocation'],
...$this->options,
]);
], $this->routes);
}

/**
Expand Down Expand Up @@ -184,10 +204,18 @@ private function buildAwaitablePage(array $options): AwaitableWebpage

$url = ComputeUrl::from($this->url);

return new AwaitableWebpage(
$context->newPage()->goto($url, $options),
$awaitableWebPage = new AwaitableWebpage(
$context->newPage(),
$url,
);

foreach ($this->routes as $pattern => $handler) {
$awaitableWebPage->route($pattern, $handler);
}

$awaitableWebPage->page()->goto($url, $options);

return $awaitableWebPage;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Api/Webpage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

final readonly class Webpage
{
use Concerns\HasWaitCapabilities,
use Concerns\HasRoutes,
Concerns\HasWaitCapabilities,
Concerns\InteractsWithElements,
Concerns\InteractsWithFrames,
Concerns\InteractsWithScreen,
Expand Down
Loading