Skip to content

Commit 8f7383d

Browse files
authored
Merge pull request #1 from SamMousa/patch-1
Support HTML5 form attribute
2 parents 3e5df13 + 83f2609 commit 8f7383d

File tree

7 files changed

+108
-24
lines changed

7 files changed

+108
-24
lines changed

.scrutinizer.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tools:
2+
external_code_coverage: true
3+
build:
4+
nodes:
5+
analysis:
6+
tests:
7+
override:
8+
- php-scrutinizer-run
9+
filter:
10+
excluded_paths:
11+
- "tests/_support/_generated"

.travis.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
language: php
2-
2+
stages:
3+
- test
4+
-
35
php:
46
- 5.6
57
- 7.0
68
- 7.1
79
- 7.2
810
- 7.3
9-
1011
# faster builds on new travis setup not using sudo
1112
sudo: false
1213

1314
install:
1415
- '[[ -z "$CI_USER_TOKEN" ]] || composer config github-oauth.github.com ${CI_USER_TOKEN};'
1516
- travis_retry composer self-update && composer --version
16-
- travis_retry composer update --prefer-dist --no-interaction
17-
17+
- travis_retry composer install --prefer-dist --no-interaction
18+
- phpenv config-rm xdebug.ini
19+
- if [[ "$TRAVIS_PHP_VERSION" == "7.3" ]]; then pecl install -f pcov; fi
20+
1821
script:
1922
- php ./vendor/bin/codecept run
23+
- if [[ "$TRAVIS_PHP_VERSION" == "7.3" ]]; then php ./vendor/bin/codecept run --coverage-xml; fi
24+
after_script:
25+
- if [[ "$TRAVIS_PHP_VERSION" == "7.3" ]]; then wget https://scrutinizer-ci.com/ocular.phar; php ocular.phar code-coverage:upload --format=php-clover tests/_output/coverage.xml; fi

codeception.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ paths:
55
support: tests/_support
66
envs: tests/_envs
77
actor_suffix: Tester
8+
coverage:
9+
enabled: true
10+
local: true
11+
include:
12+
- src/*.php

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,8 @@
3434
},
3535
"config": {
3636
"classmap-authoritative": true
37+
},
38+
"scripts": {
39+
"test": "codecept run --coverage-xml"
3740
}
3841
}

src/Codeception/Lib/InnerBrowser.php

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -439,29 +439,47 @@ protected function clickByLocator($link)
439439
*/
440440
private function clickButton(\DOMNode $node)
441441
{
442-
$formParams = [];
443-
$buttonName = (string)$node->getAttribute('name');
444-
$buttonValue = $node->getAttribute('value');
445-
446-
if ($buttonName !== '' && $buttonValue !== null) {
447-
$formParams = [$buttonName => $buttonValue];
442+
/**
443+
* First we check if the button is associated to a form.
444+
* It is associated to a form when it has a nonempty form
445+
*/
446+
$formAttribute = $node->attributes->getNamedItem('form');
447+
if (isset($formAttribute)) {
448+
$form = empty($formAttribute->nodeValue) ? null : $this->filterByCSS('#' . $formAttribute->nodeValue)->getNode(0);
449+
} else {
450+
// Check parents
451+
$currentNode = $node;
452+
$form = null;
453+
while ($currentNode->parentNode !== null) {
454+
$currentNode = $currentNode->parentNode;
455+
if ($currentNode->nodeName === 'form') {
456+
$form = $node;
457+
break;
458+
}
459+
}
448460
}
449461

450-
while ($node->parentNode !== null) {
451-
$node = $node->parentNode;
452-
if (!isset($node->tagName)) {
453-
// this is the top most node, it has no parent either
454-
break;
462+
if (isset($form)) {
463+
$buttonName = $node->getAttribute('name');
464+
if ($buttonName !== '') {
465+
$formParams = [$buttonName => $node->getAttribute('value')];
466+
} else {
467+
$formParams = [];
455468
}
456-
if ($node->tagName === 'a') {
457-
$this->openHrefFromDomNode($node);
458-
return true;
459-
} elseif ($node->tagName === 'form') {
460-
$this->proceedSubmitForm(
461-
new Crawler($node, $this->getAbsoluteUrlFor($this->_getCurrentUri()), $this->getBaseUrl()),
462-
$formParams
463-
);
464-
return true;
469+
$this->proceedSubmitForm(
470+
new Crawler($form, $this->getAbsoluteUrlFor($this->_getCurrentUri()), $this->getBaseUrl()),
471+
$formParams
472+
);
473+
return true;
474+
} else {
475+
// Check if the button is inside an anchor.
476+
$currentNode = $node;
477+
while ($currentNode->parentNode !== null) {
478+
$currentNode = $currentNode->parentNode;
479+
if ($currentNode->nodeName === 'a') {
480+
$this->openHrefFromDomNode($currentNode);
481+
return true;
482+
}
465483
}
466484
}
467485
throw new TestRuntimeException('Button is not inside a link or a form');

tests/data/app/view/form/button-not-in-form.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
<form action="/form/button" method="POST" id="form-id">
66
<input type="hidden" name="text" value="val" />
77
<button type="submit" name="btn0">Submit</button>
8+
<input id="button2" type="submit" form="form-nonexistent" value="Invalid form2" />
9+
<input type="submit" value="Should not submit" form="" />
810
</form>
911
</div>
1012

1113
<div>
1214
<input type="submit" form="form-id" value="Submit 2" />
15+
<input type="submit" value="Outside submit" />
16+
17+
<input id="button2" type="submit" form="form-nonexistent" value="Invalid form" />
1318
</div>
1419
</body>
1520
</html>

tests/unit/Codeception/Module/TestsForWeb.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,42 @@ public function testClickHashButton()
16481648
$this->module->seeCurrentUrlEquals('/form/anchor');
16491649
}
16501650

1651+
public function testClickButtonWithForm()
1652+
{
1653+
$this->module->amOnPage('/form/button-not-in-form');
1654+
$this->module->click('Submit 2');
1655+
$this->module->seeCurrentUrlEquals('/form/button');
1656+
}
1657+
1658+
public function testClickButtonWithEmptyForm()
1659+
{
1660+
$this->module->amOnPage('/form/button-not-in-form');
1661+
$this->expectException(\Codeception\Exception\TestRuntimeException::class);
1662+
$this->module->click('Should not submit');
1663+
$this->module->seeCurrentUrlEquals('/form/button-not-in-form');
1664+
}
1665+
1666+
public function testClickButtonNotInForm()
1667+
{
1668+
$this->module->amOnPage('/form/button-not-in-form');
1669+
$this->expectException(\Codeception\Exception\TestRuntimeException::class);
1670+
$this->module->click('Outside submit');
1671+
}
1672+
1673+
public function testClickButtonWithFormInvalidIdInside()
1674+
{
1675+
$this->module->amOnPage('/form/button-not-in-form');
1676+
$this->expectException(\Codeception\Exception\TestRuntimeException::class);
1677+
$this->module->click('Invalid form2');
1678+
}
1679+
1680+
public function testClickButtonWithFormInvalidIdOutside()
1681+
{
1682+
$this->module->amOnPage('/form/button-not-in-form');
1683+
$this->expectException(\Codeception\Exception\TestRuntimeException::class);
1684+
$this->module->click('Invalid form');
1685+
}
1686+
16511687
public function testSubmitHashForm()
16521688
{
16531689
$this->module->amOnPage('/form/anchor');

0 commit comments

Comments
 (0)