<?php
/**
* Created by PhpStorm.
* User: albert
* Date: 27/11/18
* Time: 18:31
*/
namespace AWCmsBundle\Behat\Context;
use Behat\Behat\Hook\Scope\AfterStepScope;
use Behat\MinkExtension\Context\MinkAwareContext;
use Behat\Symfony2Extension\Context\KernelDictionary;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use PHPUnit\Framework\Assert;
class CMSFeatureContext extends RawFeatureContext implements MinkAwareContext
{
use KernelDictionary;
private $temporal_files = [];
/**
* @Given I m logged as an super admin
*/
public function iMLoggedAsAnSuperAdmin()
{
$this->visitPath('/fr/admin/login');
$this->getPage()->findField('Identifiant')->setValue('super-admin@test.com');
$this->getPage()->findField('Mot de passe')->setValue('123456');
$this->getPage()->pressButton('Connexion');
}
/**
* @Then the :arg1 row should contains :arg2
*/
public function theRowShouldContains($rowText, $rowContains)
{
$row = $this->getPage()->find('css', sprintf('table.table tr:contains("%s")', $rowText));
Assert::assertNotNull($row, 'Could not find a row with text '.$rowText);
$td = $row->find('css', sprintf('td:contains("%s")', $rowContains));
Assert::assertNotNull($td, 'Could not find a row with text '.$rowContains);
}
/**
* @When I follow :linkText on the :rowText row
*/
public function iFollowOnTheRow($linkText, $rowText)
{
$selector = sprintf('table.table tr[data-user="%s"]', $rowText);
$row = $this->getPage()->find('css', $selector);
Assert::assertNotNull($row, 'Could not find a row with text '.$rowText);
Assert::assertContains($linkText, $row->getHtml());
$row->clickLink($linkText);
}
/**
* @Given I m logged as an admin
*/
public function iMLoggedAsAnAdmin()
{
$this->visitPath('/fr/admin/login');
$this->getPage()->findField('Identifiant')->setValue('admin@test.com');
$this->getPage()->findField('Mot de passe')->setValue('123456');
$this->getPage()->pressButton('Connexion');
}
/**
* @Then I should see :arg1 in output
*/
public function iShouldSeeInOutput($arg1)
{
Assert::assertContains($arg1, $this->getPage()->getContent());
}
/**
* @param $input
* @Then I should see :input unchecked
*/
public function iShouldSeeInputUnchecked($input)
{
Assert::assertFalse($this->getPage()->findField($input)->isChecked());
}
/**
* @param $input
* @Then I should see :input checked
*/
public function iShouldSeeInputChecked($input)
{
Assert::assertTrue($this->getPage()->findField($input)->isChecked());
}
/**
* @When I click on label :arg1
*/
public function iClickOnLabel($arg1)
{
$element = $this->getPage()->find('css', 'label[data-label="'.$arg1.'"]');
if (empty($element)) {
throw new \Exception("No html element found for the selector ('$arg1')");
}
$element->click();
$this->saveScreenshot('screenshot.jpg', '/var/www/web/');
}
/**
* @Given /^print last logs$/
*/
public function printLastLogs()
{
$file = $this->getKernel()->getLogDir().'/test.log';
if(file_exists($file)){
echo file_get_contents($file);
}
}
/**
* @Given /^print last apache logs$/
*/
public function printLastApacheLogs()
{
$file = '/var/logs/apache2/app_error.log';
if(file_exists($file)){
echo file_get_contents($file);
}
}
/**
* @param $seconds
* @param $text
* @When I wait :arg1 seconds until I see :arg2
*/
public function iWaitSecondsUntilISee($seconds, $text)
{
$this->getSession()->wait($seconds*1000, 'document.evaluate(\'contains(., "'.$text.'")\', document, null, XPathResult.ANY_TYPE, null ).booleanValue === true');
}
/**
* @param $seconds
* @param $text
* @When I wait :arg1 seconds until I don't see :arg2
*/
public function iWaitSecondsUntilIDontSee($seconds, $text)
{
$this->getSession()->wait($seconds*1000, 'document.evaluate(\'contains(., "'.$text.'")\', document, null, XPathResult.ANY_TYPE, null ).booleanValue === false');
}
/**
* @AfterStep
* @param AfterStepScope $event
*/
public function errorHandle(AfterStepScope $event){
if(!$event->getTestResult()->isPassed()){
if( $this->getPage()->find('css', '.exception-message')){
echo "\n".$this->getPage()->find('css', '.exception-message')->getText();
}
if( $this->getPage()->find('css', '.trace-file-path')){
echo "\n".$this->getPage()->find('css', '.trace-file-path')->getText();
}
}
}
/**
* @When /^I check the "([^"]*)" radio button$/
*/
public function iCheckTheRadioButton($labelText)
{
$page = $this->getSession()->getPage();
$radioButton = $page->find('named', ['radio', $labelText]);
if ($radioButton) {
$select = $radioButton->getAttribute('name');
$option = $radioButton->getAttribute('value');
$page->selectFieldOption($select, $option);
return;
}
throw new \Exception("Radio button with label {$labelText} not found");
}
}