Skip to content

Commit

Permalink
automatically capture source code on failure
Browse files Browse the repository at this point in the history
if an assertion has been made against the source code, capture the source code for analysis
  • Loading branch information
browner12 committed Sep 29, 2020
1 parent 3ca17f1 commit 10afe1e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Concerns/MakesAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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}]"
Expand All @@ -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}]"
Expand Down
19 changes: 19 additions & 0 deletions src/Concerns/ProvidesBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
*
Expand Down
24 changes: 24 additions & 0 deletions src/Console/DuskCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 10afe1e

Please sign in to comment.