1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PagaMasTarde\SeleniumFormUtils; |
4
|
|
|
|
5
|
|
|
use Facebook\WebDriver\WebDriver; |
6
|
|
|
use Facebook\WebDriver\WebDriverBy; |
7
|
|
|
use Facebook\WebDriver\WebDriverExpectedCondition; |
8
|
|
|
use PagaMasTarde\SeleniumFormUtils\Step\AbstractStep; |
9
|
|
|
use PagaMasTarde\SeleniumFormUtils\Step\Result\Status\Approved; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class SeleniumHelper |
13
|
|
|
* @package PagaMasTarde\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.pagamastarde.com'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var WebDriver |
24
|
|
|
*/ |
25
|
|
|
static protected $webDriver; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string $mobilePhone needed to identify returning users |
29
|
|
|
*/ |
30
|
|
|
static public $mobilePhone = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param WebDriver $webDriver |
34
|
|
|
* @param string $mobilePhone |
35
|
|
|
* |
36
|
|
|
* @throws \Exception |
37
|
|
|
*/ |
38
|
|
|
public static function finishForm(WebDriver $webDriver, $mobilePhone = null) |
39
|
|
|
{ |
40
|
|
|
self::$webDriver = $webDriver; |
41
|
|
|
self::$mobilePhone = $mobilePhone; |
42
|
|
|
self::waitToLoad(); |
43
|
|
|
self::removeCookiesNotification(); |
44
|
|
|
self::validateFormUrl(); |
45
|
|
|
$maxSteps = 15; |
46
|
|
|
do { |
47
|
|
|
$formStep = self::getFormStep(); |
48
|
|
|
$formStepClass = self::getStepClass($formStep); |
49
|
|
|
/** @var AbstractStep $stepClass */ |
50
|
|
|
$stepClass = new $formStepClass(self::$webDriver); |
51
|
|
|
$stepClass->run(); |
52
|
|
|
self::waitToLoad(); |
53
|
|
|
--$maxSteps; |
54
|
|
|
} while ($formStep !== Approved::STEP && $maxSteps > 0); |
55
|
|
|
|
56
|
|
|
if ($maxSteps <= 0) { |
57
|
|
|
throw new \Exception('Error while finishing form, step: ' . $formStep); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param WebDriver $webDriver |
63
|
|
|
* @param string $mobilePhone |
64
|
|
|
* |
65
|
|
|
* @throws \Exception |
66
|
|
|
*/ |
67
|
|
|
public static function cancelForm(WebDriver $webDriver, $mobilePhone = null) |
68
|
|
|
{ |
69
|
|
|
self::$webDriver = $webDriver; |
70
|
|
|
self::$mobilePhone = $mobilePhone; |
71
|
|
|
self::waitToLoad(); |
72
|
|
|
self::removeCookiesNotification(); |
73
|
|
|
self::validateFormUrl(); |
74
|
|
|
|
75
|
|
|
self::$webDriver->wait()->until( |
76
|
|
|
WebDriverExpectedCondition::elementToBeClickable( |
77
|
|
|
WebDriverBy::name('back_to_store_button') |
78
|
|
|
) |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$formCancel = self::$webDriver->findElement(WebDriverBy::name('back_to_store_button')); |
82
|
|
|
$formCancel->click(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @throws \Exception |
87
|
|
|
*/ |
88
|
|
|
protected static function validateFormUrl() |
89
|
|
|
{ |
90
|
|
|
$currentUrl = self::$webDriver->getCurrentURL(); |
91
|
|
|
if (strpos($currentUrl, self::FORM_BASE_URL) === false) { |
92
|
|
|
throw new \Exception('Unable to identify form url'); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the step of the form from the URL: '/result/status/approved' |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
protected static function getFormStep() |
102
|
|
|
{ |
103
|
|
|
$path = parse_url(self::$webDriver->getCurrentURL(), PHP_URL_PATH); |
104
|
|
|
$arguments = explode(DIRECTORY_SEPARATOR, $path); |
105
|
|
|
$step = ''; |
106
|
|
|
for ($i = 2; $i < count($arguments); $i++) { |
|
|
|
|
107
|
|
|
$step .= DIRECTORY_SEPARATOR.$arguments[$i]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $step; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Turn the form step into a selenium handler class: |
115
|
|
|
* from: '/result/status-approved' to '\Result\StatusApproved' |
116
|
|
|
* |
117
|
|
|
* @param $formStep |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
protected static function getStepClass($formStep) |
122
|
|
|
{ |
123
|
|
|
$formSteps = explode(DIRECTORY_SEPARATOR, $formStep); |
124
|
|
|
$stepClass = 'PagaMasTarde\SeleniumFormUtils\Step'; |
125
|
|
|
foreach ($formSteps as $formStep) { |
|
|
|
|
126
|
|
|
if ($formStep !== '') { |
127
|
|
|
$stepClass .= "\\".str_replace('-', '', ucwords($formStep, '-')); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $stepClass; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @throws \Facebook\WebDriver\Exception\NoSuchElementException |
136
|
|
|
* @throws \Facebook\WebDriver\Exception\TimeOutException |
137
|
|
|
*/ |
138
|
|
|
public static function waitToLoad() |
139
|
|
|
{ |
140
|
|
|
$element = WebDriverBy::cssSelector(".Loading .is-disabled"); |
141
|
|
|
$condition = WebDriverExpectedCondition::presenceOfElementLocated($element); |
142
|
|
|
self::$webDriver->wait()->until($condition); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @throws \Facebook\WebDriver\Exception\NoSuchElementException |
147
|
|
|
* @throws \Facebook\WebDriver\Exception\TimeOutException |
148
|
|
|
*/ |
149
|
|
|
public static function removeCookiesNotification() |
150
|
|
|
{ |
151
|
|
|
$element = WebDriverBy::id('sg-notification-global-trigger'); |
152
|
|
|
$condition = WebDriverExpectedCondition::presenceOfElementLocated($element); |
153
|
|
|
self::$webDriver->wait()->until($condition); |
154
|
|
|
self::$webDriver->findElement($element)->click(); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: