Passed
Pull Request — master (#14)
by pablo
01:55
created

SeleniumHelper::finishForm()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 22
c 3
b 0
f 0
dl 0
loc 30
rs 8.9457
cc 6
nc 20
nop 2
1
<?php
2
3
namespace Pagantis\SeleniumFormUtils;
4
5
use Facebook\WebDriver\WebDriver;
6
use Facebook\WebDriver\WebDriverBy;
7
use Facebook\WebDriver\WebDriverExpectedCondition;
8
use Pagantis\SeleniumFormUtils\Step\AbstractStep;
9
use Pagantis\SeleniumFormUtils\Step\Rejected;
0 ignored issues
show
Bug introduced by
The type Pagantis\SeleniumFormUtils\Step\Rejected was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Pagantis\SeleniumFormUtils\Step\AccountVerification;
11
12
/**
13
 * Class SeleniumHelper
14
 * @package Clearpay\SeleniumFormUtils
15
 */
16
class SeleniumHelper
17
{
18
    /**
19
     * Form base domain, initial status to verify before start testing
20
     */
21
    const FORM_BASE_URL = 'clearpay.com';
22
23
    /**
24
     *
25
     */
26
    const CLEARPAY_TITLE = 'Clearpay';
27
28
    /**
29
     * @var WebDriver
30
     */
31
    protected static $webDriver;
32
33
    /**
34
     * @param WebDriver $webDriver
35
     * @param bool      $rejected
36
     * @return string Useful to parse the exit step
37
     * @throws \Facebook\WebDriver\Exception\NoSuchElementException
38
     * @throws \Facebook\WebDriver\Exception\TimeOutException
39
     */
40
    public static function finishForm(WebDriver $webDriver, $rejected = false)
41
    {
42
        try {
43
            self::$webDriver = $webDriver;
44
            self::waitToLoad();
45
            self::validateFormUrl();
46
            $maxSteps = 20;
47
            do {
48
                self::waitToLoad();
49
                $formStep = self::getFormStep();
50
                if(self::stepIsExcluded($formStep)){
51
                    $continue = true;
52
                    continue;
53
                }
54
                $formStepClass = self::getStepClass($formStep);
55
                /** @var AbstractStep $stepClass */
56
                $stepClass = new $formStepClass(self::$webDriver);
57
                $continue = $stepClass->run($rejected);
58
                --$maxSteps;
59
            } while ($continue && $maxSteps>0);
60
        } catch (\Exception $exception) {
61
            echo $exception->getMessage();
62
            echo self::$webDriver->getCurrentURL();
63
        }
64
65
        if ($maxSteps <= 0) {
66
            throw new \Exception('Error while finishing form, step: ' . $formStep);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $formStep does not seem to be defined for all execution paths leading up to this point.
Loading history...
67
        }
68
69
        return $formStep;
70
    }
71
72
    /**
73
     * @param $currentStep
74
     *
75
     * @return bool
76
     */
77
    public static function stepIsExcluded($currentStep)
78
    {
79
        return (substr($currentStep,0,4) === '004.');
80
    }
81
82
    /**
83
     * @param WebDriver $webDriver
84
     *
85
     * @throws \Exception
86
     */
87
    public static function cancelForm(WebDriver $webDriver)
88
    {
89
        self::$webDriver = $webDriver;
90
        self::waitToLoad();
91
        self::validateFormUrl();
92
93
        self::$webDriver->wait(90, 1500)->until(
94
            WebDriverExpectedCondition::elementToBeClickable(
95
                WebDriverBy::name('back_to_store_button')
96
            )
97
        );
98
99
        $formCancel = self::$webDriver->findElement(WebDriverBy::name('back_to_store_button'));
100
        $formCancel->click();
101
    }
102
103
    /**
104
     * @throws \Exception
105
     */
106
    protected static function validateFormUrl()
107
    {
108
        $currentUrl = self::$webDriver->getCurrentURL();
109
        if (strpos($currentUrl, self::FORM_BASE_URL) === false) {
110
            throw new \Exception('Unable to identify form url');
111
        }
112
    }
113
114
    /**
115
     * Get the step of the url
116
     *
117
     * @return string
118
     */
119
    protected static function getFormStep()
120
    {
121
        $formStep = explode(DIRECTORY_SEPARATOR, self::$webDriver->getCurrentURL());
122
123
        return array_pop($formStep);
124
    }
125
126
    /**
127
     * Turn the form step into a selenium handler class:
128
     * from: 'status-approved' to 'StatusApproved'
129
     *
130
     * @param $formStep
131
     *
132
     * @return string
133
     */
134
    protected static function getStepClass($formStep)
135
    {
136
        $formSteps = explode(DIRECTORY_SEPARATOR, $formStep);
137
        $stepClass = 'Pagantis\SeleniumFormUtils\Step';
138
        foreach ($formSteps as $formStep) {
0 ignored issues
show
introduced by
$formStep is overwriting one of the parameters of this function.
Loading history...
139
            if ($formStep !== '') {
140
                $stepClass .= "\\".str_replace('-', '', ucwords($formStep, '-'));
141
            }
142
        }
143
144
        return $stepClass;
145
    }
146
147
    /**
148
     * @throws \Facebook\WebDriver\Exception\NoSuchElementException
149
     * @throws \Facebook\WebDriver\Exception\TimeOutException
150
     */
151
    public static function waitToLoad()
152
    {
153
        $condition = WebDriverExpectedCondition::titleContains(self::CLEARPAY_TITLE);
154
        self::$webDriver->wait(90, 1500)
155
                        ->until($condition, self::$webDriver->getCurrentURL());
156
    }
157
}
158