/** * Fuzz fill all form fields with random data * * @param Element\NodeElement $form */ protected function fillForm(Element\NodeElement $form) { $inputs = $form->findAll('css', 'input[type="text"]'); foreach ($inputs as $i) { /** @var Element\NodeElement $i */ if ($i->isVisible()) { if ($i->hasAttribute('name')) { $name = $i->getAttribute('name'); $value = $this->getFakerValue($name); } else { $value = $this->faker()->text(); } $i->setValue($value); } } $passwords = $form->findAll('css', 'input[type="password"]'); $password = $this->faker()->password; foreach ($passwords as $p) { if ($p->isVisible()) { $p->setValue($password); } } $selects = $form->findAll('css', 'select'); foreach ($selects as $s) { /** @var Element\NodeElement $s */ if ($s->isVisible()) { $s->selectOption($s->find('css', 'option')->getAttribute('name')); } } $checkboxes = $form->findAll('css', 'checkbox'); foreach ($checkboxes as $c) { /** @var Element\NodeElement $c */ if ($c->isVisible()) { $c->check(); } } $radios = $form->findAll('css', 'input[type="radio"]'); $radio_names = array(); foreach ($radios as $r) { /** @var Element\NodeElement $r */ if ($r->isVisible()) { if ($r->hasAttribute('name')) { $name = $r->getAttribute('name'); if (!isset($radio_names[$name])) { $radio_names[$name] = true; $r->click(); } } } } }
/** * @return NodeElement[] */ public function findAll() { if (null === $this->parent) { throw new \RuntimeException(sprintf('An object "%s" instantiated incorrectly.', __CLASS__)); } return $this->parent->findAll('xpath', (string) $this); }
public function parse(NodeElement $node) { $this->node = $node; $elems = $node->findAll('css', 'li'); foreach ($elems as $elem) { $notyMessage = NotyMessage::createFromNode($elem); $this->messages[] = $notyMessage; } }
public static function reverseAll(NodeElement $content, Elements $elements = null) { $inputsElements = $content->findAll('css', 'input,select,textarea'); if ($inputsElements) { foreach ($inputsElements as $inputsElement) { $input = self::reverse($inputsElement, $elements); if ($input) { $elements->addInput($input); } } } }
public static function reverseAll(NodeElement $content, Elements $elements) { $buttonElements = $content->findAll('css', 'button'); if ($buttonElements) { foreach ($buttonElements as $buttonElement) { $button = new Button(); self::reverse($buttonElement, $button, $elements); if (!$elements->isRegisteredButton($button)) { $elements->addButton($button); } } } }
/** * @param NodeElement $scopeBlock * * @return array */ protected function getScopeData(NodeElement $scopeBlock) { $ratio = $scopeBlock ? $scopeBlock->find('css', '.literal-progress')->getHtml() : ''; $state = $scopeBlock ? explode('-', $scopeBlock->find('css', '.progress')->getAttribute('class'))[1] : ''; $missingValuesBlocks = $scopeBlock ? $scopeBlock->findAll('css', '.missing-attributes [data-attribute]') : []; $missingValues = []; if (!empty($missingValuesBlocks)) { foreach ($missingValuesBlocks as $missingValuesBlock) { $attributeCode = $missingValuesBlock->getAttribute('data-attribute'); $attributeLabel = $missingValuesBlock->getHtml(); $missingValues[$attributeCode] = $attributeLabel; } } return ['ratio' => $ratio, 'state' => $state, 'missing_values' => $missingValues]; }
private static function reverseActions(NodeElement $header, Header $head) { $actions = new Actions($head->getDt()); $buttonsElems = $header->findAll('css', 'button'); if ($buttonsElems) { /** @var NodeElement $element */ foreach ($buttonsElems as $buttonElement) { $button = new Button(); self::reverseButton($buttonElement, $button, self::$elements); if (!self::$elements->isRegisteredButton($button)) { $actions->addButton($button); } } } $inputElems = $header->findAll('css', 'input,select,textarea'); if ($inputElems) { /** @var NodeElement $element */ foreach ($inputElems as $inputElement) { $input = self::reverseInput($inputElement, self::$elements); if ($input) { $actions->addInput($input); } } } }
/** * @param NodeElement $row * @param int $position * * @return NodeElement */ protected function getRowCell($row, $position) { $cells = $row->findAll('xpath', '/td'); $visibleCells = []; foreach ($cells as $cell) { $style = $cell->getAttribute('style'); if (!$style || !preg_match('/display: ?none;/', $style)) { $visibleCells[] = $cell; } } $cells = $visibleCells; if (!isset($cells[$position])) { throw new \InvalidArgumentException(sprintf('Trying to access cell %d of a row which has %d cell(s).', $position + 1, count($cells))); } return $cells[$position]; }
/** * @param NodeElement $row * @param int $position * * @throws \InvalidArgumentException * * @return NodeElement */ protected function getRowCell($row, $position) { // $row->findAll('css', 'td') will not work in the case of nested table (like proposals changes) // because we only need to find the direct children cells $cells = $row->findAll('xpath', './td'); if (!isset($cells[$position])) { throw new \InvalidArgumentException(sprintf('Trying to access cell %d of a row which has %d cell(s).', $position + 1, count($cells))); } return $cells[$position]; }
/** * @param NodeElement $table * @param string $columnName * * @return int * * @throws \Exception If column was not found */ private function getColumnIndex(NodeElement $table, $columnName) { $rows = $table->findAll('css', 'tr'); if (!isset($rows[0])) { throw new \InvalidArgumentException('There are no rows!'); } /** @var NodeElement $firstRow */ $firstRow = $rows[0]; $columns = $firstRow->findAll('css', 'th,td'); foreach ($columns as $index => $column) { /** @var NodeElement $column */ if (0 === stripos($column->getText(), $columnName)) { return $index; } } throw new \InvalidArgumentException(sprintf('Column with name "%s" not found!', $columnName)); }
public function testFindAllParentUnion() { $node = new NodeElement('some_xpath | another_xpath', $this->session); $xpath = "some_tag1 | some_tag2"; $expectedPrefixed = "(some_xpath | another_xpath)/some_tag1 | (some_xpath | another_xpath)/some_tag2"; $this->driver->expects($this->exactly(1))->method('find')->will($this->returnValueMap(array(array($expectedPrefixed, array(2, 3, 4))))); $this->selectors->expects($this->exactly(1))->method('selectorToXpath')->will($this->returnValueMap(array(array('xpath', $xpath, $xpath)))); $this->assertEquals(3, count($node->findAll('xpath', $xpath))); }
/** * @param NodeElement $table * @param array $fields * @param bool $onlyFirstOccurence * * @return NodeElement[] * * @throws \Exception If columns or rows were not found */ protected function getRowsWithFields(NodeElement $table, array $fields, $onlyFirstOccurence = false) { $rows = $table->findAll('css', 'tr'); if (!isset($rows[0])) { throw new \Exception('There are no rows!'); } $fields = $this->replaceColumnNamesWithColumnIds($table, $fields); $foundRows = []; /** @var NodeElement[] $rows */ $rows = $table->findAll('css', 'tr'); foreach ($rows as $row) { $found = true; /** @var NodeElement[] $columns */ $columns = $row->findAll('css', 'th,td'); foreach ($fields as $index => $searchedValue) { if (!isset($columns[$index])) { throw new \InvalidArgumentException(sprintf('There is no column with index %d', $index)); } $containing = false; $searchedValue = trim($searchedValue); if (0 === strpos($searchedValue, '%') && strlen($searchedValue) - 1 === strrpos($searchedValue, '%')) { $searchedValue = substr($searchedValue, 1, strlen($searchedValue) - 2); $containing = true; } $position = stripos(trim($columns[$index]->getText()), $searchedValue); if ($containing && false === $position || !$containing && 0 !== $position) { $found = false; break; } } if ($found) { $foundRows[] = $row; if ($onlyFirstOccurence) { break; } } } return $foundRows; }
/** * @param NodeElement $table * @param string $fieldName * * @return int * * @throws \InvalidArgumentException */ private function getColumnIndex(NodeElement $table, $fieldName) { $rows = $table->findAll('css', 'tr'); Assert::notEmpty($rows, 'There are no rows!'); /** @var NodeElement $headerRow */ $headerRow = $rows[0]; $headers = $headerRow->findAll('css', 'th,td'); /** @var NodeElement $column */ foreach ($headers as $index => $column) { $columnFieldName = $this->getColumnFieldName($column); if ($fieldName === $columnFieldName) { return $index; } } throw new \InvalidArgumentException(sprintf('Column with name "%s" not found!', $fieldName)); }
/** * @param NodeElement $table * @param array $fields * @param boolean $onlyFirstOccurence * * @return NodeElement[] * * @throws \Exception If columns or rows were not found */ protected function getRowsWithFields(NodeElement $table, array $fields, $onlyFirstOccurence = false) { $rows = $table->findAll('css', 'tr'); if (!isset($rows[0])) { throw new \Exception("There are no rows!"); } $fields = $this->replaceColumnNamesWithColumnIds($table, $fields); $foundRows = array(); /** @var NodeElement[] $rows */ $rows = $table->findAll('css', 'tr'); foreach ($rows as $row) { $found = true; /** @var NodeElement[] $columns */ $columns = $row->findAll('css', 'th,td'); foreach ($fields as $index => $searchedValue) { if (!isset($columns[$index])) { throw new \InvalidArgumentException(sprintf('There is no column with index %d', $index)); } if (0 !== stripos(trim($columns[$index]->getText()), trim($searchedValue))) { $found = false; break; } } if ($found) { $foundRows[] = $row; if ($onlyFirstOccurence) { break; } } } return $foundRows; }
/** * @Given /^("[^"]*" collection) should have (\d+) elements$/ */ public function collectionShouldHaveElements(NodeElement $collection, $elementsCount) { $elements = $collection->findAll('xpath', '/*/*[@class = "form-group"]'); expect(count($elements))->toBe($elementsCount); }
/** * Finds all elements with specified selector. * * @param string $selector Selector engine name. * @param string|array $locator Selector locator. * * @return NodeElement[] */ public function findAll($selector, $locator) { return $this->_wrappedElement->findAll($selector, $locator); }