diff --git a/dev/tests/_bootstrap.php b/dev/tests/_bootstrap.php index e06e373d4..b41f80394 100644 --- a/dev/tests/_bootstrap.php +++ b/dev/tests/_bootstrap.php @@ -17,7 +17,10 @@ $kernel = \AspectMock\Kernel::getInstance(); $kernel->init([ 'debug' => true, - 'includePaths' => [PROJECT_ROOT . DIRECTORY_SEPARATOR . 'src'], + 'includePaths' => [ + PROJECT_ROOT . DIRECTORY_SEPARATOR . 'src', + PROJECT_ROOT . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'allure-framework' + ], 'cacheDir' => PROJECT_ROOT . DIRECTORY_SEPARATOR . 'dev' . diff --git a/dev/tests/unit/Magento/FunctionalTestFramework/Allure/AllureHelperTest.php b/dev/tests/unit/Magento/FunctionalTestFramework/Allure/AllureHelperTest.php new file mode 100644 index 000000000..048c6f7de --- /dev/null +++ b/dev/tests/unit/Magento/FunctionalTestFramework/Allure/AllureHelperTest.php @@ -0,0 +1,97 @@ +mockAttachmentWriteEvent(); + $expectedData = "string"; + $expectedCaption = "caption"; + + //Prepare Allure lifecycle + Allure::lifecycle()->fire(new StepStartedEvent('firstStep')); + + //Call function + AllureHelper::addAttachmentToCurrentStep($expectedData, $expectedCaption); + + // Assert Attachment is created as expected + $step = Allure::lifecycle()->getStepStorage()->pollLast(); + $expectedAttachment = new Attachment($expectedCaption, self::MOCK_FILENAME, null); + $this->assertEquals($step->getAttachments()[0], $expectedAttachment); + } + + /** + * AddAttachmentToLastStep should add an attachment only to the last step + * @throws \Yandex\Allure\Adapter\AllureException + */ + public function testAddAttachmentToLastStep() + { + $this->mockAttachmentWriteEvent(); + $expectedData = "string"; + $expectedCaption = "caption"; + + //Prepare Allure lifecycle + Allure::lifecycle()->fire(new StepStartedEvent('firstStep')); + Allure::lifecycle()->fire(new StepFinishedEvent('firstStep')); + Allure::lifecycle()->fire(new StepStartedEvent('secondStep')); + Allure::lifecycle()->fire(new StepFinishedEvent('secondStep')); + + //Call function + AllureHelper::addAttachmentToLastStep($expectedData, $expectedCaption); + + //Continue Allure lifecycle + Allure::lifecycle()->fire(new StepStartedEvent('thirdStep')); + Allure::lifecycle()->fire(new StepFinishedEvent('thirdStep')); + + // Assert Attachment is created as expected on the right step + $rootStep = Allure::lifecycle()->getStepStorage()->pollLast(); + + $firstStep = $rootStep->getSteps()[0]; + $secondStep = $rootStep->getSteps()[1]; + $thirdStep = $rootStep->getSteps()[2]; + + $expectedAttachment = new Attachment($expectedCaption, self::MOCK_FILENAME, null); + $this->assertEmpty($firstStep->getAttachments()); + $this->assertEquals($secondStep->getAttachments()[0], $expectedAttachment); + $this->assertEmpty($thirdStep->getAttachments()); + } + + /** + * Mock file system manipulation function + * @throws \Exception + */ + public function mockAttachmentWriteEvent() + { + AspectMock::double(AddAttachmentEvent::class, [ + "getAttachmentFileName" => self::MOCK_FILENAME + ]); + } +} diff --git a/src/Magento/FunctionalTestingFramework/Allure/AllureHelper.php b/src/Magento/FunctionalTestingFramework/Allure/AllureHelper.php new file mode 100644 index 000000000..d8f1c63d8 --- /dev/null +++ b/src/Magento/FunctionalTestingFramework/Allure/AllureHelper.php @@ -0,0 +1,40 @@ +fire(new AddAttachmentEvent($data, $caption)); + } + + /** + * Adds Attachment to the last executed step. + * Use this when adding attachments outside of an $I->doSomething() step/context. + * @param mixed $data + * @param string $caption + * @return void + */ + public static function addAttachmentToLastStep($data, $caption) + { + $rootStep = Allure::lifecycle()->getStepStorage()->getLast(); + $trueLastStep = array_last($rootStep->getSteps()); + + $attachmentEvent = new AddAttachmentEvent($data, $caption); + $attachmentEvent->process($trueLastStep); + } +} diff --git a/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/CurlHandler.php b/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/CurlHandler.php index f6d8773bc..4b184c206 100644 --- a/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/CurlHandler.php +++ b/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/CurlHandler.php @@ -5,6 +5,7 @@ */ namespace Magento\FunctionalTestingFramework\DataGenerator\Persist; +use Magento\FunctionalTestingFramework\Allure\AllureHelper; use Magento\FunctionalTestingFramework\DataGenerator\Persist\Curl\AdminExecutor; use Magento\FunctionalTestingFramework\DataGenerator\Persist\Curl\FrontendExecutor; use Magento\FunctionalTestingFramework\DataGenerator\Persist\Curl\WebapiExecutor; @@ -166,6 +167,14 @@ public function executeRequest($dependentEntities) $response = $executor->read($successRegex, $returnRegex, $returnIndex); $executor->close(); + AllureHelper::addAttachmentToLastStep($apiUrl, 'API Endpoint'); + AllureHelper::addAttachmentToLastStep(json_encode($headers, JSON_PRETTY_PRINT), 'Request Headers'); + AllureHelper::addAttachmentToLastStep(json_encode($this->requestData, JSON_PRETTY_PRINT), 'Request Body'); + AllureHelper::addAttachmentToLastStep( + json_encode(json_decode($response, true), JSON_PRETTY_PRINT+JSON_UNESCAPED_UNICODE+JSON_UNESCAPED_SLASHES), + 'Response Data' + ); + return $response; } diff --git a/src/Magento/FunctionalTestingFramework/Module/MagentoWebDriver.php b/src/Magento/FunctionalTestingFramework/Module/MagentoWebDriver.php index d268e23b4..731643eaf 100644 --- a/src/Magento/FunctionalTestingFramework/Module/MagentoWebDriver.php +++ b/src/Magento/FunctionalTestingFramework/Module/MagentoWebDriver.php @@ -9,6 +9,7 @@ use Codeception\Module\WebDriver; use Codeception\Test\Descriptor; use Codeception\TestInterface; +use Magento\FunctionalTestingFramework\Allure\AllureHelper; use Facebook\WebDriver\Interactions\WebDriverActions; use Codeception\Exception\ModuleConfigException; use Codeception\Exception\ModuleException; @@ -198,7 +199,10 @@ public function _getCurrentUri() */ public function dontSeeCurrentUrlEquals($url) { - $this->assertNotEquals($url, $this->webDriver->getCurrentURL()); + $actualUrl = $this->webDriver->getCurrentURL(); + $comparison = "Expected: $url\nActual: $actualUrl"; + AllureHelper::addAttachmentToCurrentStep($comparison, 'Comparison'); + $this->assertNotEquals($url, $actualUrl); } /** @@ -209,7 +213,10 @@ public function dontSeeCurrentUrlEquals($url) */ public function dontSeeCurrentUrlMatches($regex) { - $this->assertNotRegExp($regex, $this->webDriver->getCurrentURL()); + $actualUrl = $this->webDriver->getCurrentURL(); + $comparison = "Expected: $regex\nActual: $actualUrl"; + AllureHelper::addAttachmentToCurrentStep($comparison, 'Comparison'); + $this->assertNotRegExp($regex, $actualUrl); } /** @@ -220,7 +227,10 @@ public function dontSeeCurrentUrlMatches($regex) */ public function dontSeeInCurrentUrl($needle) { - $this->assertNotContains($needle, $this->webDriver->getCurrentURL()); + $actualUrl = $this->webDriver->getCurrentURL(); + $comparison = "Expected: $needle\nActual: $actualUrl"; + AllureHelper::addAttachmentToCurrentStep($comparison, 'Comparison'); + $this->assertNotContains($needle, $actualUrl); } /** @@ -254,7 +264,10 @@ public function grabFromCurrentUrl($regex = null) */ public function seeCurrentUrlEquals($url) { - $this->assertEquals($url, $this->webDriver->getCurrentURL()); + $actualUrl = $this->webDriver->getCurrentURL(); + $comparison = "Expected: $url\nActual: $actualUrl"; + AllureHelper::addAttachmentToCurrentStep($comparison, 'Comparison'); + $this->assertEquals($url, $actualUrl); } /** @@ -265,7 +278,10 @@ public function seeCurrentUrlEquals($url) */ public function seeCurrentUrlMatches($regex) { - $this->assertRegExp($regex, $this->webDriver->getCurrentURL()); + $actualUrl = $this->webDriver->getCurrentURL(); + $comparison = "Expected: $regex\nActual: $actualUrl"; + AllureHelper::addAttachmentToCurrentStep($comparison, 'Comparison'); + $this->assertRegExp($regex, $actualUrl); } /** @@ -276,7 +292,10 @@ public function seeCurrentUrlMatches($regex) */ public function seeInCurrentUrl($needle) { - $this->assertContains($needle, $this->webDriver->getCurrentURL()); + $actualUrl = $this->webDriver->getCurrentURL(); + $comparison = "Expected: $needle\nActual: $actualUrl"; + AllureHelper::addAttachmentToCurrentStep($comparison, 'Comparison'); + $this->assertContains($needle, $actualUrl); } /**