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.
 
 
 
 
 

180 lines
6.9 KiB

<?php
namespace Prometeo\CommandsBundle\Commands;
use Symfony\Component\Console\Command\Command as ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Selenium2Phanter extends ContainerAwareCommand
{
private $em;
private $options=[
'receiver'=>'client',
'extendedClass'=> 'PantherTestCase',
];
public function __construct(ContainerInterface $container)
{
parent::__construct();
$this->em = $container->get('doctrine')->getManager();
}
/**
* {@inheritdoc}
*/
public function configure(): void
{
$this->setName('prometeo:test:selenium2phanter');
$this->addArgument(
'seleniumfile',
InputArgument::REQUIRED,
'Selenium File in SIDE format'
)
;
$this->addArgument(
'testcaseroute',
InputArgument::OPTIONAL,
'Route to test cases'
);
}
/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output): void
{
$seleniumfile=$input->getArgument('seleniumfile');
if ($input->getArgument('testcaseroute')) {
$testcaseroute=$input->getArgument('testcaseroute');
}else{
$testcaseroute='tests/Selenium';
}
$selenium=file_get_contents($seleniumfile);
$seleniumobj=json_decode($selenium,true);
$namespace=explode('/',$testcaseroute);
foreach($namespace as $key=>$space){
$namespace[$key]=ucfirst($space);
}
$this->options['namespace']= 'App\\'.implode('\\',$namespace);
if(!is_dir($testcaseroute.'/'.$seleniumobj['name'])){
mkdir($testcaseroute.'/'.$seleniumobj['name']);
}
$depends='';
foreach($seleniumobj['tests'] as $test) {
$test['name']=str_replace(' ','',$test['name']);
$test['name']=substr($test['name'],3);
$header="<?php\n"
."namespace ".$this->options['namespace'].";\n"
."\n"
."use Symfony\\Component\\Panther\\PantherTestCase;\n"
."use Prometeo\\CommandsBundle\\TestTraits\\PantherTrait;\n"
."use Symfony\\Component\\DomCrawler\\Crawler;\n"
."\n"
. "class ".$test['name']."Test extends ".$this->options['extendedClass']."\n"
. "{\n"
. "\t/**\n"
. "\t * @var WebDriver\\Remote\\RemoteWebDriver\n"
. "\t */\n"
. "\tprivate $".$this->options['receiver'].";\n"
. "\n"
. "\t/**\n"
. "\t * @var string\n"
. "\t */\n"
. "\t".'private $baseUrl;'."\n"
. "\n"
. "\t/**\n"
. "\t * init webdriver\n"
. "\t */\n"
. "\tpublic function setUp() :void\n"
. "\t{\n"
. "\t\tself::bootKernel();\n"
. "\t\t ".'$this->client'." = static::createPantherClient();\n"
. "\t}\n"
. "\n";
$body = "\t/**\n"
. "\t * Method " . strtolower($test['name']) . "Test\n"
. "\t * @test\n";
if(!empty($depends)){
$body.= "\t * @Depends $depends\n";
}
$body.= "\t */\n"
. "\tpublic function " . strtolower($test['name']) . "Test()\n"
. "\t{\n";
foreach ($test['commands'] as $command) {
$body .= $this->processCommand($command);
}
$body .= "\t}\n";
$body.= "\t}\n";
$depends=$test['name'].'Test::'.strtolower($test['name']).'Test()';
file_put_contents($testcaseroute.'/'.$seleniumobj['name'].'/'.$test['name'] ."Test.php",$header.$body);
}
echo "Conversion finalizada";
}
public function processCommand($command){
switch($command['command']){
case 'open':
return "\t\t".'$crawler = $this->client->request(\'GET\',\''.$command['target'].'\');'."\n";
case 'edit content':
case 'type':
return "\t\t".$this->getTypeCommand($command).'->sendKeys("'.$command['value'].'");'."\n";
case 'click':
if($command['value']=='submit' ){
return "\t\t".$this->getTypeCommand($command)."\t\n";
}
return "\t\t".$this->getTypeCommand($command).'->click();'."\t\n";
case 'assert element present':
return "\t\t"."$this->assertTrue(!empty(".$this->getTypeCommand($command)."));"."\n";
}
}
public function getTypeCommand($command){
$commandarray=explode('=',$command['target']);
$locatorType=$commandarray[0];
unset($commandarray[0]);
$locatorString=implode('=',$commandarray);
switch ($locatorType){
case 'xpath':
return '$crawler->filter("' . $locatorString . '")';
case 'css':
if ($command['value']=='submit'){
return '$form=$crawler->filter("' . $locatorString . '")->form();'."\n\t"
.'$crawler = $this->client->submit($form);';
}else{
return '$crawler->filter("' . $locatorString . '")';
}
case 'id':
return '$crawler->filter("input[id$=' . $locatorString . ']")';
case 'linkText':
if ($command['value']=='submit') {
return '$link=$crawler->selectLink("' . $locatorString . '")->link();' . "\n\t"
. '$crawler = $this->client->request(\'GET\',$link->getUri());';
}else{
return '$crawler->selectLink("' . $locatorString . '")';
}
case 'name':
if ($command['value']=='submit') {
return '$form=$crawler->filter("[name=\'' . $locatorString . '\']")->form();'."\n\t"
.'$crawler = $this->client->submit($form);';
}
return '$crawler->filter("[name=\'' . $locatorString . '\']")';
case 'tag_name':
return '$crawler->filter("' . $locatorString . '")';
case 'button':
return '$form = $crawler->selectButton("' . $locatorString . '")->form();'
."\n\t".'$crawler = $this->client->submit($form);';
case 'submit':
return '$form=$crawler->filter("' . $locatorString . '")->form();'."\n\t"
.'$crawler = $this->client->submit($form);';
}
}
}