Created
May 4, 2020 17:47
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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