Skip to content

Commit

Permalink
[#132] Replaced URL-encoded characters in the screenshot file names.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Jan 18, 2025
1 parent 957dd0a commit 7846775
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/DrevOps/BehatScreenshotExtension/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ protected static function replaceUrlToken(string $token, string $name, ?string $
$url_parts = parse_url($url);

if (!$url_parts) {
throw new \RuntimeException(sprintf('Could not parse URL "%s".', $url));
return $token;
}

switch ($qualifier) {
Expand Down Expand Up @@ -223,7 +223,7 @@ protected static function replaceUrlToken(string $token, string $name, ?string $
break;
}

$replacement = urlencode($replacement);
$replacement = preg_replace('/[^\w\-]+/', '_', $replacement) ?: $replacement;
}

return $replacement;
Expand Down
24 changes: 12 additions & 12 deletions tests/behat/features/selenium.feature
Original file line number Diff line number Diff line change
Expand Up @@ -25,43 +25,43 @@ Feature: Selenium screenshots
Scenario: Capture a screenshot with name using Selenium driver with a custom name containing URL domain and path tokens
Given I am on the screenshot test page
When I save screenshot with name "test.{url_domain}.{url_path}.{ext}"
Then file wildcard "*.example\.com.screenshot\.html\.png" should exist
And file wildcard "*.example\.com.screenshot\.html\.html" should exist
Then file wildcard "test.example_com.screenshot_html.html" should exist
And file wildcard "test.example_com.screenshot_html.png" should exist

@phpserver @javascript
Scenario: Capture a screenshot with name using Selenium driver with a custom name containing URL origin token
Given I am on the screenshot test page
When I save screenshot with name "test.{url_origin}.{ext}"
Then file wildcard "*.http%3A%2F%2Fexample\.com\.png" should exist
And file wildcard "*.http%3A%2F%2Fexample\.com\.html" should exist
Then file wildcard "test.http_example_com.html" should exist
And file wildcard "test.http_example_com.png" should exist

@phpserver @javascript
Scenario: Capture a screenshot with name using Selenium driver with a custom name containing URL string token
Given I am on the screenshot test page
When I save screenshot with name "test.{url}.{ext}"
Then file wildcard "*.http%3A%2F%2Fexample\.com%3A8888%2Fscreenshot.html.png" should exist
And file wildcard "*.http%3A%2F%2Fexample\.com%3A8888%2Fscreenshot.html.html" should exist
Then file wildcard "test.http_example_com_8888_screenshot_html.html" should exist
And file wildcard "test.http_example_com_8888_screenshot_html.png" should exist

@phpserver @javascript
Scenario: Capture a screenshot with name using Selenium driver with a custom name containing URL query and fragment tokens
Given I am on the screenshot test page with query "foo=test-foo" and fragment "foo-fragment"
When I save screenshot with name "test.{url_query}.{url_fragment}.{ext}"
Then file wildcard "*.foo%3Dtest-foo.foo-fragment.png" should exist
And file wildcard "*.foo%3Dtest-foo.foo-fragment.html" should exist
Then file wildcard "test.foo_test-foo.foo-fragment.html" should exist
And file wildcard "test.foo_test-foo.foo-fragment.png" should exist

@phpserver @javascript
Scenario: Capture a screenshot with name using Selenium driver with a custom name containing URL relative path token
Given I am on the screenshot test page with query "foo=test-foo" and fragment "foo-fragment"
When I save screenshot with name "test.{url_relative}.{ext}"
Then file wildcard "*.screenshot.html%3Ffoo%3Dtest-foo%23foo-fragment.png" should exist
And file wildcard "*.screenshot.html%3Ffoo%3Dtest-foo%23foo-fragment.html" should exist
Then file wildcard "test.screenshot_html_foo_test-foo_foo-fragment.html" should exist
And file wildcard "test.screenshot_html_foo_test-foo_foo-fragment.png" should exist

@phpserver @javascript
Scenario: Capture a screenshot with name using Selenium driver with a custom name containing URL query and fragment in a URL token
Given I am on the screenshot test page with query "foo=test-foo" and fragment "foo-fragment"
When I save screenshot with name "test.{url}.{ext}"
Then file wildcard "*.http%3A%2F%2Fexample\.com%3A8888%2Fscreenshot.html%3Ffoo%3Dtest-foo%23foo-fragment.png" should exist
And file wildcard "*.http%3A%2F%2Fexample\.com%3A8888%2Fscreenshot.html%3Ffoo%3Dtest-foo%23foo-fragment.html" should exist
Then file wildcard "test.http_example_com_8888_screenshot_html_foo_test-foo_foo-fragment.html" should exist
And file wildcard "test.http_example_com_8888_screenshot_html_foo_test-foo_foo-fragment.png" should exist

@phpserver @javascript
Scenario: Capture a screenshot with name using Selenium driver with a custom name containing step line and name tokens
Expand Down
42 changes: 18 additions & 24 deletions tests/phpunit/Unit/Tokenizer/TokenizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,47 +144,41 @@ public static function dataProviderReplaceFailToken(): array {
}

#[DataProvider('dataProviderReplaceUrlToken')]
public function testReplaceUrlToken(string $token, string $name, ?string $qualifier, ?string $format, array $data, string $expected, ?string $exception = NULL): void {
if ($exception) {
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage($exception);
}

public function testReplaceUrlToken(string $token, string $name, ?string $qualifier, ?string $format, array $data, string $expected): void {
$replacement = $this->callProtectedMethod(Tokenizer::class, 'replaceUrlToken', [$token, $name, $qualifier, $format, $data]);
$this->assertEquals($expected, $replacement);
}

public static function dataProviderReplaceUrlToken(): array {
$url = 'http://example.com/foo?foo=foo-value#hello-fragment';

return [
['{url}', 'url', NULL, NULL, [], '{url}'],
['{url}', 'url', NULL, NULL, ['url' => $url], urlencode($url)],
['{url}', 'url', NULL, 'relative', ['url' => $url], urlencode($url)],
['{url}', 'url', NULL, NULL, ['url' => 'http:///example.com'], '', 'Could not parse URL "http:///example.com".'],
['{url}', 'url', NULL, NULL, ['url' => 'http://e.com/path?f1=f1-v1#frag'], 'http_e_com_path_f1_f1-v1_frag'],

['{url}', 'url', NULL, 'relative', ['url' => 'http://e.com/path?f1=f1-v1#frag'], 'http_e_com_path_f1_f1-v1_frag'],
['{url}', 'url', NULL, NULL, ['url' => 'http:///e.com/path?f1=f1-v1#frag'], '{url}'],

['{url_relative}', 'url', 'relative', NULL, ['url' => $url], urlencode('foo?foo=foo-value#hello-fragment')],
['{url_relative}', 'url', NULL, NULL, ['url' => $url], urlencode($url)],
['{url_relative}', 'url', 'relative', NULL, ['url' => 'http://e.com/path?f1=f1-v1#frag'], urlencode('path_f1_f1-v1_frag')],
['{url_relative}', 'url', NULL, NULL, ['url' => 'http://e.com/path?f1=f1-v1#frag'], 'http_e_com_path_f1_f1-v1_frag'],
['{url_relative}', 'url', NULL, NULL, [], '{url_relative}'],

['{url_origin}', 'url', 'origin', NULL, ['url' => $url], urlencode('http://example.com')],
['{url_origin}', 'url', NULL, NULL, ['url' => $url], urlencode($url)],
['{url_origin}', 'url', 'origin', NULL, ['url' => 'http://e.com/path?f1=f1-v1#frag'], 'http_e_com'],
['{url_origin}', 'url', NULL, NULL, ['url' => 'http://e.com/path?f1=f1-v1#frag'], 'http_e_com_path_f1_f1-v1_frag'],
['{url_origin}', 'url', NULL, NULL, [], '{url_origin}'],

['{url_domain}', 'url', 'domain', NULL, ['url' => $url], urlencode('example.com')],
['{url_domain}', 'url', NULL, NULL, ['url' => $url], urlencode($url)],
['{url_domain}', 'url', 'domain', NULL, ['url' => 'http://e.com/path?f1=f1-v1#frag'], 'e_com'],
['{url_domain}', 'url', NULL, NULL, ['url' => 'http://e.com/path?f1=f1-v1#frag'], 'http_e_com_path_f1_f1-v1_frag'],
['{url_domain}', 'url', NULL, NULL, [], '{url_domain}'],

['{url_path}', 'url', 'path', NULL, ['url' => 'http://example.com/foo?foo=foo-value#hello-fragment'], 'foo'],
['{url_path}', 'url', NULL, NULL, ['url' => 'http://example.com/foo?foo=foo-value#hello-fragment'], urlencode($url)],
['{url_path}', 'url', 'path', NULL, ['url' => 'http://e.com/path?f1=f1-v1#frag'], 'path'],
['{url_path}', 'url', NULL, NULL, ['url' => 'http://e.com/path?f1=f1-v1#frag'], 'http_e_com_path_f1_f1-v1_frag'],
['{url_path}', 'url', NULL, NULL, [], '{url_path}'],

['{url_query}', 'url', 'query', NULL, ['url' => $url], urlencode('foo=foo-value')],
['{url_query}', 'url', NULL, NULL, ['url' => $url], urlencode($url)],
['{url_query}', 'url', 'query', NULL, ['url' => 'http://e.com/path?f1=f1-v1#frag'], 'f1_f1-v1'],
['{url_query}', 'url', NULL, NULL, ['url' => 'http://e.com/path?f1=f1-v1#frag'], 'http_e_com_path_f1_f1-v1_frag'],
['{url_query}', 'url', NULL, NULL, [], '{url_query}'],

['{url_fragment}', 'url', 'fragment', NULL, ['url' => $url], 'hello-fragment'],
['{url_fragment}', 'url', NULL, NULL, ['url' => $url], urlencode($url)],
['{url_fragment}', 'url', 'fragment', NULL, ['url' => 'http://e.com/path?f1=f1-v1#frag'], 'frag'],
['{url_fragment}', 'url', NULL, NULL, ['url' => 'http://e.com/path?f1=f1-v1#frag'], 'http_e_com_path_f1_f1-v1_frag'],
['{url_fragment}', 'url', NULL, NULL, [], '{url_fragment}'],
];
}
Expand Down Expand Up @@ -230,7 +224,7 @@ public static function dataProviderReplaceTokens(): array {
[
'{url}.{ext}',
$data,
'http%3A%2F%2Fexample.com%2Ffoo%3Ffoo%3Dfoo-value%23hello-fragment.png',
'http_example_com_foo_foo_foo-value_hello-fragment.png',
],
[
'{nontoken}.{ext}',
Expand Down

0 comments on commit 7846775

Please sign in to comment.