Creates a PHP string for the actions contained withing a <test> block. Since nearly half of all Codeception methods don't share the same signature I had to setup a massive Case statement to handle each unique action. At the bottom of the case statement there is a generic function that can construct the PHP string for nearly half of all Codeception actions.
485 $this->currentGenerationScope = $generationScope;
487 foreach ($actionObjects as $actionObject) {
488 $stepKey = $actionObject->getStepKey();
489 $customActionAttributes = $actionObject->getCustomActionAttributes();
495 $parameterArray =
null;
496 $returnVariable =
null;
508 $requiredAction =
null;
512 $dependentSelector =
null;
520 $assertExpected =
null;
521 $assertActual =
null;
522 $assertMessage =
null;
523 $assertIsStrict =
null;
527 $this->validateXmlAttributesMutuallyExclusive($stepKey, $actionObject->getType(), $customActionAttributes);
529 if (isset($customActionAttributes[
'command'])) {
530 $command = $this->addUniquenessFunctionCall($customActionAttributes[
'command']);
532 if (isset($customActionAttributes[
'arguments'])) {
533 $arguments = $this->addUniquenessFunctionCall($customActionAttributes[
'arguments']);
536 if (isset($customActionAttributes[
'attribute'])) {
537 $attribute = $customActionAttributes[
'attribute'];
540 if (isset($customActionAttributes[
'sortOrder'])) {
541 $sortOrder = $customActionAttributes[
'sortOrder'];
544 if (isset($customActionAttributes[
'userInput']) && isset($customActionAttributes[
'url'])) {
545 $input = $this->addUniquenessFunctionCall($customActionAttributes[
'userInput']);
546 $url = $this->addUniquenessFunctionCall($customActionAttributes[
'url']);
547 }
elseif (isset($customActionAttributes[
'userInput'])) {
548 $input = $this->addUniquenessFunctionCall($customActionAttributes[
'userInput']);
549 }
elseif (isset($customActionAttributes[
'url'])) {
550 $input = $this->addUniquenessFunctionCall($customActionAttributes[
'url']);
551 $url = $this->addUniquenessFunctionCall($customActionAttributes[
'url']);
552 }
elseif (isset($customActionAttributes[
'expectedValue'])) {
554 $assertExpected = $this->addUniquenessFunctionCall($customActionAttributes[
'expectedValue']);
555 }
elseif (isset($customActionAttributes[
'regex'])) {
556 $input = $this->addUniquenessFunctionCall($customActionAttributes[
'regex']);
559 if (isset($customActionAttributes[
'date']) && isset($customActionAttributes[
'format'])) {
560 $input = $this->addUniquenessFunctionCall($customActionAttributes[
'date']);
564 $format = $this->addUniquenessFunctionCall($customActionAttributes[
'format']);
570 if (isset($customActionAttributes[
'expected'])) {
571 $assertExpected = $this->resolveValueByType(
572 $customActionAttributes[
'expected'],
573 isset($customActionAttributes[
'expectedType']) ? $customActionAttributes[
'expectedType'] :
null 576 if (isset($customActionAttributes[
'actual'])) {
577 $assertActual = $this->resolveValueByType(
578 $customActionAttributes[
'actual'],
579 isset($customActionAttributes[
'actualType']) ? $customActionAttributes[
'actualType'] :
null 582 if (isset($customActionAttributes[
'message'])) {
583 $assertMessage = $this->addUniquenessFunctionCall($customActionAttributes[
'message']);
585 if (isset($customActionAttributes[
'delta'])) {
586 $assertDelta = $this->resolveValueByType($customActionAttributes[
'delta'],
"float");
588 if (isset($customActionAttributes[
'strict'])) {
589 $assertIsStrict = $this->resolveValueByType($customActionAttributes[
'strict'],
"bool");
592 if (isset($customActionAttributes[
'time'])) {
593 $time = $customActionAttributes[
'time'];
595 if (isset($customActionAttributes[
'timeout'])) {
596 $time = $customActionAttributes[
'timeout'];
599 if (isset($customActionAttributes[
'parameterArray']) && $actionObject->getType() !=
'pressKey') {
601 $this->validateParameterArray($customActionAttributes[
'parameterArray']);
603 $parameterArray =
"[";
604 $parameterArray .= $this->addUniquenessToParamArray($customActionAttributes[
'parameterArray']);
605 $parameterArray .=
"]";
608 if (isset($customActionAttributes[
'requiredAction'])) {
609 $requiredAction = $customActionAttributes[
'requiredAction'];
612 if (isset($customActionAttributes[
'selectorArray'])) {
613 $selector = $customActionAttributes[
'selectorArray'];
614 }
elseif (isset($customActionAttributes[
'selector'])) {
615 $selector = $this->addUniquenessFunctionCall($customActionAttributes[
'selector']);
616 $selector = $this->resolveLocatorFunctionInAttribute($selector);
619 if (isset($customActionAttributes[
'selector1']) || isset($customActionAttributes[
'filterSelector'])) {
620 $selectorOneValue = $customActionAttributes[
'selector1'] ?? $customActionAttributes[
'filterSelector'];
621 $selector1 = $this->addUniquenessFunctionCall($selectorOneValue);
622 $selector1 = $this->resolveLocatorFunctionInAttribute($selector1);
625 if (isset($customActionAttributes[
'selector2']) || isset($customActionAttributes[
'optionSelector'])) {
626 $selectorTwoValue = $customActionAttributes[
'selector2'] ?? $customActionAttributes[
'optionSelector'];
627 $selector2 = $this->addUniquenessFunctionCall($selectorTwoValue);
628 $selector2 = $this->resolveLocatorFunctionInAttribute($selector2);
631 if (isset($customActionAttributes[
'x'])) {
632 $x = $customActionAttributes[
'x'];
635 if (isset($customActionAttributes[
'y'])) {
636 $y = $customActionAttributes[
'y'];
639 if (isset($customActionAttributes[
'function'])) {
640 $function = $this->addUniquenessFunctionCall($customActionAttributes[
'function']);
641 if (in_array($actionObject->getType(), ActionObject::FUNCTION_CLOSURE_ACTIONS)) {
643 $function = trim($function,
'"');
646 if ($actionObject->getType() ==
"executeJS") {
647 $function = preg_replace(
'/(?<!{)(\$[A-Za-z._]+)(?![A-z.]*+\$)/',
'\\\\$1', $function);
651 if (isset($customActionAttributes[
'html'])) {
652 $html = $customActionAttributes[
'html'];
655 if (isset($customActionAttributes[
'locale'])) {
656 $locale = $this->wrapWithDoubleQuotes($customActionAttributes[
'locale']);
659 if (isset($customActionAttributes[
'username'])) {
660 $username = $this->wrapWithDoubleQuotes($customActionAttributes[
'username']);
663 if (isset($customActionAttributes[
'password'])) {
664 $password = $this->wrapWithDoubleQuotes($customActionAttributes[
'password']);
667 if (isset($customActionAttributes[
'width'])) {
668 $width = $customActionAttributes[
'width'];
671 if (isset($customActionAttributes[
'height'])) {
672 $height = $customActionAttributes[
'height'];
675 if (isset($customActionAttributes[
'value'])) {
676 $value = $this->wrapWithDoubleQuotes($customActionAttributes[
'value']);
679 if (isset($customActionAttributes[
'button'])) {
680 $button = $this->wrapWithDoubleQuotes($customActionAttributes[
'button']);
683 if (isset($customActionAttributes[
'parameter'])) {
684 $parameter = $this->wrapWithDoubleQuotes($customActionAttributes[
'parameter']);
687 if (isset($customActionAttributes[
'dependentSelector'])) {
688 $dependentSelector = $this->addUniquenessFunctionCall($customActionAttributes[
'dependentSelector']);
691 if (isset($customActionAttributes[
'visible'])) {
692 $visible = $customActionAttributes[
'visible'];
695 if (isset($customActionAttributes[
'storeCode'])) {
696 $storeCode = $customActionAttributes[
'storeCode'];
698 switch ($actionObject->getType()) {
700 $entity = $customActionAttributes[
'entity'];
702 $testSteps .= sprintf(
703 "\t\t$%s->amGoingTo(\"create entity that has the stepKey: %s\");\n",
709 $customEntityFields =
710 $customActionAttributes[ActionObjectExtractor::ACTION_OBJECT_PERSISTENCE_FIELDS] ?? [];
712 $requiredEntityKeys = [];
713 foreach ($actionObject->getCustomActionAttributes() as $actionAttribute) {
714 if (is_array($actionAttribute) && $actionAttribute[
'nodeName'] ==
'requiredEntity') {
716 $requiredEntityActionGroup = $actionAttribute[
'actionGroup'] ??
null;
717 $requiredEntityKeys[] = $actionAttribute[
'createDataKey'] . $requiredEntityActionGroup;
721 $requiredEntityKeysArray =
"";
722 if (!empty($requiredEntityKeys)) {
723 $requiredEntityKeysArray =
'"' . implode(
'", "', $requiredEntityKeys) .
'"';
726 $scope = PersistedObjectHandler::TEST_SCOPE;
727 if ($generationScope == TestGenerator::HOOK_SCOPE) {
728 $scope = PersistedObjectHandler::HOOK_SCOPE;
729 }
elseif ($generationScope == TestGenerator::SUITE_SCOPE) {
730 $scope = PersistedObjectHandler::SUITE_SCOPE;
733 $createEntityFunctionCall =
"\t\tPersistedObjectHandler::getInstance()->createEntity(";
734 $createEntityFunctionCall .=
"\n\t\t\t\"{$stepKey}\",";
735 $createEntityFunctionCall .=
"\n\t\t\t\"{$scope}\",";
736 $createEntityFunctionCall .=
"\n\t\t\t\"{$entity}\"";
737 $createEntityFunctionCall .=
",\n\t\t\t[{$requiredEntityKeysArray}]";
738 if (count($customEntityFields) > 1) {
739 $createEntityFunctionCall .=
",\n\t\t\t\${$stepKey}Fields";
741 $createEntityFunctionCall .=
",\n\t\t\tnull";
744 $createEntityFunctionCall .=
",\n\t\t\t\"{$storeCode}\"";
746 $createEntityFunctionCall .=
"\n\t\t);\n";
747 $testSteps .= $createEntityFunctionCall;
750 if (isset($customActionAttributes[
'createDataKey'])) {
751 $key = $this->resolveStepKeyReferences(
752 $customActionAttributes[
'createDataKey'],
753 $actionObject->getActionOrigin(),
756 $actionGroup = $actionObject->getCustomActionAttributes()[
'actionGroup'] ??
null;
757 $key .= $actionGroup;
759 $contextSetter = sprintf(
760 "\t\t$%s->amGoingTo(\"delete entity that has the createDataKey: %s\");\n",
766 $scope = PersistedObjectHandler::TEST_SCOPE;
767 if ($generationScope == TestGenerator::HOOK_SCOPE) {
768 $scope = PersistedObjectHandler::HOOK_SCOPE;
769 }
elseif ($generationScope == TestGenerator::SUITE_SCOPE) {
770 $scope = PersistedObjectHandler::SUITE_SCOPE;
773 $deleteEntityFunctionCall =
"\t\tPersistedObjectHandler::getInstance()->deleteEntity(";
774 $deleteEntityFunctionCall .=
"\n\t\t\t\"{$key}\",";
775 $deleteEntityFunctionCall .=
"\n\t\t\t\"{$scope}\"";
776 $deleteEntityFunctionCall .=
"\n\t\t);\n";
778 $testSteps .= $contextSetter;
779 $testSteps .= $deleteEntityFunctionCall;
781 $url = $this->resolveAllRuntimeReferences([
$url])[0];
782 $url = $this->resolveTestVariable([
$url],
null)[0];
784 "\t\t$%s->deleteEntityByUrl(%s);\n",
792 $key = $this->resolveStepKeyReferences(
793 $customActionAttributes[
'createDataKey'],
794 $actionObject->getActionOrigin(),
797 $updateEntity = $customActionAttributes[
'entity'];
798 $actionGroup = $actionObject->getCustomActionAttributes()[
'actionGroup'] ??
null;
799 $key .= $actionGroup;
802 $testSteps .= sprintf(
803 "\t\t$%s->amGoingTo(\"update entity that has the createdDataKey: %s\");\n",
809 $requiredEntityKeys = [];
810 foreach ($actionObject->getCustomActionAttributes() as $actionAttribute) {
811 if (is_array($actionAttribute) && $actionAttribute[
'nodeName'] ==
'requiredEntity') {
813 $requiredEntityActionGroup = $actionAttribute[
'actionGroup'] ??
null;
814 $requiredEntityKeys[] = $actionAttribute[
'createDataKey'] . $requiredEntityActionGroup;
817 $requiredEntityKeysArray =
"";
818 if (!empty($requiredEntityKeys)) {
819 $requiredEntityKeysArray =
'"' . implode(
'", "', $requiredEntityKeys) .
'"';
822 $scope = PersistedObjectHandler::TEST_SCOPE;
823 if ($generationScope == TestGenerator::HOOK_SCOPE) {
824 $scope = PersistedObjectHandler::HOOK_SCOPE;
825 }
elseif ($generationScope == TestGenerator::SUITE_SCOPE) {
826 $scope = PersistedObjectHandler::SUITE_SCOPE;
829 $updateEntityFunctionCall =
"\t\tPersistedObjectHandler::getInstance()->updateEntity(";
830 $updateEntityFunctionCall .=
"\n\t\t\t\"{$key}\",";
831 $updateEntityFunctionCall .=
"\n\t\t\t\"{$scope}\",";
832 $updateEntityFunctionCall .=
"\n\t\t\t\"{$updateEntity}\"";
833 $updateEntityFunctionCall .=
",\n\t\t\t[{$requiredEntityKeysArray}]";
835 $updateEntityFunctionCall .=
",\n\t\t\t\"{$storeCode}\"";
837 $updateEntityFunctionCall .=
"\n\t\t);\n";
838 $testSteps .= $updateEntityFunctionCall;
842 $entity = $customActionAttributes[
'entity'];
844 if (isset($customActionAttributes[
'index'])) {
845 $index = (int)$customActionAttributes[
'index'];
848 $testSteps .= sprintf(
849 "\t\t$%s->amGoingTo(\"get entity that has the stepKey: %s\");\n",
855 $requiredEntityKeys = [];
856 foreach ($actionObject->getCustomActionAttributes() as $actionAttribute) {
857 if (is_array($actionAttribute) && $actionAttribute[
'nodeName'] ==
'requiredEntity') {
858 $requiredEntityActionGroup = $actionAttribute[
'actionGroup'] ??
null;
859 $requiredEntityKeys[] = $actionAttribute[
'createDataKey'] . $requiredEntityActionGroup;
862 $requiredEntityKeysArray =
"";
863 if (!empty($requiredEntityKeys)) {
864 $requiredEntityKeysArray =
'"' . implode(
'", "', $requiredEntityKeys) .
'"';
868 $scope = PersistedObjectHandler::TEST_SCOPE;
869 if ($generationScope == TestGenerator::HOOK_SCOPE) {
870 $scope = PersistedObjectHandler::HOOK_SCOPE;
871 }
elseif ($generationScope == TestGenerator::SUITE_SCOPE) {
872 $scope = PersistedObjectHandler::SUITE_SCOPE;
876 $getEntityFunctionCall =
"\t\tPersistedObjectHandler::getInstance()->getEntity(";
877 $getEntityFunctionCall .=
"\n\t\t\t\"{$stepKey}\",";
878 $getEntityFunctionCall .=
"\n\t\t\t\"{$scope}\",";
879 $getEntityFunctionCall .=
"\n\t\t\t\"{$entity}\"";
880 $getEntityFunctionCall .=
",\n\t\t\t[{$requiredEntityKeysArray}]";
882 $getEntityFunctionCall .=
",\n\t\t\t\"{$storeCode}\"";
884 $getEntityFunctionCall .=
",\n\t\t\tnull";
887 $getEntityFunctionCall .=
",\n\t\t\t{$index}";
889 $getEntityFunctionCall .=
"\n\t\t);\n";
890 $testSteps .= $getEntityFunctionCall;
893 case "assertArrayIsSorted":
894 $testSteps .= $this->wrapFunctionCall(
898 $this->wrapWithDoubleQuotes($sortOrder)
901 case "seeCurrentUrlEquals":
902 case "seeCurrentUrlMatches":
903 case "dontSeeCurrentUrlEquals":
904 case "dontSeeCurrentUrlMatches":
906 case "saveSessionSnapshot":
908 case "seeInCurrentUrl":
909 case "switchToIFrame":
910 case "switchToWindow":
914 $testSteps .= $this->wrapFunctionCall($actor, $actionObject, $input, $selector);
916 case "switchToNextTab":
917 case "switchToPreviousTab":
918 $testSteps .= $this->wrapFunctionCall($actor, $actionObject, $input);
920 case "clickWithLeftButton":
921 case "clickWithRightButton":
922 case "moveMouseOver":
927 $testSteps .= $this->wrapFunctionCall($actor, $actionObject, $selector, $x, $y);
929 case "dontSeeCookie":
932 $testSteps .= $this->wrapFunctionCall(
940 $testSteps .= $this->wrapFunctionCallWithReturnValue(
948 case "dontSeeElement":
949 case "dontSeeElementInDOM":
950 case "dontSeeInFormFields":
952 case "seeElementInDOM":
953 case "seeInFormFields":
954 $testSteps .= $this->wrapFunctionCall(
962 $parameterArray = $customActionAttributes[
'parameterArray'] ??
null;
963 if ($parameterArray) {
964 $parameterArray = $this->processPressKey($parameterArray);
966 $testSteps .= $this->wrapFunctionCall(
975 case "unselectOption":
976 $testSteps .= $this->wrapFunctionCall(
985 $testSteps .= $this->wrapFunctionCall(
994 $testSteps .= $this->wrapFunctionCall(
1003 case "selectMultipleOptions":
1004 $testSteps .= $this->wrapFunctionCall(
1013 case "executeInSelenium":
1014 $testSteps .= $this->wrapFunctionCall($actor, $actionObject, $function);
1017 $testSteps .= $this->wrapFunctionCallWithReturnValue(
1025 case "waitForElementChange":
1026 $testSteps .= $this->wrapFunctionCall(
1035 $testSteps .= $this->wrapFunctionCall(
1043 case "waitForAjaxLoad":
1044 case "waitForElement":
1045 case "waitForElementVisible":
1046 case "waitForElementNotVisible":
1047 $testSteps .= $this->wrapFunctionCall($actor, $actionObject, $selector, $time);
1049 case "waitForPageLoad":
1051 $testSteps .= $this->wrapFunctionCall(
1060 $testSteps .= $this->wrapFunctionCallWithReturnValue(
1069 $testSteps .= $this->wrapFunctionCall($actor, $actionObject, $input, $locale);
1071 case "grabAttributeFrom":
1072 case "grabMultiple":
1073 case "grabFromCurrentUrl":
1074 $testSteps .= $this->wrapFunctionCallWithReturnValue(
1082 case "grabTextFrom":
1083 case "grabValueFrom":
1084 $testSteps .= $this->wrapFunctionCallWithReturnValue(
1091 case "grabPageSource":
1092 $testSteps .= $this->wrapFunctionCallWithReturnValue(
1098 case "resizeWindow":
1099 $testSteps .= $this->wrapFunctionCall($actor, $actionObject, $width, $height);
1101 case "searchAndMultiSelectOption":
1102 $testSteps .= $this->wrapFunctionCall(
1113 $testSteps .= $this->wrapFunctionCall($actor, $actionObject, $input,
$url);
1116 $testSteps .= $this->wrapFunctionCall(
1126 case "amOnSubdomain":
1131 case "dontSeeInField":
1132 case "dontSeeInCurrentUrl":
1133 case "dontSeeInTitle":
1134 case "dontSeeInPageSource":
1135 case "dontSeeOptionIsSelected":
1137 case "loadSessionSnapshot":
1139 case "seeOptionIsSelected":
1140 $testSteps .= $this->wrapFunctionCall($actor, $actionObject, $selector, $input);
1142 case "seeNumberOfElements":
1143 $testSteps .= $this->wrapFunctionCall(
1151 case "seeInPageSource":
1153 case "dontSeeInSource":
1155 $testSteps .= $this->wrapFunctionCall($actor, $actionObject, $html);
1157 case "conditionalClick":
1158 $testSteps .= $this->wrapFunctionCall(
1166 case "assertEquals":
1167 case "assertGreaterOrEquals":
1168 case "assertGreaterThan":
1169 case "assertGreaterThanOrEqual":
1170 case "assertInternalType":
1171 case "assertLessOrEquals":
1172 case "assertLessThan":
1173 case "assertLessThanOrEqual":
1174 case "assertNotEquals":
1175 case "assertInstanceOf":
1176 case "assertNotInstanceOf":
1177 case "assertNotRegExp":
1178 case "assertNotSame":
1179 case "assertRegExp":
1181 case "assertStringStartsNotWith":
1182 case "assertStringStartsWith":
1183 case "assertArrayHasKey":
1184 case "assertArrayNotHasKey":
1186 case "assertContains":
1187 case "assertNotContains":
1188 case "expectException":
1189 $testSteps .= $this->wrapFunctionCall(
1198 case "assertElementContainsAttribute":
1200 if (empty($assertExpected)) {
1201 $assertExpected =
'""';
1204 $testSteps .= $this->wrapFunctionCall(
1214 case "assertFileExists":
1215 case "assertFileNotExists":
1216 case "assertIsEmpty":
1217 case "assertNotEmpty":
1218 case "assertNotNull":
1221 $testSteps .= $this->wrapFunctionCall(
1228 case "assertArraySubset":
1229 $testSteps .= $this->wrapFunctionCall(
1239 $testSteps .= $this->wrapFunctionCall(
1246 $testSteps .= $this->wrapFunctionCallWithReturnValue(
1253 $testSteps .= sprintf(
1254 "\t\t$%s->comment(\$%s);\n",
1260 $fieldKey = $actionObject->getCustomActionAttributes()[
'key'];
1261 $input = $this->resolveTestVariable(
1263 $actionObject->getActionOrigin()
1266 $argRef .= str_replace(ucfirst($fieldKey),
"", $stepKey) .
"Fields['{$fieldKey}'] = ${input};\n";
1267 $testSteps .= $argRef;
1269 case "generateDate":
1270 $timezone = getenv(
"DEFAULT_TIMEZONE");
1271 if (isset($customActionAttributes[
'timezone'])) {
1272 $timezone = $customActionAttributes[
'timezone'];
1275 $dateGenerateCode =
"\t\t\$date = new \DateTime();\n";
1276 $dateGenerateCode .=
"\t\t\$date->setTimestamp(strtotime({$input}));\n";
1277 $dateGenerateCode .=
"\t\t\$date->setTimezone(new \DateTimeZone(\"{$timezone}\"));\n";
1278 $dateGenerateCode .=
"\t\t\${$stepKey} = \$date->format({$format});\n";
1280 $testSteps .= $dateGenerateCode;
1282 case "skipReadinessCheck":
1283 $testSteps .= $this->wrapFunctionCall($actor, $actionObject, $customActionAttributes[
'state']);
1286 $testSteps .= $this->wrapFunctionCall(
elseif(isset( $params[ 'redirect_parent']))