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

SeleniumHelper::getFormStep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 2
c 2
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
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
        self::$webDriver = $webDriver;
43
        self::waitToLoad();
44
        self::validateFormUrl();
45
        $maxSteps = 20;
46
        do {
47
            self::waitToLoad();
48
            $formStep = self::getFormStep();
49
            if(self::stepIsExcluded($formStep)){
50
                $continue = true;
51
                continue;
52
            }
53
            $formStepClass = self::getStepClass($formStep);
54
            /** @var AbstractStep $stepClass */
55
            $stepClass = new $formStepClass(self::$webDriver);
56
            $continue = $stepClass->run($rejected);
57
            --$maxSteps;
58
        } while ($continue && $maxSteps > 0);
59
60
        if ($maxSteps <= 0) {
61
            throw new \Exception('Error while finishing form, step: ' . $formStep);
62
        }
63
64
        return $formStep;
65
    }
66
67
    /**
68
     * @param $currentStep
69
     *
70
     * @return bool
71
     */
72
    public static function stepIsExcluded($currentStep)
73
    {
74
        return (substr($currentStep,0,4) === '004.');
75
    }
76
77
    /**
78
     * @param WebDriver $webDriver
79
     *
80
     * @throws \Exception
81
     */
82
    public static function cancelForm(WebDriver $webDriver)
83
    {
84
        self::$webDriver = $webDriver;
85
        self::waitToLoad();
86
        self::validateFormUrl();
87
88
        self::$webDriver->wait(90, 1500)->until(
89
            WebDriverExpectedCondition::elementToBeClickable(
90
                WebDriverBy::name('back_to_store_button')
91
            )
92
        );
93
94
        $formCancel = self::$webDriver->findElement(WebDriverBy::name('back_to_store_button'));
95
        $formCancel->click();
96
    }
97
98
    /**
99
     * @throws \Exception
100
     */
101
    protected static function validateFormUrl()
102
    {
103
        $currentUrl = self::$webDriver->getCurrentURL();
104
        if (strpos($currentUrl, self::FORM_BASE_URL) === false) {
105
            throw new \Exception('Unable to identify form url');
106
        }
107
    }
108
109
    /**
110
     * Get the step of the url
111
     *
112
     * @return string
113
     */
114
    protected static function getFormStep()
115
    {
116
        $formStep = explode(DIRECTORY_SEPARATOR, self::$webDriver->getCurrentURL());
117
118
        return array_pop($formStep);
119
    }
120
121
    /**
122
     * Turn the form step into a selenium handler class:
123
     * from: 'status-approved' to 'StatusApproved'
124
     *
125
     * @param $formStep
126
     *
127
     * @return string
128
     */
129
    protected static function getStepClass($formStep)
130
    {
131
        $formSteps = explode(DIRECTORY_SEPARATOR, $formStep);
132
        $stepClass = 'Pagantis\SeleniumFormUtils\Step';
133
        foreach ($formSteps as $formStep) {
0 ignored issues
show
introduced by
$formStep is overwriting one of the parameters of this function.
Loading history...
134
            if ($formStep !== '') {
135
                $stepClass .= "\\".str_replace('-', '', ucwords($formStep, '-'));
136
            }
137
        }
138
139
        return $stepClass;
140
    }
141
142
    /**
143
     * @throws \Facebook\WebDriver\Exception\NoSuchElementException
144
     * @throws \Facebook\WebDriver\Exception\TimeOutException
145
     */
146
    public static function waitToLoad()
147
    {
148
        $condition = WebDriverExpectedCondition::titleContains(self::CLEARPAY_TITLE);
149
        self::$webDriver->wait(90, 1500)
150
                        ->until($condition, self::$webDriver->getCurrentURL());
151
    }
152
}
153