From ae6d4f116cca6b26c683bc2ecd40b18b0befce09 Mon Sep 17 00:00:00 2001 From: elwafa Date: Sat, 22 Feb 2025 12:33:26 +0200 Subject: [PATCH 1/3] prepare for relase v0.9.2 --- composer.json | 2 +- src/Platform.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 163f51d..410038f 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "volt-test/php-sdk", "description": "Volt Test PHP SDK - A performance testing tool for PHP applications", "type": "library", - "version": "0.9.1", + "version": "0.9.2", "keywords": [ "volt-test", "php-sdk", diff --git a/src/Platform.php b/src/Platform.php index 85f5de7..d7aed74 100644 --- a/src/Platform.php +++ b/src/Platform.php @@ -6,7 +6,7 @@ class Platform { private const BINARY_NAME = 'volt-test'; - private const ENGINE_CURRENT_VERSION = 'v0.9.1'; + private const ENGINE_CURRENT_VERSION = 'v0.9.2'; private const BASE_DOWNLOAD_URL = 'https://github.com/volt-test/binaries/releases/download'; private const SUPPORTED_PLATFORMS = [ 'linux-amd64' => 'volt-test-linux-amd64', From 51bdbad7eb86b654d8693ff49390d0b798be3693 Mon Sep 17 00:00:00 2001 From: Islam A-Elwafa Date: Sat, 22 Feb 2025 13:14:29 +0200 Subject: [PATCH 2/3] feat(extractor): Add HTMLExtractor to PHP SDK for structured HTML data extraction (#24) * feat(extractor): Add HTMLExtractor to PHP SDK for structured HTML data extraction - Implemented `HTMLExtractor.php` to define HTML extraction rules in the Volt-Test PHP SDK. - Supports: - CSS selectors for targeting specific elements. - Extracting text content from nested elements. - Extracting attributes (e.g., `value`, `href`, `src`). - Added `toArray()` method for serialization, ensuring consistency with existing extractors. - Aligns with the existing `JsonExtractor`, `RegexExtractor`, `HeaderExtractor`, and `CookieExtractor`. --- src/Extractors/HtmlExtractor.php | 55 +++++++++++++++++++++++++++ src/Step.php | 9 +++++ tests/Units/HtmlExtractorTest.php | 63 +++++++++++++++++++++++++++++++ tests/Units/StepTest.php | 31 +++++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 src/Extractors/HtmlExtractor.php create mode 100644 tests/Units/HtmlExtractorTest.php diff --git a/src/Extractors/HtmlExtractor.php b/src/Extractors/HtmlExtractor.php new file mode 100644 index 0000000..83ce044 --- /dev/null +++ b/src/Extractors/HtmlExtractor.php @@ -0,0 +1,55 @@ +variableName = $variableName; + $this->selector = $selector; + $this->attribute = $attribute; + } + + public function toArray(): array + { + if (! $this->validate()) { + throw new VoltTestException('Invalid HTML Extractor'); + } + + return [ + 'variable_name' => $this->variableName, + 'selector' => $this->selector, + 'attribute' => $this->attribute, + 'type' => 'html', + ]; + } + + public function validate(): bool + { + if (trim($this->variableName) === '') { + return false; + } + + // Check for empty or whitespace-only selector + if (trim($this->selector) === '') { + return false; + } + + // Check for empty or whitespace-only attribute + if ($this->attribute !== null && trim($this->attribute) === '') { + return false; + } + + return true; + } +} \ No newline at end of file diff --git a/src/Step.php b/src/Step.php index 6eb29a5..009bf55 100644 --- a/src/Step.php +++ b/src/Step.php @@ -10,6 +10,7 @@ use VoltTest\Extractors\CookieExtractor; use VoltTest\Extractors\Extractor; use VoltTest\Extractors\HeaderExtractor; +use VoltTest\Extractors\HtmlExtractor; use VoltTest\Extractors\JsonExtractor; use VoltTest\Extractors\RegexExtractor; use VoltTest\Validators\StatusValidator; @@ -191,6 +192,14 @@ public function extractFromRegex(string $variableName, string $selector): self return $this; } + + public function extractFromHtml(string $variableName, string $selector, ?string $attribute = null): self + { + $htmlExtractor = new HtmlExtractor($variableName, $selector, $attribute); + $this->extracts[] = $htmlExtractor; + return $this; + } + public function validateStatus(string $name, int $expected): self { $this->validations[] = new StatusValidator($name, $expected); diff --git a/tests/Units/HtmlExtractorTest.php b/tests/Units/HtmlExtractorTest.php new file mode 100644 index 0000000..e6d0dfc --- /dev/null +++ b/tests/Units/HtmlExtractorTest.php @@ -0,0 +1,63 @@ +htmlExtractor = new \VoltTest\Extractors\HtmlExtractor('testVar', 'div#test', 'data-id'); + } + + public function testToArray(): void + { + $expected = [ + 'variable_name' => 'testVar', + 'selector' => 'div#test', + 'attribute' => 'data-id', + 'type' => 'html', + ]; + $this->assertEquals($expected, $this->htmlExtractor->toArray()); + } + + public function testValidate(): void + { + $this->assertTrue($this->htmlExtractor->validate()); + } + + public function testEmptyVariableName(): void + { + $extractor = new \VoltTest\Extractors\HtmlExtractor('', 'div#test', 'data-id'); + $this->assertFalse($extractor->validate()); + } + + public function testEmptySelector(): void + { + $extractor = new \VoltTest\Extractors\HtmlExtractor('testVar', '', 'data-id'); + $this->assertFalse($extractor->validate()); + } + + public function testEmptyAttribute(): void + { + $extractor = new \VoltTest\Extractors\HtmlExtractor('testVar', 'div#test', ''); + $this->assertFalse($extractor->validate()); + } + + public function testNullAttribute(): void + { + $extractor = new \VoltTest\Extractors\HtmlExtractor('testVar', 'div#test', null); + $this->assertTrue($extractor->validate()); + } + + public function testEmptyAttributeWithNullSelector(): void + { + $this->expectException(\VoltTest\Exceptions\VoltTestException::class); + $extractor = new \VoltTest\Extractors\HtmlExtractor('testVar', 'div#date', ''); + $extractor->toArray(); + + } +} \ No newline at end of file diff --git a/tests/Units/StepTest.php b/tests/Units/StepTest.php index 8419b1e..8bb9fb4 100644 --- a/tests/Units/StepTest.php +++ b/tests/Units/StepTest.php @@ -117,6 +117,37 @@ public function testExtractFromJson(): void $this->assertEquals('json', $extract['type']); } + public function testExtractFromHtml(): void + { + $this->step->get(self::TEST_URL) + ->extractFromHtml('userId', 'div#user-id'); + $stepArray = $this->step->toArray(); + $extract = $stepArray['extract'][0]; + $this->assertEquals('userId', $extract['variable_name']); + $this->assertEquals('div#user-id', $extract['selector']); + $this->assertEquals('html', $extract['type']); + } + + + public function testExtractFromHtmlWithAttribute(): void + { + $this->step->get(self::TEST_URL) + ->extractFromHtml('userId', 'div#user-id', 'data-id'); + $stepArray = $this->step->toArray(); + $extract = $stepArray['extract'][0]; + $this->assertEquals('userId', $extract['variable_name']); + $this->assertEquals('div#user-id', $extract['selector']); + $this->assertEquals('html', $extract['type']); + } + + public function testExtractFromHtmlThrowsException(): void + { + $this->expectException(VoltTestException::class); + $this->step->get(self::TEST_URL) + ->extractFromHtml('userId', '#div#user-id', ''); + $this->step->toArray(); + } + public function testInvalidJsonPathThrowsException(): void { $this->expectException(InvalidJsonPathException::class); From b361aef64ff93a7371e265a6daebc09b3a3277d0 Mon Sep 17 00:00:00 2001 From: elwafa Date: Sat, 22 Feb 2025 13:18:48 +0200 Subject: [PATCH 3/3] CI --- src/Extractors/HtmlExtractor.php | 2 +- tests/Units/HtmlExtractorTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Extractors/HtmlExtractor.php b/src/Extractors/HtmlExtractor.php index 83ce044..a7cadb3 100644 --- a/src/Extractors/HtmlExtractor.php +++ b/src/Extractors/HtmlExtractor.php @@ -52,4 +52,4 @@ public function validate(): bool return true; } -} \ No newline at end of file +} diff --git a/tests/Units/HtmlExtractorTest.php b/tests/Units/HtmlExtractorTest.php index e6d0dfc..fb89704 100644 --- a/tests/Units/HtmlExtractorTest.php +++ b/tests/Units/HtmlExtractorTest.php @@ -60,4 +60,4 @@ public function testEmptyAttributeWithNullSelector(): void $extractor->toArray(); } -} \ No newline at end of file +}