Skip to content

Support HTML5 form attribute #1

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

Merged
merged 7 commits into from
Nov 21, 2019
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 .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
tools:
external_code_coverage: true
build:
nodes:
analysis:
tests:
override:
- php-scrutinizer-run
filter:
excluded_paths:
- "tests/_support/_generated"
14 changes: 10 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
language: php

stages:
- test
-
php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3

# faster builds on new travis setup not using sudo
sudo: false

install:
- '[[ -z "$CI_USER_TOKEN" ]] || composer config github-oauth.github.com ${CI_USER_TOKEN};'
- travis_retry composer self-update && composer --version
- travis_retry composer update --prefer-dist --no-interaction

- travis_retry composer install --prefer-dist --no-interaction
- phpenv config-rm xdebug.ini
- if [[ "$TRAVIS_PHP_VERSION" == "7.3" ]]; then pecl install -f pcov; fi

script:
- php ./vendor/bin/codecept run
- if [[ "$TRAVIS_PHP_VERSION" == "7.3" ]]; then php ./vendor/bin/codecept run --coverage-xml; fi
after_script:
- 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
5 changes: 5 additions & 0 deletions codeception.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ paths:
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
coverage:
enabled: true
local: true
include:
- src/*.php
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@
},
"config": {
"classmap-authoritative": true
},
"scripts": {
"test": "codecept run --coverage-xml"
}
}
58 changes: 38 additions & 20 deletions src/Codeception/Lib/InnerBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,29 +439,47 @@ protected function clickByLocator($link)
*/
private function clickButton(\DOMNode $node)
{
$formParams = [];
$buttonName = (string)$node->getAttribute('name');
$buttonValue = $node->getAttribute('value');

if ($buttonName !== '' && $buttonValue !== null) {
$formParams = [$buttonName => $buttonValue];
/**
* First we check if the button is associated to a form.
* It is associated to a form when it has a nonempty form
*/
$formAttribute = $node->attributes->getNamedItem('form');
if (isset($formAttribute)) {
$form = empty($formAttribute->nodeValue) ? null : $this->filterByCSS('#' . $formAttribute->nodeValue)->getNode(0);
} else {
// Check parents
$currentNode = $node;
$form = null;
while ($currentNode->parentNode !== null) {
$currentNode = $currentNode->parentNode;
if ($currentNode->nodeName === 'form') {
$form = $node;
break;
}
}
}

while ($node->parentNode !== null) {
$node = $node->parentNode;
if (!isset($node->tagName)) {
// this is the top most node, it has no parent either
break;
if (isset($form)) {
$buttonName = $node->getAttribute('name');
if ($buttonName !== '') {
$formParams = [$buttonName => $node->getAttribute('value')];
} else {
$formParams = [];
}
if ($node->tagName === 'a') {
$this->openHrefFromDomNode($node);
return true;
} elseif ($node->tagName === 'form') {
$this->proceedSubmitForm(
new Crawler($node, $this->getAbsoluteUrlFor($this->_getCurrentUri()), $this->getBaseUrl()),
$formParams
);
return true;
$this->proceedSubmitForm(
new Crawler($form, $this->getAbsoluteUrlFor($this->_getCurrentUri()), $this->getBaseUrl()),
$formParams
);
return true;
} else {
// Check if the button is inside an anchor.
$currentNode = $node;
while ($currentNode->parentNode !== null) {
$currentNode = $currentNode->parentNode;
if ($currentNode->nodeName === 'a') {
$this->openHrefFromDomNode($currentNode);
return true;
}
}
}
throw new TestRuntimeException('Button is not inside a link or a form');
Expand Down
5 changes: 5 additions & 0 deletions tests/data/app/view/form/button-not-in-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
<form action="/form/button" method="POST" id="form-id">
<input type="hidden" name="text" value="val" />
<button type="submit" name="btn0">Submit</button>
<input id="button2" type="submit" form="form-nonexistent" value="Invalid form2" />
<input type="submit" value="Should not submit" form="" />
</form>
</div>

<div>
<input type="submit" form="form-id" value="Submit 2" />
<input type="submit" value="Outside submit" />

<input id="button2" type="submit" form="form-nonexistent" value="Invalid form" />
</div>
</body>
</html>
36 changes: 36 additions & 0 deletions tests/unit/Codeception/Module/TestsForWeb.php
Original file line number Diff line number Diff line change
Expand Up @@ -1648,6 +1648,42 @@ public function testClickHashButton()
$this->module->seeCurrentUrlEquals('/form/anchor');
}

public function testClickButtonWithForm()
{
$this->module->amOnPage('/form/button-not-in-form');
$this->module->click('Submit 2');
$this->module->seeCurrentUrlEquals('/form/button');
}

public function testClickButtonWithEmptyForm()
{
$this->module->amOnPage('/form/button-not-in-form');
$this->expectException(\Codeception\Exception\TestRuntimeException::class);
$this->module->click('Should not submit');
$this->module->seeCurrentUrlEquals('/form/button-not-in-form');
}

public function testClickButtonNotInForm()
{
$this->module->amOnPage('/form/button-not-in-form');
$this->expectException(\Codeception\Exception\TestRuntimeException::class);
$this->module->click('Outside submit');
}

public function testClickButtonWithFormInvalidIdInside()
{
$this->module->amOnPage('/form/button-not-in-form');
$this->expectException(\Codeception\Exception\TestRuntimeException::class);
$this->module->click('Invalid form2');
}

public function testClickButtonWithFormInvalidIdOutside()
{
$this->module->amOnPage('/form/button-not-in-form');
$this->expectException(\Codeception\Exception\TestRuntimeException::class);
$this->module->click('Invalid form');
}

public function testSubmitHashForm()
{
$this->module->amOnPage('/form/anchor');
Expand Down