Skip to content
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
45 changes: 42 additions & 3 deletions src/Codeception/Lib/InnerBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,10 @@ public function _loadPage(
array $parameters = [],
array $files = [],
array $server = [],
?string $content = null
?string $content = null,
bool $changeHistory = true
): void {
$this->crawler = $this->clientRequest($method, $uri, $parameters, $files, $server, $content);
$this->crawler = $this->clientRequest($method, $uri, $parameters, $files, $server, $content, $changeHistory);
$this->baseUrl = $this->retrieveBaseUrl();
$this->forms = [];
}
Expand Down Expand Up @@ -2006,7 +2007,45 @@ public function moveBack(int $numberOfSteps = 1): void
$request->getParameters(),
$request->getFiles(),
$request->getServer(),
$request->getContent()
$request->getContent(),
false
);
}

/**
* Moves forward in history.
*
* @param int $numberOfSteps (default value 1)
*/
public function moveForward(int $numberOfSteps = 1): void
{
$request = null;
if (!is_int($numberOfSteps) || $numberOfSteps < 1) {
throw new InvalidArgumentException('numberOfSteps must be positive integer');
}

try {
$history = $this->getRunningClient()->getHistory();
for ($i = $numberOfSteps; $i > 0; --$i) {
$request = $history->forward();
}
} catch (LogicException $exception) {
throw new InvalidArgumentException(
sprintf(
'numberOfSteps is set to %d, but there are only %d forward steps in the history',
$numberOfSteps,
$numberOfSteps - $i
), $exception->getCode(), $exception);
}

$this->_loadPage(
$request->getMethod(),
$request->getUri(),
$request->getParameters(),
$request->getFiles(),
$request->getServer(),
$request->getContent(),
false
);
}

Expand Down
55 changes: 55 additions & 0 deletions tests/unit/Codeception/Module/FrameworksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ public function testMoveBackTwoSteps()
$this->module->seeCurrentUrlEquals('/iframe');
}

public function testMoveBackPreservesForwardHistory()
{
$this->module->amOnPage('/iframe');
$this->module->amOnPage('/info');
$this->module->amOnPage('/');
$this->module->moveBack();
$this->module->seeCurrentUrlEquals('/info');
$this->module->moveBack();
$this->module->seeCurrentUrlEquals('/iframe');
}

public function testMoveBackThrowsExceptionIfNumberOfStepsIsInvalid()
{
$this->module->amOnPage('/iframe');
Expand All @@ -89,6 +100,50 @@ public function testMoveBackThrowsExceptionIfNumberOfStepsIsInvalid()
}
}

public function testMoveForwardOneStep()
{
$this->module->amOnPage('/iframe');
$this->module->amOnPage('/info');
$this->module->amOnPage('/');
$this->module->moveBack(2);
$this->module->seeCurrentUrlEquals('/iframe');
$this->module->moveForward();
$this->module->seeCurrentUrlEquals('/info');
$this->module->moveForward();
$this->module->seeCurrentUrlEquals('/');
}

public function testMoveForwardTwoSteps()
{
$this->module->amOnPage('/iframe');
$this->module->amOnPage('/info');
$this->module->amOnPage('/');
$this->module->moveBack(2);
$this->module->seeCurrentUrlEquals('/iframe');
$this->module->moveForward(2);
$this->module->seeCurrentUrlEquals('/');
}

public function testMoveForwardThrowsExceptionIfNumberOfStepsIsInvalid()
{
$this->module->amOnPage('/iframe');
$this->module->amOnPage('/');
$this->module->moveBack();
$this->module->seeCurrentUrlEquals('/iframe');

$invalidValues = [0, -5, 1.5, 'a', 3];
foreach ($invalidValues as $invalidValue) {
try {
$this->module->moveForward($invalidValue);
$this->fail('Expected to get exception here');
} catch (InvalidArgumentException $exception) {
codecept_debug('Exception: ' . $exception->getMessage());
} catch (TypeError $error) {
codecept_debug('Error: ' . $error->getMessage());
}
}
}

public function testCreateSnapshotOnFail()
{
$container = Stub::make(ModuleContainer::class);
Expand Down