Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MQE-1633 + MQE-1514 #436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 4, 2019
5 changes: 4 additions & 1 deletion dev/tests/_bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' .
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Tests\unit\Magento\FunctionalTestingFramework\Allure;

use Magento\FunctionalTestingFramework\Allure\AllureHelper;
use Yandex\Allure\Adapter\Allure;
use Yandex\Allure\Adapter\Event\AddAttachmentEvent;
use Yandex\Allure\Adapter\Event\StepFinishedEvent;
use Yandex\Allure\Adapter\Event\StepStartedEvent;
use Yandex\Allure\Adapter\Model\Attachment;
use AspectMock\Test as AspectMock;
use PHPUnit\Framework\TestCase;

class AllureHelperTest extends TestCase
{
const MOCK_FILENAME = 'filename';

/**
* Clear Allure Lifecycle
*/
public function tearDown()
{
Allure::setDefaultLifecycle();
}

/**
* AddAtachmentToStep should add an attachment to the current step
* @throws \Yandex\Allure\Adapter\AllureException
*/
public function testAddAttachmentToStep()
{
$this->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
]);
}
}
40 changes: 40 additions & 0 deletions src/Magento/FunctionalTestingFramework/Allure/AllureHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Allure;

use Yandex\Allure\Adapter\Allure;
use Yandex\Allure\Adapter\Event\AddAttachmentEvent;

class AllureHelper
{
/**
* Adds attachment to the current step
* @param mixed $data
* @param string $caption
* @throws \Yandex\Allure\Adapter\AllureException
* @return void
*/
public static function addAttachmentToCurrentStep($data, $caption)
{
Allure::lifecycle()->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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious about in general when to use this over the current step? I see you use this one in the curl handler, but I cant figure out why. Maybe add a bit to the phpdoc like:

This function should be used when ___blahblahblah___

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add a comment to clear it up, the gist of it is that during a step (aka a function called in the test via $I->doathing()) you use current. If the framework is doign its own logic outisde a function like that, you can add artifact to the last step (which is usually a comment step)

{
$rootStep = Allure::lifecycle()->getStepStorage()->getLast();
$trueLastStep = array_last($rootStep->getSteps());

$attachmentEvent = new AddAttachmentEvent($data, $caption);
$attachmentEvent->process($trueLastStep);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
31 changes: 25 additions & 6 deletions src/Magento/FunctionalTestingFramework/Module/MagentoWebDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down