diff --git a/src/Command/Runner/CustomCommandRunner.php b/src/Command/Runner/CustomCommandRunner.php index 14525e9f..5b45c39f 100644 --- a/src/Command/Runner/CustomCommandRunner.php +++ b/src/Command/Runner/CustomCommandRunner.php @@ -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; @@ -41,6 +42,7 @@ public function getAllCommands(): array self::UPLOAD, self::ASSERT_FILE_DOWNLOADED, self::ASSERT_CLIPBOARD, + self::UPDATE_CLIPBOARD, ]; } @@ -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( @@ -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'", @@ -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; } @@ -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()); + } } diff --git a/tests/Command/Runner/CustomCommandRunnerTest.php b/tests/Command/Runner/CustomCommandRunnerTest.php index a3fefcc9..0bb2b7d0 100644 --- a/tests/Command/Runner/CustomCommandRunnerTest.php +++ b/tests/Command/Runner/CustomCommandRunnerTest.php @@ -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 [ @@ -189,6 +230,7 @@ public function commandsRequireTarget(): array CustomCommandRunner::UPLOAD, CustomCommandRunner::ASSERT_FILE_DOWNLOADED, CustomCommandRunner::ASSERT_CLIPBOARD, + CustomCommandRunner::UPDATE_CLIPBOARD, ]; }