Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] automatically capture source code on failure #819

Merged
merged 1 commit into from
Sep 30, 2020
Merged
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
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