functionalSuiteHooks.txt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Group;
  3. use Magento\FunctionalTestingFramework\DataGenerator\Handlers\PersistedObjectHandler;
  4. /**
  5. * Group class is Codeception Extension which is allowed to handle to all internal events.
  6. * This class itself can be used to listen events for test execution of one particular group.
  7. * It may be especially useful to create fixtures data, prepare server, etc.
  8. *
  9. * INSTALLATION:
  10. *
  11. * To use this group extension, include it to "extensions" option of global Codeception config.
  12. */
  13. class functionalSuiteHooks extends \Codeception\GroupObject
  14. {
  15. public static $group = 'functionalSuiteHooks';
  16. private $testCount = 1;
  17. private $preconditionFailure = null;
  18. private $currentTestRun = 0;
  19. private static $HOOK_EXECUTION_INIT = "\n/******** Beginning execution of functionalSuiteHooks suite %s block ********/\n";
  20. private static $HOOK_EXECUTION_END = "\n/******** Execution of functionalSuiteHooks suite %s block complete ********/\n";
  21. public function _before(\Codeception\Event\TestEvent $e)
  22. {
  23. // increment test count per execution
  24. $this->currentTestRun++;
  25. $this->executePreConditions();
  26. if ($this->preconditionFailure != null) {
  27. //if our preconditions fail, we need to mark all the tests as incomplete.
  28. $e->getTest()->getMetadata()->setIncomplete("SUITE PRECONDITION FAILED:" . PHP_EOL . $this->preconditionFailure);
  29. }
  30. }
  31. private function executePreConditions()
  32. {
  33. if ($this->currentTestRun == 1) {
  34. print sprintf(self::$HOOK_EXECUTION_INIT, "before");
  35. try {
  36. $webDriver = $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver');
  37. // close any open sessions
  38. if ($webDriver->webDriver != null) {
  39. $webDriver->webDriver->close();
  40. $webDriver->webDriver = null;
  41. }
  42. // initialize the webdriver session
  43. $webDriver->_initializeSession();
  44. $webDriver->amOnPage("some.url");
  45. $createFields['someKey'] = "dataHere";
  46. PersistedObjectHandler::getInstance()->createEntity(
  47. "create",
  48. "suite",
  49. "createThis",
  50. $createFields
  51. );
  52. $webDriver->click(PersistedObjectHandler::getInstance()->retrieveEntityField('create', 'data', 'suite'));
  53. $webDriver->see("John", msq("uniqueData") . "John");
  54. // reset configuration and close session
  55. $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver')->_resetConfig();
  56. $webDriver->webDriver->close();
  57. $webDriver->webDriver = null;
  58. } catch (\Exception $exception) {
  59. $this->preconditionFailure = $exception->getMessage();
  60. }
  61. print sprintf(self::$HOOK_EXECUTION_END, "before");
  62. }
  63. }
  64. public function _after(\Codeception\Event\TestEvent $e)
  65. {
  66. $this->executePostConditions($e);
  67. }
  68. private function executePostConditions(\Codeception\Event\TestEvent $e)
  69. {
  70. if ($this->currentTestRun == $this->testCount) {
  71. print sprintf(self::$HOOK_EXECUTION_INIT, "after");
  72. try {
  73. // Find out if Test in Suite failed, will cause potential failures in suite after
  74. $cest = $e->getTest();
  75. //Access private TestResultObject to find stack and if there are any errors (as opposed to failures)
  76. $testResultObject = call_user_func(\Closure::bind(
  77. function () use ($cest) {
  78. return $cest->getTestResultObject();
  79. },
  80. $cest
  81. ));
  82. $errors = $testResultObject->errors();
  83. if (!empty($errors)) {
  84. foreach ($errors as $error) {
  85. if ($error->failedTest()->getTestMethod() == $cest->getName()) {
  86. // Do not attempt to run _after if failure was in the _after block
  87. // Try to run _after but catch exceptions to prevent them from overwriting original failure.
  88. print("LAST TEST IN SUITE FAILED, TEST AFTER MAY NOT BE SUCCESSFUL\n");
  89. }
  90. }
  91. }
  92. $webDriver = $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver');
  93. // close any open sessions
  94. if ($webDriver->webDriver != null) {
  95. $webDriver->webDriver->close();
  96. $webDriver->webDriver = null;
  97. }
  98. // initialize the webdriver session
  99. $webDriver->_initializeSession();
  100. $webDriver->amOnPage("some.url");
  101. $webDriver->deleteEntityByUrl("deleteThis");
  102. $webDriver->see("John", msq("uniqueData") . "John");
  103. } catch (\Exception $exception) {
  104. print $exception->getMessage();
  105. }
  106. PersistedObjectHandler::getInstance()->clearSuiteObjects();
  107. print sprintf(self::$HOOK_EXECUTION_END, "after");
  108. }
  109. }
  110. }