|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the ReactPHP WebDriver <https://github.com/itnelo/reactphp-webdriver>. |
| 5 | + * |
| 6 | + * (c) 2020 Pavel Petrov <itnelo@gmail.com>. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + * |
| 11 | + * @license https://opensource.org/licenses/mit MIT |
| 12 | + */ |
| 13 | + |
| 14 | +declare(strict_types=1); |
| 15 | + |
| 16 | +namespace Itnelo\React\WebDriver; |
| 17 | + |
| 18 | +use React\Promise\PromiseInterface; |
| 19 | + |
| 20 | +/** |
| 21 | + * Manipulates a remote browser instance asynchronously, using Selenium Grid (hub) API. |
| 22 | + * |
| 23 | + * As an extension to the base client, it can perform both action requests and some client-side (offline) tasks, |
| 24 | + * between these actions, such as conditional waits. |
| 25 | + */ |
| 26 | +interface WebDriverInterface extends ClientInterface |
| 27 | +{ |
| 28 | + /** |
| 29 | + * Returns a promise that will be resolved when the driver completes idling for the specified amount of time. |
| 30 | + * |
| 31 | + * Usage example: |
| 32 | + * |
| 33 | + * ``` |
| 34 | + * $navigationPromise = $webdriver->openUri('https://github.com/itnelo'); |
| 35 | + * |
| 36 | + * $elementIdentifierPromise = $navigationPromise->then( |
| 37 | + * function () use ($webdriver) { |
| 38 | + * // try-catch |
| 39 | + * $delayPromise = $webdriver->wait(5.0); |
| 40 | + * |
| 41 | + * return $delayPromise->then( |
| 42 | + * function () use ($webdriver) { |
| 43 | + * return $webdriver->getElementIdentifier('sessionIdentifier', 'xpathQuery'); |
| 44 | + * } |
| 45 | + * ); |
| 46 | + * } |
| 47 | + * // handle rejection reason (e.g. a connection timeout due to unexpected rate limiting) |
| 48 | + * ); |
| 49 | + * |
| 50 | + * $elementClickPromise = $elementIdentifierPromise->then( |
| 51 | + * function (string $elementIdentifier) use ($webdriver) { |
| 52 | + * // try-catch |
| 53 | + * return $webdriver->clickElement('sessionIdentifier', $elementIdentifier); |
| 54 | + * } |
| 55 | + * // handle rejection reason (e.g. invalid xpath or element not found error) |
| 56 | + * ); |
| 57 | + * ``` |
| 58 | + * |
| 59 | + * Note: each wait call is a separate action and starts its own timer; that timer will not wait for any other |
| 60 | + * timers to fire (i.e. this method is not suited for "shooting" an array of concurrent requests with delays, to |
| 61 | + * bypass rate limits, but to wait page loading and javascript code). Basically, it is just a syntactic sugar for |
| 62 | + * promise timer boilerplate, so you should use the event reactor directly, to limit driver calls for some |
| 63 | + * sensitive operations. |
| 64 | + * |
| 65 | + * @param float $time Time in seconds to wait (e.g. 0.351; max precision can be 3) |
| 66 | + * |
| 67 | + * @return PromiseInterface<null> |
| 68 | + */ |
| 69 | + public function wait(float $time): PromiseInterface; |
| 70 | + |
| 71 | + /** |
| 72 | + * Returns a promise that will be resolved when a given condition is met within specified amount of time and |
| 73 | + * rejected, otherwise. |
| 74 | + * |
| 75 | + * A condition callback must return an instance of PromiseInterface. Whenever that promise becomes rejected, driver |
| 76 | + * will try to get a new promise from the callback, until it reaches a given timeout for retry attempts. |
| 77 | + * |
| 78 | + * Usage example: |
| 79 | + * |
| 80 | + * ``` |
| 81 | + * $waitUntilPromise = $webdriver->waitUntil( |
| 82 | + * 15.5, |
| 83 | + * function () use ($webdriver) { |
| 84 | + * $visibilityStatePromise = $webdriver->getElementVisibility(...); |
| 85 | + * |
| 86 | + * $becomeVisiblePromise = $visibilityStatePromise->then( |
| 87 | + * function (bool $isVisible) { |
| 88 | + * if (!$isVisible) { |
| 89 | + * throw new RuntimeException("Not visible yet! Let's retry!"); |
| 90 | + * } |
| 91 | + * |
| 92 | + * return true; |
| 93 | + * } |
| 94 | + * ); |
| 95 | + * |
| 96 | + * return $becomeVisiblePromise; |
| 97 | + * } |
| 98 | + * ); |
| 99 | + * |
| 100 | + * $waitUntilPromise->then( |
| 101 | + * function () use ($webdriver) { |
| 102 | + * // try-catch |
| 103 | + * $webdriver->clickElement(...); // sending a click command only if we are sure the target is visible. |
| 104 | + * } |
| 105 | + * // handle case when the element is not visible on the page |
| 106 | + * ); |
| 107 | + * ``` |
| 108 | + * |
| 109 | + * @param float $time Time (in seconds) to wait for successfully resolved promise from the |
| 110 | + * condition callback |
| 111 | + * @param callable $conditionMetCallback A condition to be met, as a callback |
| 112 | + * |
| 113 | + * @return PromiseInterface<null> |
| 114 | + */ |
| 115 | + public function waitUntil(float $time, callable $conditionMetCallback): PromiseInterface; |
| 116 | +} |
0 commit comments