/** * 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(); } } } } }
/** * @param array[] $allowedElements * Element machine names. */ private function restrictElements(array $allowedElements) { // Match element tag with allowed. if (!isset($allowedElements[$this->tag])) { throw new \RuntimeException("Tag is not allowed: {$this->tag}."); } $types = $allowedElements[$this->tag]; // Restrict by types only if they are specified. if (!empty($types)) { $type = $this->element->getAttribute('type'); if (!in_array($type, $types)) { throw new \RuntimeException(sprintf('Type "%s" is not allowed for "%s" tag', $type, $this->tag)); } } }
public function __construct(NodeElement $node, Session $session) { if (!$node->hasClass(self::NOTICE_CLASS)) { throw new \InvalidArgumentException(sprintf('Provided node does not have class %s', self::NOTICE_CLASS)); } parent::__construct($node->getXpath(), $session); }
/** * Returns the name * @param NodeElement $slide * @return string */ public function getNameProperty(NodeElement $slide) { $selectors = Helper::getRequiredSelectors($this, ['slideImage', 'slideLink', 'slideName']); $nameElement = $slide->find('css', $selectors['slideName']); $names = ['imageAlt' => $slide->find('css', $selectors['slideImage'])->getAttribute('alt'), 'linkTitle' => $slide->find('css', $selectors['slideLink'])->getAttribute('title'), 'name' => $nameElement->getText(), 'nameTitle' => $nameElement->getAttribute('title')]; return Helper::getUnique($names); }
public static function createFromNode(NodeElement $node) { $message = $node->find('css', '.noty_text')->getHtml(); //$x = $node->getOuterHtml(); $class = $node->getAttribute('class'); switch ($class) { case 'noty_alert': $type = 'alert'; break; case 'noty_warning': $type = 'warning'; break; case 'noty_error': $type = 'error'; break; case 'noty_information': $type = 'information'; break; case 'noty_success': $type = 'success'; break; default: $type = 'default'; } return new NotyMessage($node, $type, $message); }
/** * Helper function how to read the evaluation from the evaluation element * @param NodeElement $element * @return string */ protected function getEvaluation($element) { $evaluation = $element->getAttribute('content'); $evaluation = floatval($evaluation); $evaluation *= 2; return (string) $evaluation; }
/** * Creates Element instance based on existing NodeElement instance. * * @param NodeElement $node_element Node element. * @param IPageFactory $page_factory Page factory. * * @return static * @throws ElementException When page factory is missing. */ public static function fromNodeElement(NodeElement $node_element, IPageFactory $page_factory = null) { if (!isset($page_factory)) { throw new ElementException('Page factory is required to create this element', ElementException::TYPE_PAGE_FACTORY_REQUIRED); } $selenium_selector = array(How::XPATH => $node_element->getXpath()); return new static($selenium_selector, $page_factory); }
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 reverse(NodeElement $buttonNode, Button $buttonObj, Elements $elements = null) { if ($buttonNode->getTagName() === 'button') { $buttonObj->setNode($buttonNode); } else { throw new \LogicException('tag mismatch'); } $parents = $buttonObj->getParents(); $outer = $buttonNode->getOuterHtml(); $label = $buttonNode->getText(); if ($label) { $buttonObj->setLabel($label); } else { $x = 1; } //return; /* $elem = []; $id = $button->getAttribute('id'); $html = $button->getOuterHtml(); $test = '/<[a-z]+\s([^>]*)/'; if (preg_match($test, $html, $matches)) { preg_match_all('/([a-z]+=".*?")/', $matches[1], $attribs1); $attribs = preg_split('/ /', $matches[1], null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); $elem['attr'] = $attribs; } if ($button->getTagName() === 'a') { $href = $button->getAttribute('href'); $elem['href'] = $href; } if (!$id) { $dt = $content->find('css', 'table.table-columnfilter'); if ($dt) { //skip datatable; //continue; } $label = $button->getText(); $xpath = $button->getXpath(); $elem['xpath'] = $xpath; $elem['label'] = $label; } else { $label = $button->getText(); $elements['buttons'][] = ['id' => $id, 'label' => $label]; } $elements['buttons'][] = $elem;*/ }
/** * Helper function returns the data of an order position * @param NodeElement $position * @param string[] $selectors * @return array */ private function getOrderPositionData(NodeElement $position, array $selectors) { $data = []; foreach ($selectors as $key => $selector) { $element = $position->find('css', $selector); $data[$key] = $element->getText(); if ($key !== 'product') { $data[$key] = Helper::floatValue($data[$key]); } } return $data; }
/** * @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]; }
/** * @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); }
/** * @throws \Exception * * @return self */ public function isDateSelected() { $value = $this->datePicker(['getDate']); $initial = $this->execute($this->date); // By some reasons DatePicker could not return a date using "getDate" method // and we'll try to use it from input value directly. An issue could occur // after saving the form and/or reloading the page. if (empty($value)) { $value = $this->execute(self::jsDate($this->element->getValue())); } self::debug(['Comparing "%s" with "%s".'], [$value, $initial]); if ($value !== $initial) { throw new \Exception(sprintf('DatePicker contains the "%s" but should "%s".', $value, $initial)); } return $this; }
/** * @param NodeElement $element * * @return NodeElement|mixed|null */ protected function findCommentMessage(NodeElement $element) { return $element->find('css', 'span.message'); }
/** * @param NodeElement $modalContainer * @param string $appearClass * * @todo it really shouldn't be here :) */ protected function waitForModalToAppear(NodeElement $modalContainer, $appearClass = 'in') { $this->getDocument()->waitFor(1, function () use($modalContainer, $appearClass) { return false !== strpos($modalContainer->getAttribute('class'), $appearClass); }); }
/** * @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)); }
/** * @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]; }
/** * Returns the text preview of the blog article * @param NodeElement $article * @return null|string */ public function getTextProperty(NodeElement $article) { $selector = Helper::getRequiredSelector($this, 'articleText'); return $article->find('css', $selector)->getText(); }
/** * Returns the closest parent element having a specific class attribute. * * @param NodeElement $el * @param String $class * @return Element|null */ protected function findParentByClass(NodeElement $el, $class) { $container = $el->getParent(); while ($container && $container->getTagName() != 'body') { if ($container->isVisible() && in_array($class, explode(' ', $container->getAttribute('class')))) { return $container; } $container = $container->getParent(); } return null; }
/** * Recursive method to find the field type. * * Depending on the field the felement class node is in a level or in another. We * look recursively for a parent node with a 'felement' class to find the field type. * * @param NodeElement $fieldnode The current node. * @param Session $session The behat browser session * @return mixed A NodeElement if we continue looking for the element type and String or false when we are done. */ protected static function get_field_node_type(NodeElement $fieldnode, Session $session) { // Special handling for availability field which requires custom JavaScript. if ($fieldnode->getAttribute('name') === 'availabilityconditionsjson') { return 'availability'; } if ($fieldnode->getTagName() == 'html') { return false; } // If the type is explictly set on the element pointed to by the label - use it. if ($type = $fieldnode->getParent()->getAttribute('data-fieldtype')) { if ($type == 'tags') { return 'autocomplete'; } return $type; } // We look for a parent node with 'felement' class. if ($class = $fieldnode->getParent()->getAttribute('class')) { if (strstr($class, 'felement') != false) { // Remove 'felement f' from class value. return substr($class, 10); } // Stop propagation through the DOM, if it does not have a felement is not part of a moodle form. if (strstr($class, 'fcontainer') != false) { return false; } } return self::get_field_node_type($fieldnode->getParent(), $session); }
/** * Fills a text field element with $value, identified by its $label. * * @param NodeElement $label * @param string $value */ protected function fillTextField(NodeElement $label, $value) { if (!$label->getAttribute('for') && null !== $label->channel) { $label = $label->getParent()->find('css', sprintf('[data-scope="%s"] label', $label->channel)); } $for = $label->getAttribute('for'); $field = $this->find('css', sprintf('#%s', $for)); $field->setValue($value); }
/** * Open the filter * * @param NodeElement $filter * * @throws \InvalidArgumentException */ public function openFilter(NodeElement $filter) { if ($element = $filter->find('css', 'button')) { $element->click(); } else { throw new \InvalidArgumentException('Impossible to open filter or maybe its type is not yet implemented'); } }
/** * @param NodeElement $modalContainer */ protected function waitForModalToDisappear($modalContainer) { $this->waitFor(function () use($modalContainer) { return false === strpos($modalContainer->getAttribute('class'), 'in'); }); }
/** * @param NodeElement $tabContainer */ protected function waitForTabToActivate($tabContainer) { $this->waitFor(function () use($tabContainer) { return false !== strpos($tabContainer->getAttribute('class'), 'active'); }); }
/** * Ensures that the provided node is visible and we can interact with it. * * @throws ExpectationException * @param NodeElement $node * @return void Throws an exception if it times out without the element being visible */ protected function ensure_node_is_visible($node) { if (!$this->running_javascript()) { return; } // Exception if it timesout and the element is still there. $msg = 'The "' . $node->getXPath() . '" xpath node is not visible and it should be visible'; $exception = new ExpectationException($msg, $this->getSession()); // It will stop spinning once the isVisible() method returns true. $this->spin(function ($context, $args) { if ($args->isVisible()) { return true; } return false; }, $node, self::EXTENDED_TIMEOUT, $exception, true); }
/** * Creates Element instance based on existing NodeElement instance. * * @param NodeElement $node_element Node element. * @param IPageFactory $page_factory Page factory. * * @return static */ public static function fromNodeElement(NodeElement $node_element, IPageFactory $page_factory = null) { $selenium_selector = array(How::XPATH => $node_element->getXpath()); return new static($selenium_selector, $node_element->getSession()); }
/** * Check if the given node has the given text. * * @param NodeElement $element * @param string $text * * @throws \PHPUnit_Framework_AssertionFailedError */ private function assertNodeElementContainsText(NodeElement $element, $text) { $actual = $element->getText(); $regex = '/' . preg_quote($text, '/') . '/ui'; $message = sprintf('The text "%s" was not found in the text of the %s.', $text, $actual); PHPUnit::assertTrue((bool) preg_match($regex, $actual), $message); }
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))); }
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); } } } }
/** * Checks that the given element is of the given type. * * @param NodeElement $element * The element to check. * @param string $type * The expected type. * * @throws ExpectationException * Thrown when the given element is not of the expected type. */ public function assertElementType(NodeElement $element, $type) { if ($element->getTagName() !== $type) { throw new ExpectationException("The element is not a '{$type}'' field.", $this->getSession()); } }