123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594 |
- <?php
- namespace Facebook\WebDriver;
- use Facebook\WebDriver\Exception\NoAlertOpenException;
- use Facebook\WebDriver\Exception\NoSuchElementException;
- use Facebook\WebDriver\Exception\NoSuchFrameException;
- use Facebook\WebDriver\Exception\StaleElementReferenceException;
- class WebDriverExpectedCondition
- {
-
- private $apply;
- protected function __construct(callable $apply)
- {
- $this->apply = $apply;
- }
-
- public function getApply()
- {
- return $this->apply;
- }
-
- public static function titleIs($title)
- {
- return new static(
- function (WebDriver $driver) use ($title) {
- return $title === $driver->getTitle();
- }
- );
- }
-
- public static function titleContains($title)
- {
- return new static(
- function (WebDriver $driver) use ($title) {
- return mb_strpos($driver->getTitle(), $title) !== false;
- }
- );
- }
-
- public static function titleMatches($titleRegexp)
- {
- return new static(
- function (WebDriver $driver) use ($titleRegexp) {
- return (bool) preg_match($titleRegexp, $driver->getTitle());
- }
- );
- }
-
- public static function urlIs($url)
- {
- return new static(
- function (WebDriver $driver) use ($url) {
- return $url === $driver->getCurrentURL();
- }
- );
- }
-
- public static function urlContains($url)
- {
- return new static(
- function (WebDriver $driver) use ($url) {
- return mb_strpos($driver->getCurrentURL(), $url) !== false;
- }
- );
- }
-
- public static function urlMatches($urlRegexp)
- {
- return new static(
- function (WebDriver $driver) use ($urlRegexp) {
- return (bool) preg_match($urlRegexp, $driver->getCurrentURL());
- }
- );
- }
-
- public static function presenceOfElementLocated(WebDriverBy $by)
- {
- return new static(
- function (WebDriver $driver) use ($by) {
- return $driver->findElement($by);
- }
- );
- }
-
- public static function presenceOfAllElementsLocatedBy(WebDriverBy $by)
- {
- return new static(
- function (WebDriver $driver) use ($by) {
- $elements = $driver->findElements($by);
- return count($elements) > 0 ? $elements : null;
- }
- );
- }
-
- public static function visibilityOfElementLocated(WebDriverBy $by)
- {
- return new static(
- function (WebDriver $driver) use ($by) {
- try {
- $element = $driver->findElement($by);
- return $element->isDisplayed() ? $element : null;
- } catch (StaleElementReferenceException $e) {
- return null;
- }
- }
- );
- }
-
- public static function visibilityOfAnyElementLocated(WebDriverBy $by)
- {
- return new static(
- function (WebDriver $driver) use ($by) {
- $elements = $driver->findElements($by);
- $visibleElements = [];
- foreach ($elements as $element) {
- try {
- if ($element->isDisplayed()) {
- $visibleElements[] = $element;
- }
- } catch (StaleElementReferenceException $e) {
- }
- }
- return count($visibleElements) > 0 ? $visibleElements : null;
- }
- );
- }
-
- public static function visibilityOf(WebDriverElement $element)
- {
- return new static(
- function () use ($element) {
- return $element->isDisplayed() ? $element : null;
- }
- );
- }
-
- public static function textToBePresentInElement(WebDriverBy $by, $text)
- {
- return self::elementTextContains($by, $text);
- }
-
- public static function elementTextContains(WebDriverBy $by, $text)
- {
- return new static(
- function (WebDriver $driver) use ($by, $text) {
- try {
- $element_text = $driver->findElement($by)->getText();
- return mb_strpos($element_text, $text) !== false;
- } catch (StaleElementReferenceException $e) {
- return null;
- }
- }
- );
- }
-
- public static function elementTextIs(WebDriverBy $by, $text)
- {
- return new static(
- function (WebDriver $driver) use ($by, $text) {
- try {
- return $driver->findElement($by)->getText() == $text;
- } catch (StaleElementReferenceException $e) {
- return null;
- }
- }
- );
- }
-
- public static function elementTextMatches(WebDriverBy $by, $regexp)
- {
- return new static(
- function (WebDriver $driver) use ($by, $regexp) {
- try {
- return (bool) preg_match($regexp, $driver->findElement($by)->getText());
- } catch (StaleElementReferenceException $e) {
- return null;
- }
- }
- );
- }
-
- public static function textToBePresentInElementValue(WebDriverBy $by, $text)
- {
- return self::elementValueContains($by, $text);
- }
-
- public static function elementValueContains(WebDriverBy $by, $text)
- {
- return new static(
- function (WebDriver $driver) use ($by, $text) {
- try {
- $element_text = $driver->findElement($by)->getAttribute('value');
- return mb_strpos($element_text, $text) !== false;
- } catch (StaleElementReferenceException $e) {
- return null;
- }
- }
- );
- }
-
- public static function frameToBeAvailableAndSwitchToIt($frame_locator)
- {
- return new static(
- function (WebDriver $driver) use ($frame_locator) {
- try {
- return $driver->switchTo()->frame($frame_locator);
- } catch (NoSuchFrameException $e) {
- return false;
- }
- }
- );
- }
-
- public static function invisibilityOfElementLocated(WebDriverBy $by)
- {
- return new static(
- function (WebDriver $driver) use ($by) {
- try {
- return !$driver->findElement($by)->isDisplayed();
- } catch (NoSuchElementException $e) {
- return true;
- } catch (StaleElementReferenceException $e) {
- return true;
- }
- }
- );
- }
-
- public static function invisibilityOfElementWithText(WebDriverBy $by, $text)
- {
- return new static(
- function (WebDriver $driver) use ($by, $text) {
- try {
- return !($driver->findElement($by)->getText() === $text);
- } catch (NoSuchElementException $e) {
- return true;
- } catch (StaleElementReferenceException $e) {
- return true;
- }
- }
- );
- }
-
- public static function elementToBeClickable(WebDriverBy $by)
- {
- $visibility_of_element_located =
- self::visibilityOfElementLocated($by);
- return new static(
- function (WebDriver $driver) use ($visibility_of_element_located) {
- $element = call_user_func(
- $visibility_of_element_located->getApply(),
- $driver
- );
- try {
- if ($element !== null && $element->isEnabled()) {
- return $element;
- }
- return null;
- } catch (StaleElementReferenceException $e) {
- return null;
- }
- }
- );
- }
-
- public static function stalenessOf(WebDriverElement $element)
- {
- return new static(
- function () use ($element) {
- try {
- $element->isEnabled();
- return false;
- } catch (StaleElementReferenceException $e) {
- return true;
- }
- }
- );
- }
-
- public static function refreshed(self $condition)
- {
- return new static(
- function (WebDriver $driver) use ($condition) {
- try {
- return call_user_func($condition->getApply(), $driver);
- } catch (StaleElementReferenceException $e) {
- return null;
- }
- }
- );
- }
-
- public static function elementToBeSelected($element_or_by)
- {
- return self::elementSelectionStateToBe(
- $element_or_by,
- true
- );
- }
-
- public static function elementSelectionStateToBe($element_or_by, $selected)
- {
- if ($element_or_by instanceof WebDriverElement) {
- return new static(
- function () use ($element_or_by, $selected) {
- return $element_or_by->isSelected() === $selected;
- }
- );
- } else {
- if ($element_or_by instanceof WebDriverBy) {
- return new static(
- function (WebDriver $driver) use ($element_or_by, $selected) {
- try {
- $element = $driver->findElement($element_or_by);
- return $element->isSelected() === $selected;
- } catch (StaleElementReferenceException $e) {
- return null;
- }
- }
- );
- }
- }
- }
-
- public static function alertIsPresent()
- {
- return new static(
- function (WebDriver $driver) {
- try {
-
-
-
- $alert = $driver->switchTo()->alert();
- $alert->getText();
- return $alert;
- } catch (NoAlertOpenException $e) {
- return null;
- }
- }
- );
- }
-
- public static function numberOfWindowsToBe($expectedNumberOfWindows)
- {
- return new static(
- function (WebDriver $driver) use ($expectedNumberOfWindows) {
- return count($driver->getWindowHandles()) == $expectedNumberOfWindows;
- }
- );
- }
-
- public static function not(self $condition)
- {
- return new static(
- function (WebDriver $driver) use ($condition) {
- $result = call_user_func($condition->getApply(), $driver);
- return !$result;
- }
- );
- }
- }
|