Skip to content

Commit

Permalink
Upload command
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Jul 14, 2022
1 parent 0cdea1e commit 3beed74
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 15 deletions.
5 changes: 5 additions & 0 deletions src/Command/CommandRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ protected function getSelect(WebDriverElement $element): WebDriverSelect
{
return new WebDriverSelect($element);
}

public function validateTarget(CommandInterface $command): bool
{
return $command->getTarget() && $this->isValidSelector($command->getTarget());
}
}
54 changes: 54 additions & 0 deletions src/Command/Runner/CustomCommandRunner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Command\Runner;

use Facebook\WebDriver\Remote\LocalFileDetector;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Tienvx\Bundle\MbtBundle\Command\CommandRunner;
use Tienvx\Bundle\MbtBundle\Model\Model\Revision\CommandInterface;
use Tienvx\Bundle\MbtBundle\Model\ValuesInterface;

class CustomCommandRunner extends CommandRunner
{
public const UPLOAD = 'upload';

public function __construct(protected string $uploadDir)
{
}

public function getAllCommands(): array
{
return [
self::UPLOAD,
];
}

public function getCommandsRequireTarget(): array
{
return $this->getAllCommands();
}

public function getCommandsRequireValue(): array
{
return $this->getAllCommands();
}

public function run(CommandInterface $command, ValuesInterface $values, RemoteWebDriver $driver): void
{
switch ($command->getCommand()) {
case self::UPLOAD:
$driver
->findElement($this->getSelector($command->getTarget()))
->setFileDetector(new LocalFileDetector())
->sendKeys($this->getFilePath($command));
break;
default:
break;
}
}

protected function getFilePath(CommandInterface $command): string
{
return $this->uploadDir . DIRECTORY_SEPARATOR . (string) $command->getValue();
}
}
5 changes: 0 additions & 5 deletions src/Command/Runner/KeyboardCommandRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ public function run(CommandInterface $command, ValuesInterface $values, RemoteWe
}
}

public function validateTarget(CommandInterface $command): bool
{
return $command->getTarget() && $this->isValidSelector($command->getTarget());
}

/**
* Don't allow to upload local file.
*/
Expand Down
5 changes: 0 additions & 5 deletions src/Command/Runner/MouseCommandRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,6 @@ public function run(CommandInterface $command, ValuesInterface $values, RemoteWe
}
}

public function validateTarget(CommandInterface $command): bool
{
return $command->getTarget() && $this->isValidSelector($command->getTarget());
}

protected function getPoint(string $target): WebDriverPoint
{
list($x, $y) = explode(',', $target);
Expand Down
5 changes: 0 additions & 5 deletions src/Command/Runner/WaitCommandRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,4 @@ public function run(CommandInterface $command, ValuesInterface $values, RemoteWe
break;
}
}

public function validateTarget(CommandInterface $command): bool
{
return $command->getTarget() && $this->isValidSelector($command->getTarget());
}
}
3 changes: 3 additions & 0 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Tienvx\Bundle\MbtBundle\Command\CommandRunnerManagerInterface;
use Tienvx\Bundle\MbtBundle\Command\Runner\AlertCommandRunner;
use Tienvx\Bundle\MbtBundle\Command\Runner\AssertionRunner;
use Tienvx\Bundle\MbtBundle\Command\Runner\CustomCommandRunner;
use Tienvx\Bundle\MbtBundle\Command\Runner\KeyboardCommandRunner;
use Tienvx\Bundle\MbtBundle\Command\Runner\MouseCommandRunner;
use Tienvx\Bundle\MbtBundle\Command\Runner\ScriptCommandRunner;
Expand Down Expand Up @@ -196,6 +197,8 @@
->autoconfigure(true)
->set(WindowCommandRunner::class)
->autoconfigure(true)
->set(CustomCommandRunner::class)
->autoconfigure(true)

// Repositories
->set(BugRepository::class)
Expand Down

0 comments on commit 3beed74

Please sign in to comment.