From 10afe1ea1741db219cfc44f597d6bfa510107624 Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Mon, 28 Sep 2020 20:58:14 -0500 Subject: [PATCH] automatically capture source code on failure if an assertion has been made against the source code, capture the source code for analysis --- src/Concerns/MakesAssertions.php | 11 +++++++++++ src/Concerns/ProvidesBrowser.php | 19 +++++++++++++++++++ src/Console/DuskCommand.php | 24 ++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/src/Concerns/MakesAssertions.php b/src/Concerns/MakesAssertions.php index b7f51f2c2..ebb5f6082 100644 --- a/src/Concerns/MakesAssertions.php +++ b/src/Concerns/MakesAssertions.php @@ -9,6 +9,13 @@ trait MakesAssertions { + /** + * Indicates the browser has made an assertion about the source code of the page. + * + * @var bool + */ + public $makesSourceAssertion = false; + /** * Assert that the page title is the given value. * @@ -205,6 +212,8 @@ public function assertDontSeeIn($selector, $text) */ public function assertSourceHas($code) { + $this->makesSourceAssertion = true; + PHPUnit::assertTrue( Str::contains($this->driver->getPageSource(), $code), "Did not find expected source code [{$code}]" @@ -221,6 +230,8 @@ public function assertSourceHas($code) */ public function assertSourceMissing($code) { + $this->makesSourceAssertion = true; + PHPUnit::assertFalse( Str::contains($this->driver->getPageSource(), $code), "Found unexpected source code [{$code}]" diff --git a/src/Concerns/ProvidesBrowser.php b/src/Concerns/ProvidesBrowser.php index d08ae5a45..62a3bf91e 100644 --- a/src/Concerns/ProvidesBrowser.php +++ b/src/Concerns/ProvidesBrowser.php @@ -68,10 +68,12 @@ public function browse(Closure $callback) $callback(...$browsers->all()); } catch (Exception $e) { $this->captureFailuresFor($browsers); + $this->storeSourceLogsFor($browsers); throw $e; } catch (Throwable $e) { $this->captureFailuresFor($browsers); + $this->storeSourceLogsFor($browsers); throw $e; } finally { @@ -162,6 +164,23 @@ protected function storeConsoleLogsFor($browsers) }); } + /** + * Store the source code for the given browsers. + * + * @param \Illuminate\Support\Collection $browsers + * @return void + */ + protected function storeSourceLogsFor($browsers) + { + $browsers->each(function ($browser, $key) { + if (property_exists($browser, 'makesSourceAssertion') && $browser->makesSourceAssertion) { + $name = $this->getCallerName(); + + $browser->storeSource($name.'-'.$key); + } + }); + } + /** * Close all of the browsers except the primary (first) one. * diff --git a/src/Console/DuskCommand.php b/src/Console/DuskCommand.php index df99e77d2..1d9dcfbc0 100644 --- a/src/Console/DuskCommand.php +++ b/src/Console/DuskCommand.php @@ -56,6 +56,8 @@ public function handle() $this->purgeConsoleLogs(); + $this->purgeSourceLogs(); + $options = array_slice($_SERVER['argv'], $this->option('without-tty') ? 3 : 2); return $this->withDuskEnvironment(function () use ($options) { @@ -158,6 +160,28 @@ protected function purgeConsoleLogs() } } + /** + * Purge the source logs. + * + * @return void + */ + protected function purgeSourceLogs() + { + $path = base_path('tests/Browser/source'); + + if (! is_dir($path)) { + return; + } + + $files = Finder::create()->files() + ->in($path) + ->name('*.txt'); + + foreach ($files as $file) { + @unlink($file->getRealPath()); + } + } + /** * Run the given callback with the Dusk configuration files. *