Skip to content

Commit

Permalink
merged with 2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
DavertMik committed Jun 7, 2016
2 parents 31b67c2 + a9d8ff7 commit 587020d
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 21 deletions.
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ tests/data/included/jazz/pianist/tests/_log/*
tests/data/included/shire/tests/_log/*
tests/data/included_w/_log/*
tests/data/included_w/src/foo/AcmePack/tests/_log/*
tests/data/included_w/src/foo/AcmePack/tests/_support/_generated/*
tests/data/claypit/c3tmp/*'
tests/data/claypit/tests/_log/*
tests/data/claypit/tests/_output/*
tests/data/claypit/c3tmp

tests/data/included/jazz/pianist/tests/_helpers/_generated/TestGuyActions.php
tests/data/included/jazz/tests/_helpers/_generated/TestGuyActions.php
tests/data/included/shire/tests/_helpers/_generated/TestGuyActions.php
tests/data/included_w/src/bar/Sub/EwokPack/tests/_support/_generated/UnitTesterActions.php
tests/data/included_w/src/bar/ToastPack/tests/_support/_generated/UnitTesterActions.php
tests/data/included_w/src/foo/AcmePack/tests/_support/_generated/UnitTesterActions.php
tests/data/params/tests/_support/_generated/DummyTesterActions.php
.DS_Store
robo.phar
.env
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
* Deprecation errors won't fail tests but will be printed.
* Official [Docker image](https://hub.docker.com/r/codeception/codeception/) introduced by @schmunk42

#### 2.1.11

* [WebDriver] fixed URL matching in WebDriver::seeLink
* [WebDriver][InnerBrowser] Improved error messages of seeLink and dontSeeLink

#### 2.1.10

* PHPUnit version locked to <5.4
Expand Down
27 changes: 19 additions & 8 deletions src/Codeception/Lib/InnerBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,20 +427,30 @@ public function dontSeeInSource($raw)

public function seeLink($text, $url = null)
{
$links = $this->getCrawler()->selectLink($text);
$crawler = $this->getCrawler()->selectLink($text);
if ($crawler->count() === 0) {
$this->fail("No links containing text '$text' were found in page " . $this->_getCurrentUri());
}
if ($url) {
$links = $links->filterXPath(sprintf('.//a[contains(@href, %s)]', Crawler::xpathLiteral($url)));
$crawler = $crawler->filterXPath(sprintf('.//a[contains(@href, %s)]', Crawler::xpathLiteral($url)));
if ($crawler->count() === 0) {
$this->fail("No links containing text '$text' and URL '$url' were found in page " . $this->_getCurrentUri());
}
}
$this->assertDomContains($links, 'a');
}

public function dontSeeLink($text, $url = null)
{
$links = $this->getCrawler()->selectLink($text);
if ($url) {
$links = $links->filterXPath(sprintf('.//a[contains(@href, %s)]', Crawler::xpathLiteral($url)));
$crawler = $this->getCrawler()->selectLink($text);
if (!$url) {
if ($crawler->count() > 0) {
$this->fail("Link containing text '$text' was found in page " . $this->_getCurrentUri());
}
}
$crawler = $crawler->filterXPath(sprintf('.//a[contains(@href, %s)]', Crawler::xpathLiteral($url)));
if ($crawler->count() > 0) {
$this->fail("Link containing text '$text' and URL '$url' was found in page " . $this->_getCurrentUri());
}
$this->assertDomNotContains($links, 'a');
}

/**
Expand Down Expand Up @@ -1526,7 +1536,8 @@ protected function getFormPhpValues($requestParams)
protected function redirectIfNecessary($result, $maxRedirects, $redirectCount)
{
$locationHeader = $this->client->getInternalResponse()->getHeader('Location');
if ($locationHeader) {
$statusCode = $this->getResponseStatusCode();
if ($locationHeader && $statusCode >= 300 && $statusCode < 400) {
if ($redirectCount == $maxRedirects) {
throw new \LogicException(sprintf(
'The maximum number (%d) of redirections was reached.',
Expand Down
26 changes: 19 additions & 7 deletions src/Codeception/Module/WebDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -852,19 +852,29 @@ protected function findField($selector)
public function seeLink($text, $url = null)
{
$nodes = $this->webDriver->findElements(WebDriverBy::partialLinkText($text));
if (!$url) {
$this->assertNodesContain($text, $nodes, 'a');
return;
if (empty($nodes)) {
$this->fail("No links containing text '$text' were found in page " . $this->_getCurrentUri());
}
if ($url) {
$nodes = array_filter(
$nodes,
function (WebDriverElement $e) use ($url) {
return trim($e->getAttribute('href')) == trim($url);
}
);
if (empty($nodes)) {
$this->fail("No links containing text '$text' and URL '$url' were found in page " . $this->_getCurrentUri());
}
}
$this->assertNodesContain($text, $nodes, "a[href=$url]");
}


public function dontSeeLink($text, $url = null)
{
$nodes = $this->webDriver->findElements(WebDriverBy::partialLinkText($text));
if (!$url) {
$this->assertNodesNotContain($text, $nodes, 'a');
if (!empty($nodes)) {
$this->fail("Link containing text '$text' was found in page " . $this->_getCurrentUri());
}
return;
}
$nodes = array_filter(
Expand All @@ -873,7 +883,9 @@ function (WebDriverElement $e) use ($url) {
return trim($e->getAttribute('href')) == trim($url);
}
);
$this->assertNodesNotContain($text, $nodes, "a[href=$url]");
if (!empty($nodes)) {
$this->fail("Link containing text '$text' and URL '$url' was found in page " . $this->_getCurrentUri());
}
}

public function seeInCurrentUrl($uri)
Expand Down
6 changes: 6 additions & 0 deletions tests/data/app/controllers.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ function GET() {
}
}

class location_201 {
function GET() {
header('Location: /info', true, 201);
}
}

class external_url {
function GET() {
include __DIR__ . '/view/external_url.php';
Expand Down
1 change: 1 addition & 0 deletions tests/data/app/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
'/redirect_interval' => 'redirect_interval',
'/redirect_header_interval' => 'redirect_header_interval',
'/redirect_meta_refresh' => 'redirect_meta_refresh',
'/location_201' => 'location_201',
'/relative_redirect' => 'redirect_relative',
'/relative/redirect' => 'redirect_relative',
'/redirect_twice' => 'redirect_twice',
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/Codeception/Module/PhpBrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,13 @@ public function testRedirectLimitNotReached()
$this->module->seeCurrentUrlEquals('/info');
}

public function testLocationHeaderDoesNotRedirectWhenStatusCodeIs201()
{
$this->module->amOnPage('/location_201');
$this->module->seeResponseCodeIs(201);
$this->module->seeCurrentUrlEquals('/location_201');
}

public function testSetCookieByHeader()
{
$this->module->amOnPage('/cookies2');
Expand Down
47 changes: 42 additions & 5 deletions tests/unit/Codeception/Module/TestsForWeb.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,48 @@ public function testSeeInCurrentUrl()

public function testSeeLink()
{
$this->module->amOnPage('/');
$this->module->seeLink('More info');
$this->module->dontSeeLink('/info');
$this->module->dontSeeLink('#info');
$this->module->seeLink('More', '/info');
$this->module->amOnPage('/external_url');
$this->module->seeLink('Next');
$this->module->seeLink('Next', 'http://codeception.com/');
}

public function testDontSeeLink()
{
$this->module->amOnPage('/external_url');
$this->module->dontSeeLink('Back');
$this->module->dontSeeLink('Next', '/fsdfsdf/');
}

public function testSeeLinkFailsIfTextDoesNotMatch()
{
$this->setExpectedException('PHPUnit_Framework_AssertionFailedError',
"No links containing text 'Codeception' were found in page /external_url");
$this->module->amOnPage('/external_url');
$this->module->seeLink('Codeception');
}

public function testSeeLinkFailsIfHrefDoesNotMatch()
{
$this->setExpectedException('PHPUnit_Framework_AssertionFailedError',
"No links containing text 'Next' and URL '/fsdfsdf/' were found in page /external_url");
$this->module->amOnPage('/external_url');
$this->module->seeLink('Next', '/fsdfsdf/');
}

public function testDontSeeLinkFailsIfTextMatches()
{
$this->setExpectedException('PHPUnit_Framework_AssertionFailedError',
"Link containing text 'Next' was found in page /external_url");
$this->module->amOnPage('/external_url');
$this->module->dontSeeLink('Next');
}

public function testDontSeeLinkFailsIfTextAndUrlMatches()
{
$this->setExpectedException('PHPUnit_Framework_AssertionFailedError',
"Link containing text 'Next' and URL 'http://codeception.com/' was found in page /external_url");
$this->module->amOnPage('/external_url');
$this->module->dontSeeLink('Next', 'http://codeception.com/');
}

public function testClick()
Expand Down

0 comments on commit 587020d

Please sign in to comment.