Passed
Pull Request — master (#7)
by Raúl
02:05
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\Application;
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
    );
40
41
    /**
42
     * @param WebDriver $webDriver
43
     * @param string $mobilePhone
44
     *
45
     * @throws \Exception
46
     */
47
    public static function finishForm(WebDriver $webDriver, $mobilePhone = null)
48
    {
49
        self::$webDriver = $webDriver;
50
        self::$mobilePhone = $mobilePhone;
51
        self::waitToLoad();
52
        self::validateFormUrl();
53
        $maxSteps = 15;
54
        do {
55
            $formStep = self::getFormStep();
56
            $formStepClass = "\\".self::getStepClass($formStep);
57
            /** @var AbstractStep $stepClass */
58
            $stepClass = new $formStepClass(self::$webDriver);
59
            $stepClass->run();
60
            self::waitToLoad();
61
            --$maxSteps;
62
        } while ($formStep !== Application::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
     * @param string $mobilePhone
72
     *
73
     * @throws \Exception
74
     */
75
    public static function cancelForm(WebDriver $webDriver, $mobilePhone = null)
76
    {
77
        self::$webDriver = $webDriver;
78
        self::$mobilePhone = $mobilePhone;
79
        self::waitToLoad();
80
        self::validateFormUrl();
81
82
        self::$webDriver->wait(90, 1500)->until(
83
            WebDriverExpectedCondition::elementToBeClickable(
84
                WebDriverBy::name('back_to_store_button')
85
            )
86
        );
87
88
        $formCancel = self::$webDriver->findElement(WebDriverBy::name('back_to_store_button'));
89
        $formCancel->click();
90
    }
91
92
    /**
93
     * @throws \Exception
94
     */
95
    protected static function validateFormUrl()
96
    {
97
        $currentUrl = self::$webDriver->getCurrentURL();
98
        if (strpos($currentUrl, self::FORM_BASE_URL) === false) {
99
            throw new \Exception('Unable to identify form url');
100
        }
101
    }
102
103
    /**
104
     * Get the step of the breadcrumb progress bar
105
     *
106
     * @return string
107
     */
108
    protected static function getFormStep()
109
    {
110
111
        return self::$arraySteps[
112
            self::$webDriver->findElement(WebDriverBy::cssSelector(".ProgressBar progress"))
113
            ->getAttribute("value")
114
        ];
115
    }
116
117
    /**
118
     * Turn the form step into a selenium handler class:
119
     * from: '/result/status-approved' to '\Result\StatusApproved'
120
     *
121
     * @param $formStep
122
     *
123
     * @return string
124
     */
125
    protected static function getStepClass($formStep)
126
    {
127
        $formSteps = explode(DIRECTORY_SEPARATOR, $formStep);
128
        $stepClass = 'Pagantis\SeleniumFormUtils\Step';
129
        foreach ($formSteps as $formStep) {
0 ignored issues
show
introduced by
$formStep is overwriting one of the parameters of this function.
Loading history...
130
            if ($formStep !== '') {
131
                $stepClass .= "\\".str_replace('-', '', ucwords($formStep, '-'));
132
            }
133
        }
134
135
        return $stepClass;
136
    }
137
138
    /**
139
     * @throws \Facebook\WebDriver\Exception\NoSuchElementException
140
     * @throws \Facebook\WebDriver\Exception\TimeOutException
141
     */
142
    public static function waitToLoad()
143
    {
144
        $element = WebDriverBy::cssSelector(".MainContainer");
145
        $condition = WebDriverExpectedCondition::presenceOfElementLocated($element);
146
        self::$webDriver->wait(90, 1500)->until($condition);
147
    }
148
}
149