예제 #1
0
 /**
  * @param $context
  * @param $radioOrCheckbox
  * @param bool $byValue
  * @return mixed|null
  */
 protected function findCheckable($context, $radioOrCheckbox, $byValue = false)
 {
     if ($radioOrCheckbox instanceof WebDriverElement) {
         return $radioOrCheckbox;
     }
     if (is_array($radioOrCheckbox) or $radioOrCheckbox instanceof WebDriverBy) {
         return $this->matchFirstOrFail($this->webDriver, $radioOrCheckbox);
     }
     $locator = Crawler::xpathLiteral($radioOrCheckbox);
     if ($context instanceof WebDriverElement && $context->getTagName() === 'input') {
         $contextType = $context->getAttribute('type');
         if (!in_array($contextType, ['checkbox', 'radio'], true)) {
             return null;
         }
         $nameLiteral = Crawler::xPathLiteral($context->getAttribute('name'));
         $typeLiteral = Crawler::xPathLiteral($contextType);
         $inputLocatorFragment = "input[@type = {$typeLiteral}][@name = {$nameLiteral}]";
         $xpath = Locator::combine("ancestor::form//{$inputLocatorFragment}[(@id = ancestor::form//label[contains(normalize-space(string(.)), {$locator})]/@for) or @placeholder = {$locator}]", "ancestor::form//label[contains(normalize-space(string(.)), {$locator})]//{$inputLocatorFragment}");
         if ($byValue) {
             $xpath = Locator::combine($xpath, "ancestor::form//{$inputLocatorFragment}[@value = {$locator}]");
         }
     } else {
         $xpath = Locator::combine("//input[@type = 'checkbox' or @type = 'radio'][(@id = //label[contains(normalize-space(string(.)), {$locator})]/@for) or @placeholder = {$locator}]", "//label[contains(normalize-space(string(.)), {$locator})]//input[@type = 'radio' or @type = 'checkbox']");
         if ($byValue) {
             $xpath = Locator::combine($xpath, "//input[@type = 'checkbox' or @type = 'radio'][@value = {$locator}]");
         }
     }
     $els = $context->findElements(WebDriverBy::xpath($xpath));
     if (count($els)) {
         return reset($els);
     }
     $els = $context->findElements(WebDriverBy::xpath(str_replace('ancestor::form', '', $xpath)));
     if (count($els)) {
         return reset($els);
     }
     $els = $this->match($context, $radioOrCheckbox);
     if (count($els)) {
         return reset($els);
     }
     return null;
 }