SeleniumHelper::validateFormUrl()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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