Passed
Pull Request — master (#7)
by Raúl
01:42
created

SeleniumHelper::removeCookiesNotification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 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;
10
11
/**
12
 * Class SeleniumHelper
13
 * @package Pagantis\SeleniumFormUtils
14
 */
15
class SeleniumHelper
16
{
17
    /**
18
     * Form base domain, initial status to verify before start testing
19
     */
20
    const FORM_BASE_URL = 'https://form.sbx.pagantis.com';
21
22
    /**
23
     * @var WebDriver
24
     */
25
    protected static $webDriver;
26
27
    /**
28
     * @var string $mobilePhone needed to identify returning users
29
     */
30
    public static $mobilePhone = null;
31
32
    /**
33
     * @var array $arraySteps
34
     */
35
    public static $arraySteps = array (
36
        '25' => 'ConfirmData',
37
        '50' => 'Missing',
38
        '75' => 'Application',
39
        '100' => 'Rejected',
40
    );
41
42
    /**
43
     * @param WebDriver $webDriver
44
     * @param bool $rejected
45
     *
46
     * @throws \Exception
47
     */
48
    public static function finishForm(WebDriver $webDriver, $rejected = false)
49
    {
50
        self::$webDriver = $webDriver;
51
        self::waitToLoad();
52
        self::validateFormUrl();
53
        $maxSteps = 20;
54
        do {
55
            self::waitToLoad();
56
            $formStep = self::getFormStep();
57
            $formStepClass = "\\".self::getStepClass($formStep);
58
            /** @var AbstractStep $stepClass */
59
            $stepClass = new $formStepClass(self::$webDriver);
60
            $stepClass->run($rejected);
61
            --$maxSteps;
62
        } while ($formStep !== Rejected::STEP && $maxSteps > 0);
63
64
        if ($maxSteps <= 0) {
65
            throw new \Exception('Error while finishing form, step: ' . $formStep);
66
        }
67
    }
68
69
    /**
70
     * @param WebDriver $webDriver
71
     *
72
     * @throws \Exception
73
     */
74
    public static function cancelForm(WebDriver $webDriver)
75
    {
76
        self::$webDriver = $webDriver;
77
        self::waitToLoad();
78
        self::validateFormUrl();
79
80
        self::$webDriver->wait(90, 1500)->until(
81
            WebDriverExpectedCondition::elementToBeClickable(
82
                WebDriverBy::name('back_to_store_button')
83
            )
84
        );
85
86
        $formCancel = self::$webDriver->findElement(WebDriverBy::name('back_to_store_button'));
87
        $formCancel->click();
88
    }
89
90
    /**
91
     * @throws \Exception
92
     */
93
    protected static function validateFormUrl()
94
    {
95
        $currentUrl = self::$webDriver->getCurrentURL();
96
        if (strpos($currentUrl, self::FORM_BASE_URL) === false) {
97
            throw new \Exception('Unable to identify form url');
98
        }
99
    }
100
101
    /**
102
     * Get the step of the breadcrumb progress bar
103
     *
104
     * @return string
105
     */
106
    protected static function getFormStep()
107
    {
108
109
        return self::$arraySteps[
110
            self::$webDriver->findElement(WebDriverBy::cssSelector(".ProgressBar progress"))
111
            ->getAttribute("value")
112
        ];
113
    }
114
115
    /**
116
     * Turn the form step into a selenium handler class:
117
     * from: '/result/status-approved' to '\Result\StatusApproved'
118
     *
119
     * @param $formStep
120
     *
121
     * @return string
122
     */
123
    protected static function getStepClass($formStep)
124
    {
125
        $formSteps = explode(DIRECTORY_SEPARATOR, $formStep);
126
        $stepClass = 'Pagantis\SeleniumFormUtils\Step';
127
        foreach ($formSteps as $formStep) {
0 ignored issues
show
introduced by
$formStep is overwriting one of the parameters of this function.
Loading history...
128
            if ($formStep !== '') {
129
                $stepClass .= "\\".str_replace('-', '', ucwords($formStep, '-'));
130
            }
131
        }
132
133
        return $stepClass;
134
    }
135
136
    /**
137
     * @throws \Facebook\WebDriver\Exception\NoSuchElementException
138
     * @throws \Facebook\WebDriver\Exception\TimeOutException
139
     */
140
    public static function waitToLoad()
141
    {
142
        $element = WebDriverBy::cssSelector(".MainContainer");
143
        $condition = WebDriverExpectedCondition::presenceOfElementLocated($element);
144
        self::$webDriver->wait(90, 1500)->until($condition);
145
    }
146
}
147