| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 | <?php// Copyright 2004-present Facebook. All Rights Reserved.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at////     http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.namespace Facebook\WebDriver;use Facebook\WebDriver\Exception\NoSuchElementException;use Facebook\WebDriver\Exception\UnexpectedTagNameException;use Facebook\WebDriver\Exception\UnsupportedOperationException;use Facebook\WebDriver\Support\XPathEscaper;/** * Models a default HTML `<select>` tag, providing helper methods to select and deselect options. */class WebDriverSelect implements WebDriverSelectInterface{    /** @var WebDriverElement */    private $element;    /** @var bool */    private $isMulti;    public function __construct(WebDriverElement $element)    {        $tag_name = $element->getTagName();        if ($tag_name !== 'select') {            throw new UnexpectedTagNameException('select', $tag_name);        }        $this->element = $element;        $value = $element->getAttribute('multiple');        $this->isMulti = $value === 'true';    }    public function isMultiple()    {        return $this->isMulti;    }    public function getOptions()    {        return $this->element->findElements(WebDriverBy::tagName('option'));    }    public function getAllSelectedOptions()    {        $selected_options = [];        foreach ($this->getOptions() as $option) {            if ($option->isSelected()) {                $selected_options[] = $option;                if (!$this->isMultiple()) {                    return $selected_options;                }            }        }        return $selected_options;    }    public function getFirstSelectedOption()    {        foreach ($this->getOptions() as $option) {            if ($option->isSelected()) {                return $option;            }        }        throw new NoSuchElementException('No options are selected');    }    public function selectByIndex($index)    {        foreach ($this->getOptions() as $option) {            if ($option->getAttribute('index') === (string) $index) {                $this->selectOption($option);                return;            }        }        throw new NoSuchElementException(sprintf('Cannot locate option with index: %d', $index));    }    public function selectByValue($value)    {        $matched = false;        $xpath = './/option[@value = ' . XPathEscaper::escapeQuotes($value) . ']';        $options = $this->element->findElements(WebDriverBy::xpath($xpath));        foreach ($options as $option) {            $this->selectOption($option);            if (!$this->isMultiple()) {                return;            }            $matched = true;        }        if (!$matched) {            throw new NoSuchElementException(                sprintf('Cannot locate option with value: %s', $value)            );        }    }    public function selectByVisibleText($text)    {        $matched = false;        $xpath = './/option[normalize-space(.) = ' . XPathEscaper::escapeQuotes($text) . ']';        $options = $this->element->findElements(WebDriverBy::xpath($xpath));        foreach ($options as $option) {            $this->selectOption($option);            if (!$this->isMultiple()) {                return;            }            $matched = true;        }        // Since the mechanism of getting the text in xpath is not the same as        // webdriver, use the expensive getText() to check if nothing is matched.        if (!$matched) {            foreach ($this->getOptions() as $option) {                if ($option->getText() === $text) {                    $this->selectOption($option);                    if (!$this->isMultiple()) {                        return;                    }                    $matched = true;                }            }        }        if (!$matched) {            throw new NoSuchElementException(                sprintf('Cannot locate option with text: %s', $text)            );        }    }    public function selectByVisiblePartialText($text)    {        $matched = false;        $xpath = './/option[contains(normalize-space(.), ' . XPathEscaper::escapeQuotes($text) . ')]';        $options = $this->element->findElements(WebDriverBy::xpath($xpath));        foreach ($options as $option) {            $this->selectOption($option);            if (!$this->isMultiple()) {                return;            }            $matched = true;        }        if (!$matched) {            throw new NoSuchElementException(                sprintf('Cannot locate option with text: %s', $text)            );        }    }    public function deselectAll()    {        if (!$this->isMultiple()) {            throw new UnsupportedOperationException('You may only deselect all options of a multi-select');        }        foreach ($this->getOptions() as $option) {            $this->deselectOption($option);        }    }    public function deselectByIndex($index)    {        if (!$this->isMultiple()) {            throw new UnsupportedOperationException('You may only deselect options of a multi-select');        }        foreach ($this->getOptions() as $option) {            if ($option->getAttribute('index') === (string) $index) {                $this->deselectOption($option);                return;            }        }    }    public function deselectByValue($value)    {        if (!$this->isMultiple()) {            throw new UnsupportedOperationException('You may only deselect options of a multi-select');        }        $xpath = './/option[@value = ' . XPathEscaper::escapeQuotes($value) . ']';        $options = $this->element->findElements(WebDriverBy::xpath($xpath));        foreach ($options as $option) {            $this->deselectOption($option);        }    }    public function deselectByVisibleText($text)    {        if (!$this->isMultiple()) {            throw new UnsupportedOperationException('You may only deselect options of a multi-select');        }        $xpath = './/option[normalize-space(.) = ' . XPathEscaper::escapeQuotes($text) . ']';        $options = $this->element->findElements(WebDriverBy::xpath($xpath));        foreach ($options as $option) {            $this->deselectOption($option);        }    }    public function deselectByVisiblePartialText($text)    {        if (!$this->isMultiple()) {            throw new UnsupportedOperationException('You may only deselect options of a multi-select');        }        $xpath = './/option[contains(normalize-space(.), ' . XPathEscaper::escapeQuotes($text) . ')]';        $options = $this->element->findElements(WebDriverBy::xpath($xpath));        foreach ($options as $option) {            $this->deselectOption($option);        }    }    /**     * Mark option selected     * @param WebDriverElement $option     */    protected function selectOption(WebDriverElement $option)    {        if (!$option->isSelected()) {            $option->click();        }    }    /**     * Mark option not selected     * @param WebDriverElement $option     */    protected function deselectOption(WebDriverElement $option)    {        if ($option->isSelected()) {            $option->click();        }    }}
 |