Skip to content

Commit

Permalink
Merge pull request #635 from tienvx/refactor-commands
Browse files Browse the repository at this point in the history
Refactor commands
  • Loading branch information
tienvx authored Jul 24, 2022
2 parents a0b29e1 + 329a50b commit 9b39302
Show file tree
Hide file tree
Showing 189 changed files with 7,773 additions and 4,152 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"symfony/config": "^6.1",
"symfony/dependency-injection": "^6.1",
"symfony/expression-language": "^6.1",
"symfony/http-client-contracts": "^3.1",
"symfony/http-client": "^6.1",
"symfony/http-kernel": "^6.1",
"symfony/messenger": "^6.1",
"symfony/validator": "^6.1",
Expand Down
39 changes: 6 additions & 33 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
use Tienvx\AssignmentsEvaluator\AssignmentsEvaluator;
use Tienvx\Bundle\MbtBundle\Channel\ChannelManager;
use Tienvx\Bundle\MbtBundle\Channel\ChannelManagerInterface;
use Tienvx\Bundle\MbtBundle\Command\CommandPreprocessor;
use Tienvx\Bundle\MbtBundle\Command\CommandPreprocessorInterface;
use Tienvx\Bundle\MbtBundle\Command\CommandRunnerInterface;
use Tienvx\Bundle\MbtBundle\Command\CommandRunnerManager;
use Tienvx\Bundle\MbtBundle\Command\CommandRunnerManagerInterface;
use Tienvx\Bundle\MbtBundle\Command\CommandManager;
use Tienvx\Bundle\MbtBundle\Command\CommandManagerInterface;
use Tienvx\Bundle\MbtBundle\Command\Runner\AlertCommandRunner;
use Tienvx\Bundle\MbtBundle\Command\Runner\AssertionRunner;
use Tienvx\Bundle\MbtBundle\Command\Runner\CustomCommandRunner;
Expand Down Expand Up @@ -166,43 +164,18 @@
->set(TagsValidator::class)
->set(ValidCommandValidator::class)
->args([
service(CommandRunnerManagerInterface::class),
service(CommandManagerInterface::class),
])
->tag('validator.constraint_validator', [
'alias' => ValidCommandValidator::class,
])

// Commands
->set(CommandPreprocessor::class)
->alias(CommandPreprocessorInterface::class, CommandPreprocessor::class)
->set(CommandRunnerManager::class)
->args([
tagged_iterator(CommandRunnerInterface::TAG),
service(CommandPreprocessorInterface::class),
])
->alias(CommandRunnerManagerInterface::class, CommandRunnerManager::class)

->set(AlertCommandRunner::class)
->autoconfigure(true)
->set(AssertionRunner::class)
->autoconfigure(true)
->set(KeyboardCommandRunner::class)
->autoconfigure(true)
->set(MouseCommandRunner::class)
->autoconfigure(true)
->set(ScriptCommandRunner::class)
->autoconfigure(true)
->set(StoreCommandRunner::class)
->autoconfigure(true)
->set(WaitCommandRunner::class)
->autoconfigure(true)
->set(WindowCommandRunner::class)
->autoconfigure(true)
->set(CustomCommandRunner::class)
->autoconfigure(true)
->set(CommandManager::class)
->args([
service(HttpClientInterface::class),
])
->alias(CommandManagerInterface::class, CommandManager::class)

// Repositories
->set(BugRepository::class)
Expand Down Expand Up @@ -261,7 +234,7 @@

->set(StepRunner::class)
->args([
service(CommandRunnerManagerInterface::class),
service(CommandManagerInterface::class),
])
->alias(StepRunnerInterface::class, StepRunner::class)

Expand Down
43 changes: 35 additions & 8 deletions src/Command/CommandRunner.php → src/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,53 @@

namespace Tienvx\Bundle\MbtBundle\Command;

use Facebook\WebDriver\Exception\UnexpectedTagNameException;
use Exception;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverElement;
use Facebook\WebDriver\WebDriverSelect;
use Tienvx\Bundle\MbtBundle\Exception\UnexpectedValueException;
use Tienvx\Bundle\MbtBundle\Model\Model\Revision\CommandInterface;
use Tienvx\Bundle\MbtBundle\Model\ValuesInterface;

abstract class CommandRunner implements CommandRunnerInterface
abstract class AbstractCommand implements CommandInterface
{
public function supports(CommandInterface $command): bool
public static function getValueHelper(): string
{
return in_array($command->getCommand(), $this->getAllCommands());
return '';
}

protected function isValidSelector(string $target): bool
public static function getTargetHelper(): string
{
return "Locator e.g. 'id=email' or 'css=.last-name'";
}

public static function validateTarget(?string $target): bool
{
return $target && static::isValidSelector($target);
}

protected static function isValidSelector(string $target): bool
{
list($mechanism) = explode('=', $target, 2);

return in_array($mechanism, static::MECHANISMS);
}

public function validateValue(?string $value): bool
{
return !static::isValueRequired() || !is_null($value);
}

public function run(?string $target, ?string $value, ValuesInterface $values, RemoteWebDriver $driver): void
{
if (static::isTargetRequired() && empty($target)) {
throw new UnexpectedValueException('Target is required');
}
if (static::isValueRequired() && empty($value)) {
throw new UnexpectedValueException('Value is required');
}
}

protected function getSelector(string $target): WebDriverBy
{
list($mechanism, $value) = explode('=', $target, 2);
Expand Down Expand Up @@ -59,8 +84,10 @@ protected function getSelect(WebDriverElement $element): WebDriverSelect
return new WebDriverSelect($element);
}

public function validateTarget(CommandInterface $command): bool
protected function assert(bool $assertion, string $message): void
{
return $command->getTarget() && $this->isValidSelector($command->getTarget());
if (!$assertion) {
throw new Exception($message);
}
}
}
23 changes: 23 additions & 0 deletions src/Command/Alert/AbstractAlertCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Command\Alert;

