/** * Submits a form located on page. * Specify the form by it's css or xpath selector. * Fill the form fields values as array. * * Skipped fields will be filled by their values from page. * You don't need to click the 'Submit' button afterwards. * This command itself triggers the request to form's action. * * Examples: * * ``` php * <?php * $I->submitForm('#login', array('login' => 'davert', 'password' => '123456')); * * ``` * * For sample Sign Up form: * * ``` html * <form action="/sign_up"> * Login: <input type="text" name="user[login]" /><br/> * Password: <input type="password" name="user[password]" /><br/> * Do you agree to out terms? <input type="checkbox" name="user[agree]" /><br/> * Select pricing plan <select name="plan"><option value="1">Free</option><option value="2" selected="selected">Paid</option></select> * <input type="submit" value="Submit" /> * </form> * ``` * I can write this: * * ``` php * <?php * $I->submitForm('#userForm', array('user' => array('login' => 'Davert', 'password' => '123456', 'agree' => true))); * * ``` * Note, that pricing plan will be set to Paid, as it's selected on page. * * @param $selector * @param $params */ public function submitForm($selector, $params) { $form = $this->browser->getResponseDomCssSelector()->matchSingle($selector)->getNode(); if (!$form) { \PHPUnit_Framework_Assert::fail("Form by selector '{$selector}' not found"); } $fields = $this->browser->getResponseDomCssSelector()->matchAll($selector . ' input')->getNodes(); $url = ''; foreach ($fields as $field) { if ($field->getAttribute('type') == 'checkbox') { continue; } if ($field->getAttribute('type') == 'radio') { continue; } $url .= sprintf('%s=%s', $field->getAttribute('name'), $field->getAttribute('value')) . '&'; } $fields = $this->browser->getResponseDomCssSelector()->matchAll($selector . ' textarea')->getNodes(); foreach ($fields as $field) { $url .= sprintf('%s=%s', $field->getAttribute('name'), $field->nodeValue) . '&'; } $fields = $this->browser->getResponseDomCssSelector()->matchAll($selector . ' select')->getNodes(); foreach ($fields as $field) { foreach ($field->childNodes as $option) { if ($option->getAttribute('selected') == 'selected') { $url .= sprintf('%s=%s', $field->getAttribute('name'), $option->getAttribute('value')) . '&'; break; } } } $url .= '&' . http_build_query($params); parse_str($url, $params); $url = $form->getAttribute('action'); $method = $form->getAttribute('method'); $this->call($url, $method, $params); }