-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[7.x] Keyboard Support Improvements (#1053)
* wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * formatting --------- Signed-off-by: Mior Muhammad Zaki <[email protected]> Co-authored-by: StyleCI Bot <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
- Loading branch information
1 parent
e9653cd
commit d9c722c
Showing
6 changed files
with
185 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
/vendor | ||
composer.lock | ||
/phpunit.xml | ||
.phpunit.cache/* | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Laravel\Dusk\Concerns; | ||
|
||
use Facebook\WebDriver\WebDriverKeys; | ||
use Illuminate\Support\Str; | ||
use Laravel\Dusk\Keyboard; | ||
|
||
trait InteractsWithKeyboard | ||
{ | ||
/** | ||
* Execute the given callback while interacting with the keyboard. | ||
* | ||
* @param callable(\Laravel\Dusk\Keyboard):void $callback | ||
* @return $this | ||
*/ | ||
public function withKeyboard(callable $callback) | ||
{ | ||
return tap($this, fn () => $callback(new Keyboard($this))); | ||
} | ||
|
||
/** | ||
* Parse the keys before sending to the keyboard. | ||
* | ||
* @param array $keys | ||
* @return array | ||
*/ | ||
protected function parseKeys($keys) | ||
{ | ||
return collect($keys)->map(function ($key) { | ||
if (is_string($key) && Str::startsWith($key, '{') && Str::endsWith($key, '}')) { | ||
$key = constant(WebDriverKeys::class.'::'.strtoupper(trim($key, '{}'))); | ||
} | ||
|
||
if (is_array($key) && Str::startsWith($key[0], '{')) { | ||
$key[0] = constant(WebDriverKeys::class.'::'.strtoupper(trim($key[0], '{}'))); | ||
} | ||
|
||
return $key; | ||
})->all(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace Laravel\Dusk; | ||
|
||
use BadMethodCallException; | ||
use Illuminate\Support\Traits\Macroable; | ||
|
||
/** | ||
* @mixin \Facebook\WebDriver\Remote\RemoteKeyboard | ||
*/ | ||
class Keyboard | ||
{ | ||
use Macroable { | ||
__call as macroCall; | ||
} | ||
|
||
/** | ||
* The browser instance. | ||
* | ||
* @var \Laravel\Dusk\Browser | ||
*/ | ||
public $browser; | ||
|
||
/** | ||
* Create a keyboard instance. | ||
* | ||
* @param \Laravel\Dusk\Browser $browser | ||
* @return void | ||
*/ | ||
public function __construct(Browser $browser) | ||
{ | ||
$this->browser = $browser; | ||
} | ||
|
||
/** | ||
* Press the key using keyboard. | ||
* | ||
* @return $this | ||
*/ | ||
public function press($key) | ||
{ | ||
$this->pressKey($key); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Release the given pressed key. | ||
* | ||
* @return $this | ||
*/ | ||
public function release($key) | ||
{ | ||
$this->releaseKey($key); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Type the given keys using keyboard. | ||
* | ||
* @param string|array<int, string> $keys | ||
* @return $this | ||
*/ | ||
public function type($keys) | ||
{ | ||
$this->sendKeys($keys); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Dynamically call a method on the keyboard. | ||
* | ||
* @param string $method | ||
* @param array $parameters | ||
* @return mixed | ||
* | ||
* @throws \BadMethodCallException | ||
*/ | ||
public function __call($method, $parameters) | ||
{ | ||
if (static::hasMacro($method)) { | ||
return $this->macroCall($method, $parameters); | ||
} | ||
|
||
$keyboard = $this->browser->driver->getKeyboard(); | ||
|
||
if (method_exists($keyboard, $method)) { | ||
$response = $keyboard->{$method}(...$parameters); | ||
|
||
if ($response === $keyboard) { | ||
return $this; | ||
} else { | ||
return $response; | ||
} | ||
} | ||
|
||
throw new BadMethodCallException("Call to undefined keyboard method [{$method}]."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters