Skip to content

Commit

Permalink
DX improvements (#219)
Browse files Browse the repository at this point in the history
* Handled empty collections in criterions

* Changed Screenshot command to upload to Cloudinary

* Fixed executeJavaScript return type

* Print executed script before showing command
  • Loading branch information
mnocon authored Oct 19, 2021
1 parent a645388 commit 8f6fa5f
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 16 deletions.
10 changes: 10 additions & 0 deletions src/lib/Browser/Element/Criterion/ElementAttributeCriterion.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function matches(ElementInterface $element): bool
public function getErrorMessage(LocatorInterface $locator): string
{
return
$this->results ?
sprintf(
"Could not find element with attribute '%s' matching value '%s'. Found values: %s instead. %s locator '%s': '%s'.",
$this->attribute,
Expand All @@ -48,6 +49,15 @@ public function getErrorMessage(LocatorInterface $locator): string
$locator->getType(),
$locator->getIdentifier(),
$locator->getSelector()
)
:
sprintf(
"Could not find element with attribute '%s' matching value '%s'. Collection is empty. %s locator '%s': '%s'.",
$this->attribute,
$this->expectedAttributeValue,
$locator->getType(),
$locator->getIdentifier(),
$locator->getSelector()
);
}
}
9 changes: 9 additions & 0 deletions src/lib/Browser/Element/Criterion/ElementTextCriterion.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,22 @@ public function matches(ElementInterface $element): bool
public function getErrorMessage(LocatorInterface $locator): string
{
return
$this->results ?
sprintf(
"Could not find element named: '%s'. Found names: %s instead. %s locator '%s': '%s'.",
$this->expectedElementText,
implode(',', $this->results),
strtoupper($locator->getType()),
$locator->getIdentifier(),
$locator->getSelector()
)
:
sprintf(
"Could not find element named: '%s'. Collection is empty. %s locator '%s': '%s'.",
$this->expectedElementText,
strtoupper($locator->getType()),
$locator->getIdentifier(),
$locator->getSelector()
);
}
}
25 changes: 17 additions & 8 deletions src/lib/Browser/Element/Criterion/ElementTextFragmentCriterion.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,22 @@ public function matches(ElementInterface $element): bool
public function getErrorMessage(LocatorInterface $locator): string
{
return
sprintf(
"Could not find element with text containing: '%s'. Found texts: %s instead. %s locator '%s': '%s'.",
$this->expectedElementTextFragment,
implode(',', $this->results),
strtoupper($locator->getType()),
$locator->getIdentifier(),
$locator->getSelector()
);
$this->results ?
sprintf(
"Could not find element with text containing: '%s'. Found texts: %s instead. %s locator '%s': '%s'.",
$this->expectedElementTextFragment,
implode(',', $this->results),
strtoupper($locator->getType()),
$locator->getIdentifier(),
$locator->getSelector()
)
:
sprintf(
"Could not find element with text containing: '%s'. Collection is empty. %s locator '%s': '%s'.",
$this->expectedElementTextFragment,
strtoupper($locator->getType()),
$locator->getIdentifier(),
$locator->getSelector()
);
}
}
4 changes: 3 additions & 1 deletion src/lib/Browser/Element/Debug/Interactive/RootElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public function executeJavaScript(string $script): string
try {
return $this->element->executeJavaScript($script);
} catch (\Exception $exception) {
$this->startInteractiveSessionOnException($exception, false);
$exceptionWithScript = new Exception(sprintf('Script: %s, Error: %s', $script, $exception->getMessage()));

$this->startInteractiveSessionOnException($exceptionWithScript, true);
}
}
}
2 changes: 1 addition & 1 deletion src/lib/Browser/Element/RootElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ private function isDraggingLibraryLoaded(): bool

public function executeJavaScript(string $script): string
{
return $this->session->evaluateScript($script);
return (string) $this->session->evaluateScript($script) ?? '';
}
}
24 changes: 21 additions & 3 deletions src/lib/Core/Debug/Command/TakeScreenshotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@
namespace EzSystems\Behat\Core\Debug\Command;

use Behat\Mink\Session;
use Cloudinary;
use Cloudinary\Uploader;
use Exception;
use Psy\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class TakeScreenshotCommand extends Command
{
private const CLOUD_NAME_KEY = 'cloud_name';
private const PRESET_KEY = 'preset';
private const CLOUD_NAME = 'ezplatformtravis';
private const PRESET = 'ezplatform';

/** @var \Behat\Mink\Session */
protected $session;
private $session;

public function __construct(Session $session)
{
Expand All @@ -39,8 +47,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
$filePath = sys_get_temp_dir() . \DIRECTORY_SEPARATOR . uniqid('debug') . '.png';

file_put_contents($filePath, $image);
$output->writeln(sprintf('Screenshot saved to "%s"', $filePath));

return 0;
Cloudinary::config([self::CLOUD_NAME_KEY => self::CLOUD_NAME, self::PRESET_KEY => self::PRESET]);

try {
$response = Uploader::unsigned_upload($filePath, self::PRESET);
$output->writeln(sprintf('Open image at %s', $response['secure_url']));

return 0;
} catch (Exception $e) {
$output->writeln(sprintf('Error while uploading image. %s', $e->getMessage()));

return $e->getCode();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function dataProviderTestMatches(): array
];
}

public function testGetErrorMessage(): void
public function testGetErrorMessageWhenOtherElementFound(): void
{
$criterion = new ElementAttributeCriterion('expectedAttribute', 'expectedValue');
$nonMatchingElement = $this->createElementWithAttribute('expectedAttribute', 'notexpectedValue');
Expand All @@ -47,6 +47,16 @@ public function testGetErrorMessage(): void
);
}

public function testGetErrorMessageWhenNoElementFound(): void
{
$criterion = new ElementAttributeCriterion('expectedAttribute', 'expectedValue');

Assert::assertEquals(
"Could not find element with attribute 'expectedAttribute' matching value 'expectedValue'. Collection is empty. css locator 'id': 'selector'.",
$criterion->getErrorMessage(new CSSLocator('id', 'selector'))
);
}

private function createElementWithAttribute($attribute, $value): ElementInterface
{
$element = $this->createStub(ElementInterface::class);
Expand Down
12 changes: 11 additions & 1 deletion tests/Browser/Element/Criterion/ElementTextCriterionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function dataProviderTestMatches(): array
];
}

public function testGetErrorMessage(): void
public function testGetErrorMessageWhenCollectionHasElements(): void
{
$criterion = new ElementTextCriterion('expectedText');
$nonMatchingElement = $this->createElement('actualText');
Expand All @@ -45,4 +45,14 @@ public function testGetErrorMessage(): void
$criterion->getErrorMessage(new CSSLocator('id', 'selector'))
);
}

public function testGetErrorMessageWhenCollectionIsEmpty(): void
{
$criterion = new ElementTextCriterion('expectedText');

Assert::assertEquals(
"Could not find element named: 'expectedText'. Collection is empty. CSS locator 'id': 'selector'.",
$criterion->getErrorMessage(new CSSLocator('id', 'selector'))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function dataProviderTestMatches(): array
];
}

public function testGetErrorMessage(): void
public function testGetErrorMessageWhenOtherElementFound(): void
{
$criterion = new ElementTextFragmentCriterion('expectedText');
$nonMatchingElement = $this->createElement('actualText');
Expand All @@ -46,4 +46,14 @@ public function testGetErrorMessage(): void
$criterion->getErrorMessage(new CSSLocator('id', 'selector'))
);
}

public function testGetErrorMessageWhenNoElementFound(): void
{
$criterion = new ElementTextFragmentCriterion('expectedText');

Assert::assertEquals(
"Could not find element with text containing: 'expectedText'. Collection is empty. CSS locator 'id': 'selector'.",
$criterion->getErrorMessage(new CSSLocator('id', 'selector'))
);
}
}

0 comments on commit 8f6fa5f

Please sign in to comment.