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

SeleniumHelper   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 15
eloc 50
c 4
b 0
f 0
dl 0
loc 141
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A stepIsExcluded() 0 3 1
A getFormStep() 0 5 1
A waitToLoad() 0 5 1
B finishForm() 0 31 6
A getStepClass() 0 11 3
A cancelForm() 0 14 1
A validateFormUrl() 0 5 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
            echo self::$webDriver->getPageSource();
64
        }
65
66
        if ($maxSteps <= 0) {
67
            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...
68
        }
69
70
        return $formStep;
71
    }
72
73
    /**
74
     * @param $currentStep
75
     *
76
     * @return bool
77
     */
78
    public static function stepIsExcluded($currentStep)
79
    {
80
        return (substr($currentStep,0,4) === '004.');
81
    }
82
83
    /**
84
     * @param WebDriver $webDriver
85
     *
86
     * @throws \Exception
87
     */
88
    public static function cancelForm(WebDriver $webDriver)
89
    {
90
        self::$webDriver = $webDriver;
91
        self::waitToLoad();
92
        self::validateFormUrl();
93
94
        self::$webDriver->wait(90, 1500)->until(
95
            WebDriverExpectedCondition::elementToBeClickable(
96
                WebDriverBy::name('back_to_store_button')
97
            )
98
        );
99
100
        $formCancel = self::$webDriver->findElement(WebDriverBy::name('back_to_store_button'));
101
        $formCancel->click();
102
    }
103
104
    /**
105
     * @throws \Exception
106
     */
107
    protected static function validateFormUrl()
108
    {
109
        $currentUrl = self::$webDriver->getCurrentURL();
110
        if (strpos($currentUrl, self::FORM_BASE_URL) === false) {
111
            throw new \Exception('Unable to identify form url');
112
        }
113
    }
114
115
    /**
116
     * Get the step of the url
117
     *
118
     * @return string
119
     */
120
    protected static function getFormStep()
121
    {
122
        $formStep = explode(DIRECTORY_SEPARATOR, self::$webDriver->getCurrentURL());
123
124
        return array_pop($formStep);
125
    }
126
127
    /**
128
     * Turn the form step into a selenium handler class:
129
     * from: 'status-approved' to 'StatusApproved'
130
     *
131
     * @param $formStep
132
     *
133
     * @return string
134
     */
135
    protected static function getStepClass($formStep)
136
    {
137
        $formSteps = explode(DIRECTORY_SEPARATOR, $formStep);
138
        $stepClass = 'Pagantis\SeleniumFormUtils\Step';
139
        foreach ($formSteps as $formStep) {
0 ignored issues
show
introduced by
$formStep is overwriting one of the parameters of this function.
Loading history...
140
            if ($formStep !== '') {
141
                $stepClass .= "\\".str_replace('-', '', ucwords($formStep, '-'));
142
            }
143
        }
144
145
        return $stepClass;
146
    }
147
148
    /**
149
     * @throws \Facebook\WebDriver\Exception\NoSuchElementException
150
     * @throws \Facebook\WebDriver\Exception\TimeOutException
151
     */
152
    public static function waitToLoad()
153
    {
154
        $condition = WebDriverExpectedCondition::titleContains(self::CLEARPAY_TITLE);
155
        self::$webDriver->wait(90, 1500)
156
                        ->until($condition, self::$webDriver->getCurrentURL());
157
    }
158
}
159