/** * This method allows to fill a compound field by passing the label in reversed order separated * with whitespaces. * * Example: * We have a field "$" embedded inside a "Price" field * We can call fillField('$ Price', 26) to set the "$" value of parent field "Price" * * @param string $field * @param string $value * @param Element $element */ public function fillField($field, $value, Element $element = null) { $label = $this->extractLabelElement($field, $element); $fieldType = $this->getFieldType($label); switch ($fieldType) { case 'multiSelect2': $this->fillMultiSelect2Field($label, $value); break; case 'simpleSelect2': $this->fillSelect2Field($label, $value); break; case 'datepicker': $this->fillDateField($label, $value); break; case 'select': $this->fillSelectField($label, $value); break; case 'wysiwyg': $this->fillWysiwygField($label, $value); break; case 'text': $this->fillTextField($label, $value); break; case 'compound': $this->fillCompoundField($label, $value); break; default: parent::fillField($label->labelContent, $value); break; } }
/** * This method allows to fill a compound field by passing the label in reversed order separated * with whitespaces. * * Example: * We have a field "$" embedded inside a "Price" field * We can call fillField('$ Price', 26) to set the "$" value of parent field "Price" * * @param string $labelContent * @param string $value * @param Element $element * * @return null */ public function fillField($labelContent, $value, Element $element = null) { $subLabelContent = null; if (false !== strpbrk($labelContent, '€$')) { if (false !== strpos($labelContent, ' ')) { list($subLabelContent, $labelContent) = explode(' ', $labelContent); } } if ($element) { $label = $element->find('css', sprintf('label:contains("%s")', $labelContent)); } else { $label = $this->find('css', sprintf('label:contains("%s")', $labelContent)); } if (null === $label) { return parent::fillField($labelContent, $value); } if ($label->hasAttribute('for')) { if (false === strpos($value, ',')) { $for = $label->getAttribute('for'); if (0 === strpos($for, 's2id_')) { // We are playing with a select2 widget if (null !== ($field = $label->getParent()->find('css', 'select'))) { return $field->selectOption($value); } // Maybe it's an ajax select2? if (null !== ($link = $label->getParent()->find('css', 'a.select2-choice'))) { $link->click(); // Wait for the ajax request to finish $this->getSession()->wait(5000, '!$.active'); // Select the value in the displayed dropdown if (null !== ($item = $this->find('css', sprintf('#select2-drop li:contains("%s")', $value)))) { return $item->click(); } } throw new \InvalidArgumentException(sprintf('Could not find select2 widget inside %s', $label->getParent()->getHtml())); } elseif (preg_match('/_date$/', $for)) { $this->getSession()->executeScript(sprintf("\$('#%s').val('%s').trigger('change');", $for, $value)); } else { $field = $this->find('css', sprintf('#%s', $for)); if ($field->getTagName() === 'select') { $field->selectOption($value); } else { if (strpos($field->getAttribute('class'), 'wysiwyg') !== false) { $this->getSession()->executeScript(sprintf("\$('#%s').val('%s');", $for, $value)); } else { $field->setValue($value); } } } } else { foreach (explode(',', $value) as $value) { $label->getParent()->find('css', 'input[type="text"]')->click(); $this->getSession()->wait(100000, "\$('div:contains(\"Searching\")').length == 0"); $option = $this->find('css', sprintf('li:contains("%s")', trim($value))); if (!$option) { throw new \InvalidArgumentException(sprintf('Could not find option "%s" for "%s"', trim($value), $label->getText())); } $option->click(); } } } else { if (!$subLabelContent) { throw new \InvalidArgumentException(sprintf('The "%s" field is compound but the sub label was not provided', $labelContent)); } // it is a compound field, so let's expand the values $this->expand($label); $field = $this->findPriceField($labelContent, $subLabelContent); $field->setValue($value); } }