Skip to content

Commit 956101b

Browse files
authored
Merge pull request #20 from Codeception/issue-6022
Add form elements with form attribute to cloned Form
2 parents 3e03a13 + a9ee9ea commit 956101b

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/Codeception/Lib/InnerBrowser.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,18 @@ private function getFormFromCrawler(Crawler $form)
10031003
{
10041004
$fakeDom = new \DOMDocument();
10051005
$fakeDom->appendChild($fakeDom->importNode($form->getNode(0), true));
1006+
1007+
//add fields having form attribute with id of this form
1008+
$formId = $form->attr('id');
1009+
if ($formId !== null) {
1010+
$fakeForm = $fakeDom->firstChild;
1011+
$topParent = $form->parents()->last();
1012+
$fieldsByFormAttribute = $topParent->filter("input[form=$formId],select[form=$formId],textarea[form=$formId]");
1013+
foreach ($fieldsByFormAttribute as $field) {
1014+
$fakeForm->appendChild($fakeDom->importNode($field, true));
1015+
}
1016+
}
1017+
10061018
$node = $fakeDom->documentElement;
10071019
$action = (string)$this->getFormUrl($form);
10081020
$cloned = new Crawler($node, $action, $this->getBaseUrl());
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<html>
2+
<body>
3+
<div class="login-container2">
4+
<form action="/form/button" id="form1" accept-charset="utf-8" method="post"></form>
5+
<div class="form-element-row">
6+
<button form="form1" name="btn[login]" class="primary small">Login</button>
7+
</div>
8+
<div class="form-element-row">
9+
<input type="text" id="form1-username" name="username" placeholder="User code" form="form1"/>
10+
</div>
11+
<div class="form-element-row">
12+
<input type="password" id="form1-password" name="password" placeholder="Password" form="form1"/>
13+
</div>
14+
</div>
15+
</body>
16+
</html>

tests/unit/Codeception/Module/TestsForWeb.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,6 +1684,17 @@ public function testClickButtonWithFormInvalidIdOutside()
16841684
$this->module->click('Invalid form');
16851685
}
16861686

1687+
/**
1688+
* https://github.com/Codeception/Codeception/issues/6022
1689+
*/
1690+
public function testFillFieldNotInForm()
1691+
{
1692+
$this->module->amOnPage('/form/input-not-in-form');
1693+
$this->module->seeElement("form",["id" => "form1"]);
1694+
$this->module->seeElement("input",["name" => "username"]);
1695+
$this->module->fillField("username","usr-code");
1696+
}
1697+
16871698
public function testSubmitHashForm()
16881699
{
16891700
$this->module->amOnPage('/form/anchor');

0 commit comments

Comments
 (0)