Skip to content

Instantly share code, notes, and snippets.

@mr-feek
Created May 4, 2020 17:47
Show Gist options
  • Save mr-feek/4863ac38610ff04eb9384439b88519e8 to your computer and use it in GitHub Desktop.
Save mr-feek/4863ac38610ff04eb9384439b88519e8 to your computer and use it in GitHub Desktop.
Laravel dusk browser macro for asserting that the text in an element has not changed from the previously set baseline
class DuskBrowserServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot(): void
{
Browser::macro('assertExpectedDomText', function (string $selector): Browser {
/** @var \Laravel\Dusk\Browser $browser */
$browser = $this;
$actualDom = $browser->element($selector)->getText();
$fileName = parse_url($browser->page->url())['path'] . $selector . '.txt';
$sourcesDirectoryPath = resource_path('/dusk/expected-dom');
if (!is_dir($sourcesDirectoryPath)) {
@mkdir($sourcesDirectoryPath, 0777, true);
}
$filePath = "{$sourcesDirectoryPath}/{$fileName}";
if (!file_exists($filePath)) {
file_put_contents($filePath, $actualDom, LOCK_EX);
}
$expected = file_get_contents($filePath);
Assert::assertSame($expected, $actualDom);
return $browser;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment