Skip to content

Add form elements with form attribute to cloned Form #20

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 2 commits into from
Jan 2, 2021
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
12 changes: 12 additions & 0 deletions src/Codeception/Lib/InnerBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,18 @@ private function getFormFromCrawler(Crawler $form)
{
$fakeDom = new \DOMDocument();
$fakeDom->appendChild($fakeDom->importNode($form->getNode(0), true));

//add fields having form attribute with id of this form
$formId = $form->attr('id');
if ($formId !== null) {
$fakeForm = $fakeDom->firstChild;
$topParent = $form->parents()->last();
$fieldsByFormAttribute = $topParent->filter("input[form=$formId],select[form=$formId],textarea[form=$formId]");
foreach ($fieldsByFormAttribute as $field) {
$fakeForm->appendChild($fakeDom->importNode($field, true));
}
}

$node = $fakeDom->documentElement;
$action = (string)$this->getFormUrl($form);
$cloned = new Crawler($node, $action, $this->getBaseUrl());
Expand Down
16 changes: 16 additions & 0 deletions tests/data/app/view/form/input-not-in-form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html>
<body>
<div class="login-container2">
<form action="/form/button" id="form1" accept-charset="utf-8" method="post"></form>
<div class="form-element-row">
<button form="form1" name="btn[login]" class="primary small">Login</button>
</div>
<div class="form-element-row">
<input type="text" id="form1-username" name="username" placeholder="User code" form="form1"/>
</div>
<div class="form-element-row">
<input type="password" id="form1-password" name="password" placeholder="Password" form="form1"/>
</div>
</div>
</body>
</html>
11 changes: 11 additions & 0 deletions tests/unit/Codeception/Module/TestsForWeb.php
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,17 @@ public function testClickButtonWithFormInvalidIdOutside()
$this->module->click('Invalid form');
}

/**
* https://github.com/Codeception/Codeception/issues/6022
*/
public function testFillFieldNotInForm()
{
$this->module->amOnPage('/form/input-not-in-form');
$this->module->seeElement("form",["id" => "form1"]);
$this->module->seeElement("input",["name" => "username"]);
$this->module->fillField("username","usr-code");
}

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