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

[5.0] store the source of the browser #705

Closed
wants to merge 3 commits into from
Closed
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
26 changes: 26 additions & 0 deletions src/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class Browser
*/
public static $storeConsoleLogAt;

/**
* The directory that will contain any source files.
*
* @var string
*/
public static $storeSourceAt;

/**
* The browsers that support retrieving logs.
*
Expand Down Expand Up @@ -369,6 +376,25 @@ public function storeConsoleLog($name)
return $this;
}

/**
* Store the source with the given name.
*
* @param string $name
* @return $this
*/
public function storeSource($name)
{
$source = $this->driver->getPageSource();

if (! empty($source)) {
file_put_contents(
sprintf('%s/%s.txt', rtrim(static::$storeSourceAt, '/'), $name), $source
);
}

return $this;
}

/**
* Switch to a specified frame in the browser and execute the given callback.
*
Expand Down
16 changes: 16 additions & 0 deletions src/Concerns/ProvidesBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function browse(Closure $callback)
throw $e;
} finally {
$this->storeConsoleLogsFor($browsers);
$this->storeSourceFor($browsers);

static::$browsers = $this->closeAllButPrimary($browsers);
}
Expand Down Expand Up @@ -162,6 +163,21 @@ protected function storeConsoleLogsFor($browsers)
});
}

/**
* Store the source for the given browsers.
*
* @param \Illuminate\Support\Collection $browsers
* @return void
*/
protected function storeSourceFor($browsers)
{
$browsers->each(function ($browser, $key) {
$name = str_replace('\\', '_', get_class($this)).'_'.$this->getName(false);

$browser->storeSource($name.'-'.$key);
});
}

/**
* Close all of the browsers except the primary (first) one.
*
Expand Down
2 changes: 2 additions & 0 deletions src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ protected function setUp(): void

Browser::$storeConsoleLogAt = base_path('tests/Browser/console');

Browser::$storeSourceAt = base_path('tests/Browser/source');

Browser::$userResolver = function () {
return $this->user();
};
Expand Down
14 changes: 14 additions & 0 deletions tests/BrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,20 @@ public function test_screenshot_in_subdirectory()
$this->assertFileExists(Browser::$storeScreenshotsAt.'/'.$name.'.png');
}

public function test_source()
{
$this->driver->shouldReceive('getPageSource')->andReturn('source content');

Browser::$storeSourceAt = sys_get_temp_dir();

$this->browser->storeSource(
$name = 'screenshot-01'
);

$this->assertFileExists(Browser::$storeSourceAt.'/'.$name.'.txt');
$this->assertStringEqualsFile(Browser::$storeSourceAt.'/'.$name.'.txt', 'source content');
}

public function test_can_disable_fit_on_failure()
{
$this->browser->fitOnFailure = true;
Expand Down
11 changes: 11 additions & 0 deletions tests/ProvidesBrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ public function test_store_console_logs_for()
$this->storeConsoleLogsFor($browsers);
}

public function test_store_source_for()
{
$browser = m::mock(stdClass::class);
$browser->shouldReceive('storeSource')->with(
'Laravel_Dusk_Tests_ProvidesBrowserTest_test_store_source_for-0'
);
$browsers = collect([$browser]);

$this->storeSourceFor($browsers);
}

public function testData()
{
return [
Expand Down