-
Notifications
You must be signed in to change notification settings - Fork 318
Expand file tree
/
Copy pathPageNavigation.php
More file actions
194 lines (170 loc) · 6.01 KB
/
PageNavigation.php
File metadata and controls
194 lines (170 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/*
* This file is part of Chrome PHP.
*
* (c) Soufiane Ghzal <sghzal@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace HeadlessChromium\PageUtils;
use HeadlessChromium\Communication\Message;
use HeadlessChromium\Communication\ResponseReader;
use HeadlessChromium\Exception;
use HeadlessChromium\Exception\CommunicationException\ResponseHasError;
use HeadlessChromium\Exception\NavigationExpired;
use HeadlessChromium\Frame;
use HeadlessChromium\Page;
use HeadlessChromium\Utils;
/**
* A class that is aimed to be used withing the method Page::navigate.
*/
class PageNavigation
{
/**
* @var Frame
*/
protected $frame;
/**
* @var string
*/
protected $previousLoaderId;
/**
* @var string
*/
protected $currentLoaderId;
/**
* @var ResponseReader
*/
protected $navigateResponseReader;
/**
* @var string
*/
protected $url;
/**
* @var Page
*/
protected $page;
/**
* @var bool
*/
protected $strict;
/**
* PageNavigation constructor.
*
* @param Page $page
* @param string $url
* @param bool $strict by default this method will wait for the page to load even if a new navigation occurs
* (ie: a new loader replaced the initial navigation). Passing $string to true will make the navigation to fail
* if a new loader is generated
*
* @throws Exception\CommunicationException
* @throws Exception\CommunicationException\CannotReadResponse
* @throws Exception\CommunicationException\InvalidResponse
*/
public function __construct(Page $page, string $url, bool $strict = false)
{
// make sure latest loaderId was pulled
$page->getSession()->getConnection()->readData();
// get previous loaderId for the navigation watcher
$this->previousLoaderId = $page->getFrameManager()->getMainFrame()->getLatestLoaderId();
// send navigation message
$this->navigateResponseReader = $page->getSession()->sendMessage(
new Message('Page.navigate', ['url' => $url])
);
$this->page = $page;
$this->frame = $page->getFrameManager()->getMainFrame();
$this->url = $url;
$this->strict = $strict;
}
/**
* Wait until the page loads.
*
* Usage:
*
* ```php
* $navigation = $page->navigate('http://example.com');
* try {
* // wait max 30 seconds for dom content to load
* $navigation->waitForNavigation(Page::DOM_CONTENT_LOADED, 30000);
* } catch (OperationTimedOut $e) {
* // too long to load
* } catch (NavigationExpired $e) {
* // an other page loaded since this navigation was initiated
* }
* ```
*
* @param string $eventName
* @param int $timeout time in ms to wait for the navigation to complete. Default 30000 (30 seconds)
*
* @throws Exception\CommunicationException\CannotReadResponse
* @throws Exception\CommunicationException\InvalidResponse
* @throws Exception\NoResponseAvailable
* @throws Exception\OperationTimedOut
* @throws NavigationExpired
* @throws ResponseHasError
*
* @return mixed
*/
public function waitForNavigation($eventName = Page::LOAD, ?int $timeout = null)
{
if (null === $timeout) {
$timeout = 30000;
}
return Utils::tryWithTimeout($timeout * 1000, $this->navigationComplete($eventName));
}
/**
* To be used with @see Utils::tryWithTimeout.
*
* @param string $eventName
*
* @throws Exception\CommunicationException\CannotReadResponse
* @throws Exception\CommunicationException\InvalidResponse
* @throws Exception\NoResponseAvailable
* @throws NavigationExpired
* @throws ResponseHasError
*
* @return bool|\Generator
*/
private function navigationComplete($eventName)
{
$delay = 500;
while (true) {
// read the response only if it was not read already
if (!$this->navigateResponseReader->hasResponse()) {
$this->navigateResponseReader->checkForResponse();
if ($this->navigateResponseReader->hasResponse()) {
$response = $this->navigateResponseReader->getResponse();
if (!$response->isSuccessful()) {
throw new ResponseHasError(\sprintf('Cannot load page for url: "%s". Reason: %s', $this->url, $response->getErrorMessage()));
}
$this->currentLoaderId = $response->getResultData('loaderId');
} else {
yield $delay;
}
}
// make sure that the current loader is the good one
if ($this->frame->getLatestLoaderId() === $this->currentLoaderId) {
// check that lifecycle event exists
if ($this->page->hasLifecycleEvent($eventName)) {
return true;
// or else just wait for the new event to trigger
} else {
yield $delay;
}
// else if frame has still the previous loader, wait for the new one
} elseif ($this->frame->getLatestLoaderId() == $this->previousLoaderId) {
yield $delay;
// else if a new loader is present that means that a new navigation started
} else {
// if strict then throw or else replace the old navigation with the new one
if ($this->strict) {
throw new NavigationExpired('The page has navigated to an other page and this navigation expired');
} else {
$this->currentLoaderId = $this->frame->getLatestLoaderId();
}
}
$this->page->getSession()->getConnection()->readData();
}
}
}