protected function setSelect(WebDriverElement $element, $value) { $select = new WebDriverSelect($element); if ($select->isMultiple()) { $select->deselectAll(); } if (!is_array($value)) { $value = [$value]; } foreach ($value as $v) { if (strpos($v, 'value:') === 0) { $select->selectByValue(substr($value, 6)); } else { $select->selectByVisibleText($v); } } }
public function selectOption($select, $option) { $el = $this->findField($select); if ($el->getTagName() != 'select') { $els = $this->matchCheckables($select); $radio = null; foreach ($els as $el) { $radio = $this->findCheckable($el, $option, true); if ($radio) { break; } } if (!$radio) { throw new ElementNotFound($select, "Radiobutton with value or name '{$option} in"); } $radio->click(); return; } $wdSelect = new WebDriverSelect($el); if ($wdSelect->isMultiple()) { $wdSelect->deselectAll(); } if (!is_array($option)) { $option = [$option]; } $matched = false; foreach ($option as $opt) { try { $wdSelect->selectByVisibleText($opt); $matched = true; } catch (NoSuchElementException $e) { } } if ($matched) { return; } foreach ($option as $opt) { try { $wdSelect->selectByValue($opt); $matched = true; } catch (NoSuchElementException $e) { } } if ($matched) { return; } // partially matching foreach ($option as $opt) { try { $optElement = $el->findElement(WebDriverBy::xpath('//option [contains (., "' . $opt . '")]')); $matched = true; if (!$optElement->isSelected()) { $optElement->click(); } } catch (NoSuchElementException $e) { } } if ($matched) { return; } throw new ElementNotFound(json_encode($option), "Option inside {$select} matched by name or value"); }
/** * @param WebDriverSelect $select * @param string|string[] $value * @throws \Exception */ protected function _setFieldSelect(WebDriverSelect $select, $value) { $valueList = (array) $value; if ($select->isMultiple()) { $select->deselectAll(); } $matched = false; foreach ($valueList as $value) { try { $select->selectByVisibleText($value); $matched = true; } catch (NoSuchElementException $e) { } } if ($matched) { return; } foreach ($valueList as $value) { try { $select->selectByValue($value); $matched = true; } catch (NoSuchElementException $e) { } } if (false === $matched) { throw new \Exception("Cannot select option `{$value}`"); } }