/** * @dataProvider providerLocatorContructorsArguments */ public function testHasMethodsShouldReturnFalseForNullValuesAndTrueForNotNull($url, $key, $type, $fieldset, $tab, $parameters, $pageKey) { $locator = new Locator($url, $key, $type, $fieldset, $tab, $parameters, $pageKey); if (null === $url) { $this->assertFalse($locator->hasPageUrl()); } else { $this->assertTrue($locator->hasPageUrl()); } if (null === $key) { $this->assertFalse($locator->hasKey()); } else { $this->assertTrue($locator->hasKey()); } if (null === $type) { $this->assertFalse($locator->hasType()); } else { $this->assertTrue($locator->hasType()); } if (null === $fieldset) { $this->assertFalse($locator->hasFieldset()); } else { $this->assertTrue($locator->hasFieldset()); } if (null === $tab) { $this->assertFalse($locator->hasTab()); } else { $this->assertTrue($locator->hasTab()); } if ($parameters) { $this->assertTrue($locator->hasParameters()); } else { $this->assertFalse($locator->hasParameters()); } if (null === $pageKey) { $this->assertFalse($locator->hasPageKey()); } else { $this->assertTrue($locator->hasPageKey()); } }
/** * Returns XPath query of page element that correstonds to given locator * * @return string XPath query * @throws \OutOfRangeException If cannot find path by locator */ public function getXpath(Locator $locator) { // prepare query $query = ''; if ($locator->hasTab()) { $query .= "p:tab[@key='{$locator->getTab()}']"; } if ($locator->hasFieldset()) { $query .= "//p:fieldset[@key='{$locator->getFieldset()}']"; } if ($locator->hasKey()) { $query .= '//p:' . ($locator->getType() ?: '*') . "[@key='{$locator->getKey()}']"; } $query .= '/ancestor-or-self::*/@xpath'; // execute query $list = $this->query($query); // precess result if (!$list->length) { throw new \OutOfRangeException("Cannot find XPath by {$locator} on page '{$this->getKey()}'."); } $result = ''; foreach ($list as $item) { $result .= $item->value; } if ($locator->hasParameters()) { $patterns = array_map(function ($p) { return "%{$p}%"; }, array_keys($locator->getParameters())); $values = array_values($locator->getParameters()); $result = str_replace($patterns, $values, $result); } return $result; }