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
2 changes: 1 addition & 1 deletion src/Api/PendingAwaitablePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private function buildAwaitablePage(array $options): AwaitableWebpage
$browser = Playwright::browser($this->browserType)->launch();

$context = $browser->newContext([
'locale' => 'en-US',
'locale' => Playwright::defaultLocale(),
'timezoneId' => 'UTC',
'colorScheme' => Playwright::defaultColorScheme()->value,
...$this->device->context(),
Expand Down
10 changes: 10 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ public function withHost(?string $host): self
return $this;
}

/**
* Sets the default locale for the browser context.
*/
public function withLocale(string $locale): self
{
Playwright::setDefaultLocale($locale);

return $this;
}

/**
* Enables debug mode for assertions.
*/
Expand Down
21 changes: 21 additions & 0 deletions src/Playwright/Playwright.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ final class Playwright
*/
private static ?string $host = null;

/**
* The default locale.
*/
private static string $defaultLocale = 'en-US';

/**
* Get a browser factory for the given browser type.
*/
Expand Down Expand Up @@ -155,6 +160,22 @@ public static function host(): ?string
return self::$host;
}

/**
* Set the default locale.
*/
public static function setDefaultLocale(string $locale): void
{
self::$defaultLocale = $locale;
}

/**
* Get the default locale.
*/
public static function defaultLocale(): string
{
return self::$defaultLocale;
}

/**
* Get the default color scheme.
*/
Expand Down
50 changes: 50 additions & 0 deletions tests/Unit/Configuration/LocaleConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

use Pest\Browser\Configuration;
use Pest\Browser\Playwright\Playwright;

beforeEach(function (): void {
Playwright::setDefaultLocale('en-US');
});

it('can set locale via configuration', function (): void {
$config = new Configuration();

$result = $config->withLocale('fr-FR');

expect($result)->toBeInstanceOf(Configuration::class);
expect(Playwright::defaultLocale())->toBe('fr-FR');
});

it('follows fluent interface pattern', function (): void {
$config = new Configuration();

$result = $config
->withLocale('fr-FR')
->userAgent('Test Agent')
->timeout(10000);

expect($result)->toBeInstanceOf(Configuration::class);
expect(Playwright::defaultLocale())->toBe('fr-FR');
});

it('stores locale in Playwright global state', function (): void {
expect(Playwright::defaultLocale())->toBe('en-US');

Playwright::setDefaultLocale('ja-JP');

expect(Playwright::defaultLocale())->toBe('ja-JP');
});

it('can override locale multiple times', function (): void {
Playwright::setDefaultLocale('fr-FR');
expect(Playwright::defaultLocale())->toBe('fr-FR');

Playwright::setDefaultLocale('de-DE');
expect(Playwright::defaultLocale())->toBe('de-DE');

Playwright::setDefaultLocale('ja-JP');
expect(Playwright::defaultLocale())->toBe('ja-JP');
});