Licitator 1.0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
6.9 KiB

5 years ago
  1. <?php
  2. namespace Prometeo\CommandsBundle\Commands;
  3. use Symfony\Component\Console\Command\Command as ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputArgument;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Input\InputOption;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. class Selenium2Phanter extends ContainerAwareCommand
  11. {
  12. private $em;
  13. private $options=[
  14. 'receiver'=>'client',
  15. 'extendedClass'=> 'PantherTestCase',
  16. ];
  17. public function __construct(ContainerInterface $container)
  18. {
  19. parent::__construct();
  20. $this->em = $container->get('doctrine')->getManager();
  21. }
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function configure(): void
  26. {
  27. $this->setName('prometeo:test:selenium2phanter');
  28. $this->addArgument(
  29. 'seleniumfile',
  30. InputArgument::REQUIRED,
  31. 'Selenium File in SIDE format'
  32. )
  33. ;
  34. $this->addArgument(
  35. 'testcaseroute',
  36. InputArgument::OPTIONAL,
  37. 'Route to test cases'
  38. );
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function execute(InputInterface $input, OutputInterface $output): void
  44. {
  45. $seleniumfile=$input->getArgument('seleniumfile');
  46. if ($input->getArgument('testcaseroute')) {
  47. $testcaseroute=$input->getArgument('testcaseroute');
  48. }else{
  49. $testcaseroute='tests/Selenium';
  50. }
  51. $selenium=file_get_contents($seleniumfile);
  52. $seleniumobj=json_decode($selenium,true);
  53. $namespace=explode('/',$testcaseroute);
  54. foreach($namespace as $key=>$space){
  55. $namespace[$key]=ucfirst($space);
  56. }
  57. $this->options['namespace']= 'App\\'.implode('\\',$namespace);
  58. if(!is_dir($testcaseroute.'/'.$seleniumobj['name'])){
  59. mkdir($testcaseroute.'/'.$seleniumobj['name']);
  60. }
  61. $depends='';
  62. foreach($seleniumobj['tests'] as $test) {
  63. $test['name']=str_replace(' ','',$test['name']);
  64. $test['name']=substr($test['name'],3);
  65. $header="<?php\n"
  66. ."namespace ".$this->options['namespace'].";\n"
  67. ."\n"
  68. ."use Symfony\\Component\\Panther\\PantherTestCase;\n"
  69. ."use Prometeo\\CommandsBundle\\TestTraits\\PantherTrait;\n"
  70. ."use Symfony\\Component\\DomCrawler\\Crawler;\n"
  71. ."\n"
  72. . "class ".$test['name']."Test extends ".$this->options['extendedClass']."\n"
  73. . "{\n"
  74. . "\t/**\n"
  75. . "\t * @var WebDriver\\Remote\\RemoteWebDriver\n"
  76. . "\t */\n"
  77. . "\tprivate $".$this->options['receiver'].";\n"
  78. . "\n"
  79. . "\t/**\n"
  80. . "\t * @var string\n"
  81. . "\t */\n"
  82. . "\t".'private $baseUrl;'."\n"
  83. . "\n"
  84. . "\t/**\n"
  85. . "\t * init webdriver\n"
  86. . "\t */\n"
  87. . "\tpublic function setUp() :void\n"
  88. . "\t{\n"
  89. . "\t\tself::bootKernel();\n"
  90. . "\t\t ".'$this->client'." = static::createPantherClient();\n"
  91. . "\t}\n"
  92. . "\n";
  93. $body = "\t/**\n"
  94. . "\t * Method " . strtolower($test['name']) . "Test\n"
  95. . "\t * @test\n";
  96. if(!empty($depends)){
  97. $body.= "\t * @Depends $depends\n";
  98. }
  99. $body.= "\t */\n"
  100. . "\tpublic function " . strtolower($test['name']) . "Test()\n"
  101. . "\t{\n";
  102. foreach ($test['commands'] as $command) {
  103. $body .= $this->processCommand($command);
  104. }
  105. $body .= "\t}\n";
  106. $body.= "\t}\n";
  107. $depends=$test['name'].'Test::'.strtolower($test['name']).'Test()';
  108. file_put_contents($testcaseroute.'/'.$seleniumobj['name'].'/'.$test['name'] ."Test.php",$header.$body);
  109. }
  110. echo "Conversion finalizada";
  111. }
  112. public function processCommand($command){
  113. switch($command['command']){
  114. case 'open':
  115. return "\t\t".'$crawler = $this->client->request(\'GET\',\''.$command['target'].'\');'."\n";
  116. case 'edit content':
  117. case 'type':
  118. return "\t\t".$this->getTypeCommand($command).'->sendKeys("'.$command['value'].'");'."\n";
  119. case 'click':
  120. if($command['value']=='submit' ){
  121. return "\t\t".$this->getTypeCommand($command)."\t\n";
  122. }
  123. return "\t\t".$this->getTypeCommand($command).'->click();'."\t\n";
  124. case 'assert element present':
  125. return "\t\t"."$this->assertTrue(!empty(".$this->getTypeCommand($command)."));"."\n";
  126. }
  127. }
  128. public function getTypeCommand($command){
  129. $commandarray=explode('=',$command['target']);
  130. $locatorType=$commandarray[0];
  131. unset($commandarray[0]);
  132. $locatorString=implode('=',$commandarray);
  133. switch ($locatorType){
  134. case 'xpath':
  135. return '$crawler->filter("' . $locatorString . '")';
  136. case 'css':
  137. if ($command['value']=='submit'){
  138. return '$form=$crawler->filter("' . $locatorString . '")->form();'."\n\t"
  139. .'$crawler = $this->client->submit($form);';
  140. }else{
  141. return '$crawler->filter("' . $locatorString . '")';
  142. }
  143. case 'id':
  144. return '$crawler->filter("input[id$=' . $locatorString . ']")';
  145. case 'linkText':
  146. if ($command['value']=='submit') {
  147. return '$link=$crawler->selectLink("' . $locatorString . '")->link();' . "\n\t"
  148. . '$crawler = $this->client->request(\'GET\',$link->getUri());';
  149. }else{
  150. return '$crawler->selectLink("' . $locatorString . '")';
  151. }
  152. case 'name':
  153. if ($command['value']=='submit') {
  154. return '$form=$crawler->filter("[name=\'' . $locatorString . '\']")->form();'."\n\t"
  155. .'$crawler = $this->client->submit($form);';
  156. }
  157. return '$crawler->filter("[name=\'' . $locatorString . '\']")';
  158. case 'tag_name':
  159. return '$crawler->filter("' . $locatorString . '")';
  160. case 'button':
  161. return '$form = $crawler->selectButton("' . $locatorString . '")->form();'
  162. ."\n\t".'$crawler = $this->client->submit($form);';
  163. case 'submit':
  164. return '$form=$crawler->filter("' . $locatorString . '")->form();'."\n\t"
  165. .'$crawler = $this->client->submit($form);';
  166. }
  167. }
  168. }