use Tienvx\Bundle\MbtBundle\Command\AbstractCommand;

abstract class AbstractAlertCommand extends AbstractCommand
{
public static function getGroup(): string
{
return 'alert';
}

public static function validateTarget(?string $target): bool
{
return true;
}

public static function getTargetHelper(): string
{
return '';
}
}
25 changes: 25 additions & 0 deletions src/Command/Alert/AcceptAlertCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Command\Alert;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Tienvx\Bundle\MbtBundle\Model\ValuesInterface;

class AcceptAlertCommand extends AbstractAlertCommand
{
public static function isTargetRequired(): bool
{
return false;
}

public static function isValueRequired(): bool
{
return false;
}

public function run(?string $target, ?string $value, ValuesInterface $values, RemoteWebDriver $driver): void
{
parent::run($target, $value, $values, $driver);
$driver->switchTo()->alert()->accept();
}
}
37 changes: 37 additions & 0 deletions src/Command/Alert/AnswerPromptCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Command\Alert;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Tienvx\Bundle\MbtBundle\Model\ValuesInterface;

class AnswerPromptCommand extends AbstractAlertCommand
{
public static function isTargetRequired(): bool
{
return true;
}

public static function isValueRequired(): bool
{
return false;
}

public static function validateTarget(?string $target): bool
{
return !is_null($target);
}

public static function getTargetHelper(): string
{
return "Propt's answer";
}

public function run(?string $target, ?string $value, ValuesInterface $values, RemoteWebDriver $driver): void
{
parent::run($target, $value, $values, $driver);
$alert = $driver->switchTo()->alert();
$alert->sendKeys($target);
$alert->accept();
}
}
25 changes: 25 additions & 0 deletions src/Command/Alert/DismissPromptCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Command\Alert;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Tienvx\Bundle\MbtBundle\Model\ValuesInterface;

class DismissPromptCommand extends AbstractAlertCommand
{
public static function isTargetRequired(): bool
{
return false;
}

public static function isValueRequired(): bool
{
return false;
}

public function run(?string $target, ?string $value, ValuesInterface $values, RemoteWebDriver $driver): void
{
parent::run($target, $value, $values, $driver);
$driver->switchTo()->alert()->dismiss();
}
}
13 changes: 13 additions & 0 deletions src/Command/Assert/AbstractAssertCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Command\Assert;

use Tienvx\Bundle\MbtBundle\Command\AbstractCommand;

abstract class AbstractAssertCommand extends AbstractCommand
{
public static function getGroup(): string
{
return 'assert';
}
}
39 changes: 39 additions & 0 deletions src/Command/Assert/AssertAlertCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Command\Assert;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Tienvx\Bundle\MbtBundle\Model\ValuesInterface;

class AssertAlertCommand extends AbstractAssertCommand
{
public static function isTargetRequired(): bool
{
return true;
}

public static function isValueRequired(): bool
{
return false;
}

public static function getTargetHelper(): string
{
return 'Expected value';
}

public static function validateTarget(?string $target): bool
{
return !is_null($target);
}

public function run(?string $target, ?string $value, ValuesInterface $values, RemoteWebDriver $driver): void
{
parent::run($target, $value, $values, $driver);
$alertText = $driver->switchTo()->alert()->getText();
$this->assert(
$alertText === $target,
sprintf('Actual alert text "%s" did not match "%s"', $alertText, $target)
);
}
}
28 changes: 28 additions & 0 deletions src/Command/Assert/AssertCheckedCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Command\Assert;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Tienvx\Bundle\MbtBundle\Model\ValuesInterface;

class AssertCheckedCommand extends AbstractAssertCommand
{
public static function isTargetRequired(): bool
{
return true;
}

public static function isValueRequired(): bool
{
return false;
}

public function run(?string $target, ?string $value, ValuesInterface $values, RemoteWebDriver $driver): void
{
parent::run($target, $value, $values, $driver);
$this->assert(
$driver->findElement($this->getSelector($target))->isSelected(),
sprintf('Element "%s" is not checked, expected to be checked', $target)
);
}
}
44 changes: 44 additions & 0 deletions src/Command/Assert/AssertCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Command\Assert;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Tienvx\Bundle\MbtBundle\Model\ValuesInterface;

class AssertCommand extends AbstractAssertCommand
{
public static function isTargetRequired(): bool
{
return true;
}

public static function isValueRequired(): bool
{
return true;
}

public static function getTargetHelper(): string
{
return 'Variable to get value from';
}

public static function getValueHelper(): string
{
return 'Expected value';
}

public static function validateTarget(?string $target): bool
{
return !empty($target);
}

public function run(?string $target, ?string $value, ValuesInterface $values, RemoteWebDriver $driver): void
{
parent::run($target, $value, $values, $driver);
$actual = $values->getValue($target);
$this->assert(
$actual === $value,
sprintf('Actual value "%s" did not match "%s"', $actual, $value)
);
}
}
Loading

0 comments on commit 9b39302

Please sign in to comment.