Example #1
0
    /**
     * Drag one element onto another.
     *
     * @param   string  $sourceXpath
     * @param   string  $destinationXpath
     */
    public function dragTo($sourceXpath, $destinationXpath)
    {
        $source = $this->wdSession->element('xpath', $sourceXpath);
        $destination = $this->wdSession->element('xpath', $destinationXpath);
        $this->wdSession->moveto(array('element' => $source->getID()));
        $script = <<<JS
(function (element) {
    var event = document.createEvent("HTMLEvents");

    event.initEvent("dragstart", true, true);
    event.dataTransfer = {};

    element.dispatchEvent(event);
}({{ELEMENT}}));
JS;
        $this->withSyn()->executeJsOnXpath($sourceXpath, $script);
        $this->wdSession->buttondown();
        $this->wdSession->moveto(array('element' => $destination->getID()));
        $this->wdSession->buttonup();
        $script = <<<JS
(function (element) {
    var event = document.createEvent("HTMLEvents");

    event.initEvent("drop", true, true);
    event.dataTransfer = {};

    element.dispatchEvent(event);
}({{ELEMENT}}));
JS;
        $this->withSyn()->executeJsOnXpath($destinationXpath, $script);
    }
    /**
     * Selects a value in a radio button group
     *
     * @param Element $element An element referencing one of the radio buttons of the group
     * @param string  $value   The value to select
     *
     * @throws DriverException when the value cannot be found
     */
    private function selectRadioValue(Element $element, $value)
    {
        // short-circuit when we already have the right button of the group to avoid XPath queries
        if ($element->attribute('value') === $value) {
            $element->click();
            return;
        }
        $name = $element->attribute('name');
        if (!$name) {
            throw new DriverException(sprintf('The radio button does not have the value "%s"', $value));
        }
        $formId = $element->attribute('form');
        try {
            if (null !== $formId) {
                $xpath = <<<'XPATH'
//form[@id=%1$s]//input[@type="radio" and not(@form) and @name=%2$s and @value = %3$s]
|
//input[@type="radio" and @form=%1$s and @name=%2$s and @value = %3$s]
XPATH;
                $xpath = sprintf($xpath, $this->xpathEscaper->escapeLiteral($formId), $this->xpathEscaper->escapeLiteral($name), $this->xpathEscaper->escapeLiteral($value));
                $input = $this->wdSession->element('xpath', $xpath);
            } else {
                $xpath = sprintf('./ancestor::form//input[@type="radio" and not(@form) and @name=%s and @value = %s]', $this->xpathEscaper->escapeLiteral($name), $this->xpathEscaper->escapeLiteral($value));
                $input = $element->element('xpath', $xpath);
            }
        } catch (NoSuchElement $e) {
            $message = sprintf('The radio group "%s" does not have an option "%s"', $name, $value);
            throw new DriverException($message, 0, $e);
        }
        $input->click();
    }
Example #3
0
 /**
  * Find an element by its "value" attribute.
  *
  * @param  string $value
  * @param  string $element
  * @return \Session
  */
 protected function findByValue($value, $element = 'input')
 {
     try {
         return $this->session->element('css selector', "{$element}[value='{$value}']");
     } catch (NoSuchElement $e) {
         try {
             return $this->session->element('xpath', "//button[contains(text(),'{$value}')]");
         } catch (NoSuchElement $e) {
             throw new InvalidArgumentException("Crap. Couldn't find an {$element} with a 'value' attribute of '{$value}'. We also looked " . "for a button that contains the text, '{$value}', but no dice either.");
         }
     }
 }