1*04fd306cSNickeau<?php 2*04fd306cSNickeau 3*04fd306cSNickeaunamespace Facebook\WebDriver; 4*04fd306cSNickeau 5*04fd306cSNickeauuse Facebook\WebDriver\Exception\NoSuchAlertException; 6*04fd306cSNickeauuse Facebook\WebDriver\Exception\NoSuchElementException; 7*04fd306cSNickeauuse Facebook\WebDriver\Exception\NoSuchFrameException; 8*04fd306cSNickeauuse Facebook\WebDriver\Exception\StaleElementReferenceException; 9*04fd306cSNickeau 10*04fd306cSNickeau/** 11*04fd306cSNickeau * Canned ExpectedConditions which are generally useful within webdriver tests. 12*04fd306cSNickeau * 13*04fd306cSNickeau * @see WebDriverWait 14*04fd306cSNickeau */ 15*04fd306cSNickeauclass WebDriverExpectedCondition 16*04fd306cSNickeau{ 17*04fd306cSNickeau /** 18*04fd306cSNickeau * A callable function to be executed by WebDriverWait. It should return 19*04fd306cSNickeau * a truthy value, mostly boolean or a WebDriverElement, on success. 20*04fd306cSNickeau * @var callable 21*04fd306cSNickeau */ 22*04fd306cSNickeau private $apply; 23*04fd306cSNickeau 24*04fd306cSNickeau protected function __construct(callable $apply) 25*04fd306cSNickeau { 26*04fd306cSNickeau $this->apply = $apply; 27*04fd306cSNickeau } 28*04fd306cSNickeau 29*04fd306cSNickeau /** 30*04fd306cSNickeau * @return callable A callable function to be executed by WebDriverWait 31*04fd306cSNickeau */ 32*04fd306cSNickeau public function getApply() 33*04fd306cSNickeau { 34*04fd306cSNickeau return $this->apply; 35*04fd306cSNickeau } 36*04fd306cSNickeau 37*04fd306cSNickeau /** 38*04fd306cSNickeau * An expectation for checking the title of a page. 39*04fd306cSNickeau * 40*04fd306cSNickeau * @param string $title The expected title, which must be an exact match. 41*04fd306cSNickeau * @return static Condition returns whether current page title equals given string. 42*04fd306cSNickeau */ 43*04fd306cSNickeau public static function titleIs($title) 44*04fd306cSNickeau { 45*04fd306cSNickeau return new static( 46*04fd306cSNickeau function (WebDriver $driver) use ($title) { 47*04fd306cSNickeau return $title === $driver->getTitle(); 48*04fd306cSNickeau } 49*04fd306cSNickeau ); 50*04fd306cSNickeau } 51*04fd306cSNickeau 52*04fd306cSNickeau /** 53*04fd306cSNickeau * An expectation for checking substring of a page Title. 54*04fd306cSNickeau * 55*04fd306cSNickeau * @param string $title The expected substring of Title. 56*04fd306cSNickeau * @return static Condition returns whether current page title contains given string. 57*04fd306cSNickeau */ 58*04fd306cSNickeau public static function titleContains($title) 59*04fd306cSNickeau { 60*04fd306cSNickeau return new static( 61*04fd306cSNickeau function (WebDriver $driver) use ($title) { 62*04fd306cSNickeau return mb_strpos($driver->getTitle(), $title) !== false; 63*04fd306cSNickeau } 64*04fd306cSNickeau ); 65*04fd306cSNickeau } 66*04fd306cSNickeau 67*04fd306cSNickeau /** 68*04fd306cSNickeau * An expectation for checking current page title matches the given regular expression. 69*04fd306cSNickeau * 70*04fd306cSNickeau * @param string $titleRegexp The regular expression to test against. 71*04fd306cSNickeau * @return static Condition returns whether current page title matches the regular expression. 72*04fd306cSNickeau */ 73*04fd306cSNickeau public static function titleMatches($titleRegexp) 74*04fd306cSNickeau { 75*04fd306cSNickeau return new static( 76*04fd306cSNickeau function (WebDriver $driver) use ($titleRegexp) { 77*04fd306cSNickeau return (bool) preg_match($titleRegexp, $driver->getTitle()); 78*04fd306cSNickeau } 79*04fd306cSNickeau ); 80*04fd306cSNickeau } 81*04fd306cSNickeau 82*04fd306cSNickeau /** 83*04fd306cSNickeau * An expectation for checking the URL of a page. 84*04fd306cSNickeau * 85*04fd306cSNickeau * @param string $url The expected URL, which must be an exact match. 86*04fd306cSNickeau * @return static Condition returns whether current URL equals given one. 87*04fd306cSNickeau */ 88*04fd306cSNickeau public static function urlIs($url) 89*04fd306cSNickeau { 90*04fd306cSNickeau return new static( 91*04fd306cSNickeau function (WebDriver $driver) use ($url) { 92*04fd306cSNickeau return $url === $driver->getCurrentURL(); 93*04fd306cSNickeau } 94*04fd306cSNickeau ); 95*04fd306cSNickeau } 96*04fd306cSNickeau 97*04fd306cSNickeau /** 98*04fd306cSNickeau * An expectation for checking substring of the URL of a page. 99*04fd306cSNickeau * 100*04fd306cSNickeau * @param string $url The expected substring of the URL 101*04fd306cSNickeau * @return static Condition returns whether current URL contains given string. 102*04fd306cSNickeau */ 103*04fd306cSNickeau public static function urlContains($url) 104*04fd306cSNickeau { 105*04fd306cSNickeau return new static( 106*04fd306cSNickeau function (WebDriver $driver) use ($url) { 107*04fd306cSNickeau return mb_strpos($driver->getCurrentURL(), $url) !== false; 108*04fd306cSNickeau } 109*04fd306cSNickeau ); 110*04fd306cSNickeau } 111*04fd306cSNickeau 112*04fd306cSNickeau /** 113*04fd306cSNickeau * An expectation for checking current page URL matches the given regular expression. 114*04fd306cSNickeau * 115*04fd306cSNickeau * @param string $urlRegexp The regular expression to test against. 116*04fd306cSNickeau * @return static Condition returns whether current URL matches the regular expression. 117*04fd306cSNickeau */ 118*04fd306cSNickeau public static function urlMatches($urlRegexp) 119*04fd306cSNickeau { 120*04fd306cSNickeau return new static( 121*04fd306cSNickeau function (WebDriver $driver) use ($urlRegexp) { 122*04fd306cSNickeau return (bool) preg_match($urlRegexp, $driver->getCurrentURL()); 123*04fd306cSNickeau } 124*04fd306cSNickeau ); 125*04fd306cSNickeau } 126*04fd306cSNickeau 127*04fd306cSNickeau /** 128*04fd306cSNickeau * An expectation for checking that an element is present on the DOM of a page. 129*04fd306cSNickeau * This does not necessarily mean that the element is visible. 130*04fd306cSNickeau * 131*04fd306cSNickeau * @param WebDriverBy $by The locator used to find the element. 132*04fd306cSNickeau * @return static Condition returns the WebDriverElement which is located. 133*04fd306cSNickeau */ 134*04fd306cSNickeau public static function presenceOfElementLocated(WebDriverBy $by) 135*04fd306cSNickeau { 136*04fd306cSNickeau return new static( 137*04fd306cSNickeau function (WebDriver $driver) use ($by) { 138*04fd306cSNickeau try { 139*04fd306cSNickeau return $driver->findElement($by); 140*04fd306cSNickeau } catch (NoSuchElementException $e) { 141*04fd306cSNickeau return false; 142*04fd306cSNickeau } 143*04fd306cSNickeau } 144*04fd306cSNickeau ); 145*04fd306cSNickeau } 146*04fd306cSNickeau 147*04fd306cSNickeau /** 148*04fd306cSNickeau * An expectation for checking that there is at least one element present on a web page. 149*04fd306cSNickeau * 150*04fd306cSNickeau * @param WebDriverBy $by The locator used to find the element. 151*04fd306cSNickeau * @return static Condition return an array of WebDriverElement once they are located. 152*04fd306cSNickeau */ 153*04fd306cSNickeau public static function presenceOfAllElementsLocatedBy(WebDriverBy $by) 154*04fd306cSNickeau { 155*04fd306cSNickeau return new static( 156*04fd306cSNickeau function (WebDriver $driver) use ($by) { 157*04fd306cSNickeau $elements = $driver->findElements($by); 158*04fd306cSNickeau 159*04fd306cSNickeau return count($elements) > 0 ? $elements : null; 160*04fd306cSNickeau } 161*04fd306cSNickeau ); 162*04fd306cSNickeau } 163*04fd306cSNickeau 164*04fd306cSNickeau /** 165*04fd306cSNickeau * An expectation for checking that an element is present on the DOM of a page and visible. 166*04fd306cSNickeau * Visibility means that the element is not only displayed but also has a height and width that is greater than 0. 167*04fd306cSNickeau * 168*04fd306cSNickeau * @param WebDriverBy $by The locator used to find the element. 169*04fd306cSNickeau * @return static Condition returns the WebDriverElement which is located and visible. 170*04fd306cSNickeau */ 171*04fd306cSNickeau public static function visibilityOfElementLocated(WebDriverBy $by) 172*04fd306cSNickeau { 173*04fd306cSNickeau return new static( 174*04fd306cSNickeau function (WebDriver $driver) use ($by) { 175*04fd306cSNickeau try { 176*04fd306cSNickeau $element = $driver->findElement($by); 177*04fd306cSNickeau 178*04fd306cSNickeau return $element->isDisplayed() ? $element : null; 179*04fd306cSNickeau } catch (StaleElementReferenceException $e) { 180*04fd306cSNickeau return null; 181*04fd306cSNickeau } 182*04fd306cSNickeau } 183*04fd306cSNickeau ); 184*04fd306cSNickeau } 185*04fd306cSNickeau 186*04fd306cSNickeau /** 187*04fd306cSNickeau * An expectation for checking than at least one element in an array of elements is present on the 188*04fd306cSNickeau * DOM of a page and visible. 189*04fd306cSNickeau * Visibility means that the element is not only displayed but also has a height and width that is greater than 0. 190*04fd306cSNickeau * 191*04fd306cSNickeau * @param WebDriverBy $by The located used to find the element. 192*04fd306cSNickeau * @return static Condition returns the array of WebDriverElement that are located and visible. 193*04fd306cSNickeau */ 194*04fd306cSNickeau public static function visibilityOfAnyElementLocated(WebDriverBy $by) 195*04fd306cSNickeau { 196*04fd306cSNickeau return new static( 197*04fd306cSNickeau function (WebDriver $driver) use ($by) { 198*04fd306cSNickeau $elements = $driver->findElements($by); 199*04fd306cSNickeau $visibleElements = []; 200*04fd306cSNickeau 201*04fd306cSNickeau foreach ($elements as $element) { 202*04fd306cSNickeau try { 203*04fd306cSNickeau if ($element->isDisplayed()) { 204*04fd306cSNickeau $visibleElements[] = $element; 205*04fd306cSNickeau } 206*04fd306cSNickeau } catch (StaleElementReferenceException $e) { 207*04fd306cSNickeau } 208*04fd306cSNickeau } 209*04fd306cSNickeau 210*04fd306cSNickeau return count($visibleElements) > 0 ? $visibleElements : null; 211*04fd306cSNickeau } 212*04fd306cSNickeau ); 213*04fd306cSNickeau } 214*04fd306cSNickeau 215*04fd306cSNickeau /** 216*04fd306cSNickeau * An expectation for checking that an element, known to be present on the DOM of a page, is visible. 217*04fd306cSNickeau * Visibility means that the element is not only displayed but also has a height and width that is greater than 0. 218*04fd306cSNickeau * 219*04fd306cSNickeau * @param WebDriverElement $element The element to be checked. 220*04fd306cSNickeau * @return static Condition returns the same WebDriverElement once it is visible. 221*04fd306cSNickeau */ 222*04fd306cSNickeau public static function visibilityOf(WebDriverElement $element) 223*04fd306cSNickeau { 224*04fd306cSNickeau return new static( 225*04fd306cSNickeau function () use ($element) { 226*04fd306cSNickeau return $element->isDisplayed() ? $element : null; 227*04fd306cSNickeau } 228*04fd306cSNickeau ); 229*04fd306cSNickeau } 230*04fd306cSNickeau 231*04fd306cSNickeau /** 232*04fd306cSNickeau * An expectation for checking if the given text is present in the specified element. 233*04fd306cSNickeau * To check exact text match use elementTextIs() condition. 234*04fd306cSNickeau * 235*04fd306cSNickeau * @codeCoverageIgnore 236*04fd306cSNickeau * @deprecated Use WebDriverExpectedCondition::elementTextContains() instead 237*04fd306cSNickeau * @param WebDriverBy $by The locator used to find the element. 238*04fd306cSNickeau * @param string $text The text to be presented in the element. 239*04fd306cSNickeau * @return static Condition returns whether the text is present in the element. 240*04fd306cSNickeau */ 241*04fd306cSNickeau public static function textToBePresentInElement(WebDriverBy $by, $text) 242*04fd306cSNickeau { 243*04fd306cSNickeau return self::elementTextContains($by, $text); 244*04fd306cSNickeau } 245*04fd306cSNickeau 246*04fd306cSNickeau /** 247*04fd306cSNickeau * An expectation for checking if the given text is present in the specified element. 248*04fd306cSNickeau * To check exact text match use elementTextIs() condition. 249*04fd306cSNickeau * 250*04fd306cSNickeau * @param WebDriverBy $by The locator used to find the element. 251*04fd306cSNickeau * @param string $text The text to be presented in the element. 252*04fd306cSNickeau * @return static Condition returns whether the partial text is present in the element. 253*04fd306cSNickeau */ 254*04fd306cSNickeau public static function elementTextContains(WebDriverBy $by, $text) 255*04fd306cSNickeau { 256*04fd306cSNickeau return new static( 257*04fd306cSNickeau function (WebDriver $driver) use ($by, $text) { 258*04fd306cSNickeau try { 259*04fd306cSNickeau $element_text = $driver->findElement($by)->getText(); 260*04fd306cSNickeau 261*04fd306cSNickeau return mb_strpos($element_text, $text) !== false; 262*04fd306cSNickeau } catch (StaleElementReferenceException $e) { 263*04fd306cSNickeau return null; 264*04fd306cSNickeau } 265*04fd306cSNickeau } 266*04fd306cSNickeau ); 267*04fd306cSNickeau } 268*04fd306cSNickeau 269*04fd306cSNickeau /** 270*04fd306cSNickeau * An expectation for checking if the given text exactly equals the text in specified element. 271*04fd306cSNickeau * To check only partial substring of the text use elementTextContains() condition. 272*04fd306cSNickeau * 273*04fd306cSNickeau * @param WebDriverBy $by The locator used to find the element. 274*04fd306cSNickeau * @param string $text The expected text of the element. 275*04fd306cSNickeau * @return static Condition returns whether the element has text value equal to given one. 276*04fd306cSNickeau */ 277*04fd306cSNickeau public static function elementTextIs(WebDriverBy $by, $text) 278*04fd306cSNickeau { 279*04fd306cSNickeau return new static( 280*04fd306cSNickeau function (WebDriver $driver) use ($by, $text) { 281*04fd306cSNickeau try { 282*04fd306cSNickeau return $driver->findElement($by)->getText() == $text; 283*04fd306cSNickeau } catch (StaleElementReferenceException $e) { 284*04fd306cSNickeau return null; 285*04fd306cSNickeau } 286*04fd306cSNickeau } 287*04fd306cSNickeau ); 288*04fd306cSNickeau } 289*04fd306cSNickeau 290*04fd306cSNickeau /** 291*04fd306cSNickeau * An expectation for checking if the given regular expression matches the text in specified element. 292*04fd306cSNickeau * 293*04fd306cSNickeau * @param WebDriverBy $by The locator used to find the element. 294*04fd306cSNickeau * @param string $regexp The regular expression to test against. 295*04fd306cSNickeau * @return static Condition returns whether the element has text value equal to given one. 296*04fd306cSNickeau */ 297*04fd306cSNickeau public static function elementTextMatches(WebDriverBy $by, $regexp) 298*04fd306cSNickeau { 299*04fd306cSNickeau return new static( 300*04fd306cSNickeau function (WebDriver $driver) use ($by, $regexp) { 301*04fd306cSNickeau try { 302*04fd306cSNickeau return (bool) preg_match($regexp, $driver->findElement($by)->getText()); 303*04fd306cSNickeau } catch (StaleElementReferenceException $e) { 304*04fd306cSNickeau return null; 305*04fd306cSNickeau } 306*04fd306cSNickeau } 307*04fd306cSNickeau ); 308*04fd306cSNickeau } 309*04fd306cSNickeau 310*04fd306cSNickeau /** 311*04fd306cSNickeau * An expectation for checking if the given text is present in the specified elements value attribute. 312*04fd306cSNickeau * 313*04fd306cSNickeau * @codeCoverageIgnore 314*04fd306cSNickeau * @deprecated Use WebDriverExpectedCondition::elementValueContains() instead 315*04fd306cSNickeau * @param WebDriverBy $by The locator used to find the element. 316*04fd306cSNickeau * @param string $text The text to be presented in the element value. 317*04fd306cSNickeau * @return static Condition returns whether the text is present in value attribute. 318*04fd306cSNickeau */ 319*04fd306cSNickeau public static function textToBePresentInElementValue(WebDriverBy $by, $text) 320*04fd306cSNickeau { 321*04fd306cSNickeau return self::elementValueContains($by, $text); 322*04fd306cSNickeau } 323*04fd306cSNickeau 324*04fd306cSNickeau /** 325*04fd306cSNickeau * An expectation for checking if the given text is present in the specified elements value attribute. 326*04fd306cSNickeau * 327*04fd306cSNickeau * @param WebDriverBy $by The locator used to find the element. 328*04fd306cSNickeau * @param string $text The text to be presented in the element value. 329*04fd306cSNickeau * @return static Condition returns whether the text is present in value attribute. 330*04fd306cSNickeau */ 331*04fd306cSNickeau public static function elementValueContains(WebDriverBy $by, $text) 332*04fd306cSNickeau { 333*04fd306cSNickeau return new static( 334*04fd306cSNickeau function (WebDriver $driver) use ($by, $text) { 335*04fd306cSNickeau try { 336*04fd306cSNickeau $element_text = $driver->findElement($by)->getAttribute('value'); 337*04fd306cSNickeau 338*04fd306cSNickeau return mb_strpos($element_text, $text) !== false; 339*04fd306cSNickeau } catch (StaleElementReferenceException $e) { 340*04fd306cSNickeau return null; 341*04fd306cSNickeau } 342*04fd306cSNickeau } 343*04fd306cSNickeau ); 344*04fd306cSNickeau } 345*04fd306cSNickeau 346*04fd306cSNickeau /** 347*04fd306cSNickeau * Expectation for checking if iFrame exists. If iFrame exists switches driver's focus to the iFrame. 348*04fd306cSNickeau * 349*04fd306cSNickeau * @param string $frame_locator The locator used to find the iFrame 350*04fd306cSNickeau * expected to be either the id or name value of the i/frame 351*04fd306cSNickeau * @return static Condition returns object focused on new frame when frame is found, false otherwise. 352*04fd306cSNickeau */ 353*04fd306cSNickeau public static function frameToBeAvailableAndSwitchToIt($frame_locator) 354*04fd306cSNickeau { 355*04fd306cSNickeau return new static( 356*04fd306cSNickeau function (WebDriver $driver) use ($frame_locator) { 357*04fd306cSNickeau try { 358*04fd306cSNickeau return $driver->switchTo()->frame($frame_locator); 359*04fd306cSNickeau } catch (NoSuchFrameException $e) { 360*04fd306cSNickeau return false; 361*04fd306cSNickeau } 362*04fd306cSNickeau } 363*04fd306cSNickeau ); 364*04fd306cSNickeau } 365*04fd306cSNickeau 366*04fd306cSNickeau /** 367*04fd306cSNickeau * An expectation for checking that an element is either invisible or not present on the DOM. 368*04fd306cSNickeau * 369*04fd306cSNickeau * @param WebDriverBy $by The locator used to find the element. 370*04fd306cSNickeau * @return static Condition returns whether no visible element located. 371*04fd306cSNickeau */ 372*04fd306cSNickeau public static function invisibilityOfElementLocated(WebDriverBy $by) 373*04fd306cSNickeau { 374*04fd306cSNickeau return new static( 375*04fd306cSNickeau function (WebDriver $driver) use ($by) { 376*04fd306cSNickeau try { 377*04fd306cSNickeau return !$driver->findElement($by)->isDisplayed(); 378*04fd306cSNickeau } catch (NoSuchElementException $e) { 379*04fd306cSNickeau return true; 380*04fd306cSNickeau } catch (StaleElementReferenceException $e) { 381*04fd306cSNickeau return true; 382*04fd306cSNickeau } 383*04fd306cSNickeau } 384*04fd306cSNickeau ); 385*04fd306cSNickeau } 386*04fd306cSNickeau 387*04fd306cSNickeau /** 388*04fd306cSNickeau * An expectation for checking that an element with text is either invisible or not present on the DOM. 389*04fd306cSNickeau * 390*04fd306cSNickeau * @param WebDriverBy $by The locator used to find the element. 391*04fd306cSNickeau * @param string $text The text of the element. 392*04fd306cSNickeau * @return static Condition returns whether the text is found in the element located. 393*04fd306cSNickeau */ 394*04fd306cSNickeau public static function invisibilityOfElementWithText(WebDriverBy $by, $text) 395*04fd306cSNickeau { 396*04fd306cSNickeau return new static( 397*04fd306cSNickeau function (WebDriver $driver) use ($by, $text) { 398*04fd306cSNickeau try { 399*04fd306cSNickeau return !($driver->findElement($by)->getText() === $text); 400*04fd306cSNickeau } catch (NoSuchElementException $e) { 401*04fd306cSNickeau return true; 402*04fd306cSNickeau } catch (StaleElementReferenceException $e) { 403*04fd306cSNickeau return true; 404*04fd306cSNickeau } 405*04fd306cSNickeau } 406*04fd306cSNickeau ); 407*04fd306cSNickeau } 408*04fd306cSNickeau 409*04fd306cSNickeau /** 410*04fd306cSNickeau * An expectation for checking an element is visible and enabled such that you can click it. 411*04fd306cSNickeau * 412*04fd306cSNickeau * @param WebDriverBy $by The locator used to find the element 413*04fd306cSNickeau * @return static Condition return the WebDriverElement once it is located, visible and clickable. 414*04fd306cSNickeau */ 415*04fd306cSNickeau public static function elementToBeClickable(WebDriverBy $by) 416*04fd306cSNickeau { 417*04fd306cSNickeau $visibility_of_element_located = self::visibilityOfElementLocated($by); 418*04fd306cSNickeau 419*04fd306cSNickeau return new static( 420*04fd306cSNickeau function (WebDriver $driver) use ($visibility_of_element_located) { 421*04fd306cSNickeau $element = call_user_func( 422*04fd306cSNickeau $visibility_of_element_located->getApply(), 423*04fd306cSNickeau $driver 424*04fd306cSNickeau ); 425*04fd306cSNickeau 426*04fd306cSNickeau try { 427*04fd306cSNickeau if ($element !== null && $element->isEnabled()) { 428*04fd306cSNickeau return $element; 429*04fd306cSNickeau } 430*04fd306cSNickeau 431*04fd306cSNickeau return null; 432*04fd306cSNickeau } catch (StaleElementReferenceException $e) { 433*04fd306cSNickeau return null; 434*04fd306cSNickeau } 435*04fd306cSNickeau } 436*04fd306cSNickeau ); 437*04fd306cSNickeau } 438*04fd306cSNickeau 439*04fd306cSNickeau /** 440*04fd306cSNickeau * Wait until an element is no longer attached to the DOM. 441*04fd306cSNickeau * 442*04fd306cSNickeau * @param WebDriverElement $element The element to wait for. 443*04fd306cSNickeau * @return static Condition returns whether the element is still attached to the DOM. 444*04fd306cSNickeau */ 445*04fd306cSNickeau public static function stalenessOf(WebDriverElement $element) 446*04fd306cSNickeau { 447*04fd306cSNickeau return new static( 448*04fd306cSNickeau function () use ($element) { 449*04fd306cSNickeau try { 450*04fd306cSNickeau $element->isEnabled(); 451*04fd306cSNickeau 452*04fd306cSNickeau return false; 453*04fd306cSNickeau } catch (StaleElementReferenceException $e) { 454*04fd306cSNickeau return true; 455*04fd306cSNickeau } 456*04fd306cSNickeau } 457*04fd306cSNickeau ); 458*04fd306cSNickeau } 459*04fd306cSNickeau 460*04fd306cSNickeau /** 461*04fd306cSNickeau * Wrapper for a condition, which allows for elements to update by redrawing. 462*04fd306cSNickeau * 463*04fd306cSNickeau * This works around the problem of conditions which have two parts: find an element and then check for some 464*04fd306cSNickeau * condition on it. For these conditions it is possible that an element is located and then subsequently it is 465*04fd306cSNickeau * redrawn on the client. When this happens a StaleElementReferenceException is thrown when the second part of 466*04fd306cSNickeau * the condition is checked. 467*04fd306cSNickeau * 468*04fd306cSNickeau * @param WebDriverExpectedCondition $condition The condition wrapped. 469*04fd306cSNickeau * @return static Condition returns the return value of the getApply() of the given condition. 470*04fd306cSNickeau */ 471*04fd306cSNickeau public static function refreshed(self $condition) 472*04fd306cSNickeau { 473*04fd306cSNickeau return new static( 474*04fd306cSNickeau function (WebDriver $driver) use ($condition) { 475*04fd306cSNickeau try { 476*04fd306cSNickeau return call_user_func($condition->getApply(), $driver); 477*04fd306cSNickeau } catch (StaleElementReferenceException $e) { 478*04fd306cSNickeau return null; 479*04fd306cSNickeau } 480*04fd306cSNickeau } 481*04fd306cSNickeau ); 482*04fd306cSNickeau } 483*04fd306cSNickeau 484*04fd306cSNickeau /** 485*04fd306cSNickeau * An expectation for checking if the given element is selected. 486*04fd306cSNickeau * 487*04fd306cSNickeau * @param mixed $element_or_by Either the element or the locator. 488*04fd306cSNickeau * @return static Condition returns whether the element is selected. 489*04fd306cSNickeau */ 490*04fd306cSNickeau public static function elementToBeSelected($element_or_by) 491*04fd306cSNickeau { 492*04fd306cSNickeau return self::elementSelectionStateToBe( 493*04fd306cSNickeau $element_or_by, 494*04fd306cSNickeau true 495*04fd306cSNickeau ); 496*04fd306cSNickeau } 497*04fd306cSNickeau 498*04fd306cSNickeau /** 499*04fd306cSNickeau * An expectation for checking if the given element is selected. 500*04fd306cSNickeau * 501*04fd306cSNickeau * @param mixed $element_or_by Either the element or the locator. 502*04fd306cSNickeau * @param bool $selected The required state. 503*04fd306cSNickeau * @return static Condition returns whether the element is selected. 504*04fd306cSNickeau */ 505*04fd306cSNickeau public static function elementSelectionStateToBe($element_or_by, $selected) 506*04fd306cSNickeau { 507*04fd306cSNickeau if ($element_or_by instanceof WebDriverElement) { 508*04fd306cSNickeau return new static( 509*04fd306cSNickeau function () use ($element_or_by, $selected) { 510*04fd306cSNickeau return $element_or_by->isSelected() === $selected; 511*04fd306cSNickeau } 512*04fd306cSNickeau ); 513*04fd306cSNickeau } 514*04fd306cSNickeau 515*04fd306cSNickeau if ($element_or_by instanceof WebDriverBy) { 516*04fd306cSNickeau return new static( 517*04fd306cSNickeau function (WebDriver $driver) use ($element_or_by, $selected) { 518*04fd306cSNickeau try { 519*04fd306cSNickeau $element = $driver->findElement($element_or_by); 520*04fd306cSNickeau 521*04fd306cSNickeau return $element->isSelected() === $selected; 522*04fd306cSNickeau } catch (StaleElementReferenceException $e) { 523*04fd306cSNickeau return null; 524*04fd306cSNickeau } 525*04fd306cSNickeau } 526*04fd306cSNickeau ); 527*04fd306cSNickeau } 528*04fd306cSNickeau 529*04fd306cSNickeau throw new \InvalidArgumentException('Instance of either WebDriverElement or WebDriverBy must be given'); 530*04fd306cSNickeau } 531*04fd306cSNickeau 532*04fd306cSNickeau /** 533*04fd306cSNickeau * An expectation for whether an alert() box is present. 534*04fd306cSNickeau * 535*04fd306cSNickeau * @return static Condition returns WebDriverAlert if alert() is present, null otherwise. 536*04fd306cSNickeau */ 537*04fd306cSNickeau public static function alertIsPresent() 538*04fd306cSNickeau { 539*04fd306cSNickeau return new static( 540*04fd306cSNickeau function (WebDriver $driver) { 541*04fd306cSNickeau try { 542*04fd306cSNickeau // Unlike the Java code, we get a WebDriverAlert object regardless 543*04fd306cSNickeau // of whether there is an alert. Calling getText() will throw 544*04fd306cSNickeau // an exception if it is not really there. 545*04fd306cSNickeau $alert = $driver->switchTo()->alert(); 546*04fd306cSNickeau $alert->getText(); 547*04fd306cSNickeau 548*04fd306cSNickeau return $alert; 549*04fd306cSNickeau } catch (NoSuchAlertException $e) { 550*04fd306cSNickeau return null; 551*04fd306cSNickeau } 552*04fd306cSNickeau } 553*04fd306cSNickeau ); 554*04fd306cSNickeau } 555*04fd306cSNickeau 556*04fd306cSNickeau /** 557*04fd306cSNickeau * An expectation checking the number of opened windows. 558*04fd306cSNickeau * 559*04fd306cSNickeau * @param int $expectedNumberOfWindows 560*04fd306cSNickeau * @return static 561*04fd306cSNickeau */ 562*04fd306cSNickeau public static function numberOfWindowsToBe($expectedNumberOfWindows) 563*04fd306cSNickeau { 564*04fd306cSNickeau return new static( 565*04fd306cSNickeau function (WebDriver $driver) use ($expectedNumberOfWindows) { 566*04fd306cSNickeau return count($driver->getWindowHandles()) == $expectedNumberOfWindows; 567*04fd306cSNickeau } 568*04fd306cSNickeau ); 569*04fd306cSNickeau } 570*04fd306cSNickeau 571*04fd306cSNickeau /** 572*04fd306cSNickeau * An expectation with the logical opposite condition of the given condition. 573*04fd306cSNickeau * 574*04fd306cSNickeau * @param WebDriverExpectedCondition $condition The condition to be negated. 575*04fd306cSNickeau * @return mixed The negation of the result of the given condition. 576*04fd306cSNickeau */ 577*04fd306cSNickeau public static function not(self $condition) 578*04fd306cSNickeau { 579*04fd306cSNickeau return new static( 580*04fd306cSNickeau function (WebDriver $driver) use ($condition) { 581*04fd306cSNickeau $result = call_user_func($condition->getApply(), $driver); 582*04fd306cSNickeau 583*04fd306cSNickeau return !$result; 584*04fd306cSNickeau } 585*04fd306cSNickeau ); 586*04fd306cSNickeau } 587*04fd306cSNickeau} 588