/** * @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)); } } }
/** * 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; }
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); }
/** * @param NodeElement $node */ public function setNode(NodeElement $node) { $this->node = $node; $this->setXpath($node->getXpath()); $this->visible = $node->isVisible(); $id = $node->getAttribute('id'); if ($id) { $this->setId($id); } $classes = $node->getAttribute('class'); if ($classes) { $this->classes = explode(' ', $classes); } $this->attributes = self::parseAttribs($node); }
/** * 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); }
public function testGetAttribute() { $expected = 'http://...'; $node = new NodeElement('input_tag', $this->session); $this->driver->expects($this->once())->method('getAttribute')->with('input_tag', 'href')->will($this->returnValue($expected)); $this->assertEquals($expected, $node->getAttribute('href')); }
/** * @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 $element * @return string */ protected function getStars(NodeElement $element) { return $element->getAttribute('class'); }
/** * @param NodeElement $column * * @return string */ private function getColumnFieldName(NodeElement $column) { return preg_replace('/.*sylius-table-column-([^ ]+).*$/', '\\1', $column->getAttribute('class')); }
/** * Find the special type of a mahara field * * We look for the class to detect the correct 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) { $specialfieldnodetypes = array('artefactchooser', 'authlist', 'autocomplete', 'calendar', 'color', 'emaillist', 'expiry', 'filebrowser', 'files', 'image', 'multitext', 'password', 'rolepermissions', 'tags', 'userlist', 'weight', 'wysiwyg'); if ($class = $fieldnode->getAttribute('class')) { if (in_array($class, $specialfieldnodetypes)) { return $class; } } return false; }
/** * 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'; } // 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); }
/** * @param NodeElement $tabContainer */ protected function waitForTabToActivate($tabContainer) { $this->waitFor(function () use($tabContainer) { return false !== strpos($tabContainer->getAttribute('class'), 'active'); }); }
/** * @param NodeElement $link * * @return bool|string */ private function filterLink(NodeElement $link) { $href = trim($link->getAttribute('href')); if (empty($href) || $href === '/' || 0 === strpos($href, '#') || 0 === strpos($href, 'javascript:') || 0 === strpos($href, 'mailto:') || !preg_match('/^((https?|ftp)\\:\\/\\/)(www\\.)?.*/', $href, $matches)) { return false; } // дополнить относительный урл базовым (из behat.yml) $part = substr($href, 0, 1); if ('/' == $part) { // с учётом страницы, на которой находимся $href = $this->getBehatParameter('base_url') . $href; } elseif ('?' == $part) { // с учётом страницы, на которой находимся $href = $this->getBehatParameter('base_url') . $this->_pageMapped . $href; } return $href; }
/** * @param NodeElement $modalContainer * * @throws \Exception */ protected function waitForModalToDisappear($modalContainer) { $i = 0; while (false !== strpos($modalContainer->getAttribute('class'), 'in')) { if (10 === $i) { throw new \Exception('The confirmation modal was not closed...'); } $this->getSession()->wait(100); ++$i; } }
/** * Fills a text field element with $value, identified by its $label. * * @param \Behat\Mink\Element\NodeElement $label * @param string $value */ protected function fillTextField($label, $value) { $for = $label->getAttribute('for'); $field = $this->find('css', sprintf('#%s', $for)); $field->setValue($value); }
/** * Fills a select2 multi-select field with $values, identified by its $label. * * @param NodeElement $fieldContainer * @param string $values */ protected function fillMultiSelectField(NodeElement $fieldContainer, $values) { // clear multi select first $containerClasses = $fieldContainer->getAttribute('class'); if (preg_match('/akeneo-multi-select(-reference-data)?-field/', $containerClasses, $matches)) { $select2Selector = sprintf('.%s div.field-input > input', $matches[0]); $script = sprintf('$("%s").select2("val", "");$("%1$s").trigger("change");', $select2Selector); $this->getSession()->executeScript($script); } $link = $fieldContainer->find('css', 'ul.select2-choices'); if (null === $link) { throw new \InvalidArgumentException(sprintf('Could not find select2 widget inside %s', $fieldContainer->getParent()->getHtml())); } foreach ($this->listToArray($values) as $value) { $link->click(); $this->getSession()->wait(1000); $item = $this->spin(function () use($value) { return $this->find('css', sprintf('.select2-drop li:contains("%s"):not(.select2-selected)', $value)); }); // Select the value in the displayed dropdown if (null !== $item) { $item->click(); } else { throw new \InvalidArgumentException(sprintf('Could not find select2 item with value %s inside %s', $value, $link->getHtml())); } } }
/** * 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); }
/** * Helper function how to read the evaluation from the evaluation element * @param NodeElement $element * @return string */ protected function getEvaluation($element) { return (string) $element->getAttribute('class'); }
/** * Guesses a basic field type and returns it. * * This method is intended to detect HTML form fields when no * moodleform-specific elements have been detected. * * @param NodeElement $fieldnode * @param Session $session * @return string|bool The field type or false. */ public static function guess_field_type(NodeElement $fieldnode, Session $session) { // Textareas are considered text based elements. $tagname = strtolower($fieldnode->getTagName()); if ($tagname == 'textarea') { // If there is an iframe with $id + _ifr there a TinyMCE editor loaded. $xpath = '//div[@id="' . $fieldnode->getAttribute('id') . 'editable"]'; if ($session->getPage()->find('xpath', $xpath)) { return 'editor'; } return 'textarea'; } else { if ($tagname == 'input') { $type = $fieldnode->getAttribute('type'); switch ($type) { case 'text': case 'password': case 'email': case 'file': return 'text'; case 'checkbox': return 'checkbox'; break; case 'radio': return 'radio'; break; default: // Here we return false because all text-based // fields should be included in the first switch case. return false; } } else { if ($tagname == 'select') { // Select tag. return 'select'; } } } // We can not provide a closer field type. return false; }
private function getAttributesFromElement(NodeElement $element) { $attr = array(); $attr['id'] = strtolower($element->getAttribute('id')); $attr['name'] = strtolower($element->getAttribute('name')); $attr['label'] = strtolower($element->getAttribute('label')); $attr['value'] = strtolower($element->getAttribute('value')); $attr['text'] = strtolower($element->getText()); $attr['title'] = strtolower($element->getAttribute('title')); return $attr; }
/** * @param NodeElement $modalContainer */ protected function waitForModalToDisappear($modalContainer) { $this->waitFor(function () use($modalContainer) { return false === strpos($modalContainer->getAttribute('class'), 'in'); }); }
/** * Verifies if the element has 'special' configuration on a attribute (default -> style) * * @param \Behat\Mink\Element\NodeElement $el The element that we want to test * @param string $characteristic Verify a specific characteristic from attribute * @param string $attribute Verify a specific attribute * * @return boolean */ public function isElementEmphasized(NodeElement $el, $characteristic = null, $attribute = "style") { // verify it has the attribute we're looking for if (!$el->hasAttribute($attribute)) { return false; } // get the attribute $attr = $el->getAttribute($attribute); // check if want to test specific characteristic and if it is present if (!empty($characteristic) && strpos($attr, $characteristic) === false) { return false; } // if we're here it is emphasized return true; }