Skip to content

Commit

Permalink
Merge pull request #633 from tienvx/update-clipboard
Browse files Browse the repository at this point in the history
Update clipboard
  • Loading branch information
tienvx committed Jul 17, 2022
2 parents ad7601e + 18b4aa8 commit 29537b5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/Command/Runner/CustomCommandRunner.php
Expand Up @@ -17,6 +17,7 @@ class CustomCommandRunner extends CommandRunner
public const UPLOAD = 'upload';
public const ASSERT_FILE_DOWNLOADED = 'assertFileDownloaded';
public const ASSERT_CLIPBOARD = 'assertClipboard';
public const UPDATE_CLIPBOARD = 'updateClipboard';

protected string $uploadDir;
protected string $webdriverUri;
Expand All @@ -41,6 +42,7 @@ public function getAllCommands(): array
self::UPLOAD,
self::ASSERT_FILE_DOWNLOADED,
self::ASSERT_CLIPBOARD,
self::UPDATE_CLIPBOARD,
];
}

Expand Down Expand Up @@ -69,12 +71,7 @@ public function run(CommandInterface $command, ValuesInterface $values, RemoteWe
try {
$code = $this->httpClient->request(
'GET',
sprintf(
'%s/download/%s/%s',
rtrim($this->webdriverUri, '/'),
$driver->getSessionID(),
$command->getTarget()
)
sprintf('%s/%s', $this->getUrl('download', $driver), $command->getTarget())
)->getStatusCode();
if (200 !== $code) {
throw new Exception(sprintf(
Expand All @@ -92,10 +89,7 @@ public function run(CommandInterface $command, ValuesInterface $values, RemoteWe
break;
case self::ASSERT_CLIPBOARD:
try {
$clipboard = $this->httpClient->request(
'GET',
sprintf('%s/clipboard/%s', rtrim($this->webdriverUri, '/'), $driver->getSessionID())
)->getContent();
$clipboard = $this->httpClient->request('GET', $this->getUrl('clipboard', $driver))->getContent();
if ($command->getTarget() !== $clipboard) {
throw new Exception(sprintf(
"Failed expecting that clipboard's content equals '%s', actual value '%s'",
Expand All @@ -110,6 +104,20 @@ public function run(CommandInterface $command, ValuesInterface $values, RemoteWe
));
}
break;
case self::UPDATE_CLIPBOARD:
try {
$this->httpClient->request(
'POST',
$this->getUrl('clipboard', $driver),
['body' => $command->getTarget()]
)->getStatusCode();
} catch (ExceptionInterface $e) {
throw new RuntimeException(sprintf(
'Can not update clipboard: %s',
$e->getMessage()
));
}
break;
default:
break;
}
Expand All @@ -119,4 +127,9 @@ protected function getFilePath(CommandInterface $command): string
{
return $this->uploadDir . DIRECTORY_SEPARATOR . (string) $command->getValue();
}

protected function getUrl(string $type, RemoteWebDriver $driver): string
{
return sprintf('%s/%s/%s', rtrim($this->webdriverUri, '/'), $type, $driver->getSessionID());
}
}
42 changes: 42 additions & 0 deletions tests/Command/Runner/CustomCommandRunnerTest.php
Expand Up @@ -174,6 +174,47 @@ public function testAssertClipboardThrowException(): void
$this->runner->run($command, $this->values, $this->driver);
}

public function testUpdateClipboard(): void
{
$command = new Command();
$command->setCommand(CustomCommandRunner::UPDATE_CLIPBOARD);
$command->setTarget('clipboard');
$response = $this->createMock(ResponseInterface::class);
$response->expects($this->once())->method('getStatusCode')->willReturn(123);
$this->driver->expects($this->once())->method('getSessionID')->willReturn($this->sessionId);
$this->httpClient
->expects($this->once())
->method('request')
->with(
'POST',
$this->webdriverUri . '/clipboard/' . $this->sessionId,
['body' => 'clipboard']
)
->willReturn($response);
$this->runner->run($command, $this->values, $this->driver);
}

public function testUpdateClipboardThrowException(): void
{
$command = new Command();
$command->setCommand(CustomCommandRunner::UPDATE_CLIPBOARD);
$command->setTarget('text');
$this->runner->setWebdriverUri($this->webdriverUri);
$this->driver->expects($this->once())->method('getSessionID')->willReturn($this->sessionId);
$this->httpClient
->expects($this->once())
->method('request')
->with(
'POST',
$this->webdriverUri . '/clipboard/' . $this->sessionId,
['body' => 'text']
)
->willThrowException(new HttpClientException('Something wrong'));
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Can not update clipboard: Something wrong');
$this->runner->run($command, $this->values, $this->driver);
}

public function targetProvider(): array
{
return [
Expand All @@ -189,6 +230,7 @@ public function commandsRequireTarget(): array
CustomCommandRunner::UPLOAD,
CustomCommandRunner::ASSERT_FILE_DOWNLOADED,
CustomCommandRunner::ASSERT_CLIPBOARD,
CustomCommandRunner::UPDATE_CLIPBOARD,
];
}

Expand Down

0 comments on commit 29537b5

Please sign in to comment.