Skip to content
83 changes: 46 additions & 37 deletions src/SeleniumHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,28 @@
use Facebook\WebDriver\WebDriverExpectedCondition;
use Pagantis\SeleniumFormUtils\Step\AbstractStep;
use Pagantis\SeleniumFormUtils\Step\Rejected;
use Pagantis\SeleniumFormUtils\Step\AccountVerification;

/**
* Class SeleniumHelper
* @package Pagantis\SeleniumFormUtils
* @package Clearpay\SeleniumFormUtils
*/
class SeleniumHelper
{
/**
* Form base domain, initial status to verify before start testing
*/
const FORM_BASE_URL = 'https://form.sbx.pagantis.com';
const FORM_BASE_URL = 'clearpay.com';

/**
* @var WebDriver
*/
protected static $webDriver;

/**
* @var string $mobilePhone needed to identify returning users
*
*/
public static $mobilePhone = null;
const CLEARPAY_TITLE = 'Clearpay';

/**
* @var array $arraySteps
* @var WebDriver
*/
public static $arraySteps = array (
'25' => 'ConfirmData',
'50' => 'Missing',
'75' => 'Application',
'100' => 'Rejected',
);
protected static $webDriver;

/**
* @param WebDriver $webDriver
Expand All @@ -48,19 +39,29 @@ class SeleniumHelper
*/
public static function finishForm(WebDriver $webDriver, $rejected = false)
{
self::$webDriver = $webDriver;
self::waitToLoad();
self::validateFormUrl();
$maxSteps = 20;
do {
try {
self::$webDriver = $webDriver;
self::waitToLoad();
$formStep = self::getFormStep();
$formStepClass = "\\".self::getStepClass($formStep);
/** @var AbstractStep $stepClass */
$stepClass = new $formStepClass(self::$webDriver);
$continue = $stepClass->run($rejected);
--$maxSteps;
} while ($continue && $formStep !== Rejected::STEP && $maxSteps > 0);
self::validateFormUrl();
$maxSteps = 20;
do {
self::waitToLoad();
$formStep = self::getFormStep();
if(self::stepIsExcluded($formStep)){
$continue = true;
continue;
}
$formStepClass = self::getStepClass($formStep);
/** @var AbstractStep $stepClass */
$stepClass = new $formStepClass(self::$webDriver);
$continue = $stepClass->run($rejected);
--$maxSteps;
} while ($continue && $maxSteps>0);
} catch (\Exception $exception) {
echo $exception->getMessage();
echo self::$webDriver->getCurrentURL();
echo self::$webDriver->getPageSource();
}

if ($maxSteps <= 0) {
throw new \Exception('Error while finishing form, step: ' . $formStep);
Expand All @@ -69,6 +70,16 @@ public static function finishForm(WebDriver $webDriver, $rejected = false)
return $formStep;
}

/**
* @param $currentStep
*
* @return bool
*/
public static function stepIsExcluded($currentStep)
{
return (substr($currentStep,0,4) === '004.');
}

/**
* @param WebDriver $webDriver
*
Expand Down Expand Up @@ -102,22 +113,20 @@ protected static function validateFormUrl()
}

/**
* Get the step of the breadcrumb progress bar
* Get the step of the url
*
* @return string
*/
protected static function getFormStep()
{
$formStep = explode(DIRECTORY_SEPARATOR, self::$webDriver->getCurrentURL());

return self::$arraySteps[
self::$webDriver->findElement(WebDriverBy::cssSelector(".ProgressBar progress"))
->getAttribute("value")
];
return array_pop($formStep);
}

/**
* Turn the form step into a selenium handler class:
* from: '/result/status-approved' to '\Result\StatusApproved'
* from: 'status-approved' to 'StatusApproved'
*
* @param $formStep
*
Expand All @@ -142,8 +151,8 @@ protected static function getStepClass($formStep)
*/
public static function waitToLoad()
{
$element = WebDriverBy::cssSelector(".MainContainer");
$condition = WebDriverExpectedCondition::presenceOfElementLocated($element);
self::$webDriver->wait(90, 1500)->until($condition);
$condition = WebDriverExpectedCondition::titleContains(self::CLEARPAY_TITLE);
self::$webDriver->wait(90, 1500)
->until($condition, self::$webDriver->getCurrentURL());
}
}
17 changes: 0 additions & 17 deletions src/Step/AbstractStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,6 @@ public function waitTobeVisible(WebDriverBy $webDriverBy)
return $this->webDriver->findElement($webDriverBy);
}

/**
* @param string $step
*
* @throws \Exception
*/
public function validateStep($step)
{
$currentStep = SeleniumHelper::$arraySteps[
$this->webDriver->findElement(WebDriverBy::cssSelector(".ProgressBar progress"))
->getAttribute("value")
];

if ($step !== $currentStep) {
throw new \Exception('Wrong step: ' . $arraySteps[$step]);
}
}

/**
* @param $iFrameLocator
*
Expand Down
48 changes: 48 additions & 0 deletions src/Step/AccountVerification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Pagantis\SeleniumFormUtils\Step;

use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverExpectedCondition;
use Clearpay\Clearpay\Test\ClearpayMagentoTest;

/**
* Class AccountVerification
*
* @package Clearpay\SeleniumFormUtils\Step
*/
class AccountVerification extends AbstractStep
{
/**
* Handler step
*/
const STEP = 'AccountVerification';

const EMAIL = 'antonio@clearpay.com';

/**
* First step Confirm retrieved data
*
* @param bool $rejected
* @return bool
* @throws \Exception
*/
public function run($rejected = false)
{
//Wait after redirection
$simulatorElementSearch = WebDriverBy::name('email');
$condition = WebDriverExpectedCondition::presenceOfElementLocated($simulatorElementSearch);
$this->webDriver->wait()->until($condition);

//Fill email
$fillEmail = $this->webDriver->findElement($simulatorElementSearch)->clear()->sendKeys(self::EMAIL);

//Click on confirm
$formContinue = $this->webDriver->findElement(WebDriverBy::id('continueBtn'));
$formContinue->click();

sleep(5);

return true;
}
}
85 changes: 0 additions & 85 deletions src/Step/Application.php

This file was deleted.

37 changes: 0 additions & 37 deletions src/Step/ConfirmData.php

This file was deleted.

Loading