Skip to content

Commit d19ec0c

Browse files
committed
Fix page navigation that does not support loader to be replaced
#40
1 parent 7ce409b commit d19ec0c

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Bug fixes:
1212
* none
1313

14-
## 0.2.5
14+
## 0.3.0
1515

1616
> *20xx-xx-xx* (not released)
1717
@@ -23,7 +23,7 @@
2323
* Removed unused option ``debug``
2424
* Added ``BrowserFactory::connectToBrowser``
2525
* Bug fixes:
26-
* none
26+
* (BC Break) Page navigation now allows by default that the initial loader is replaced with a new one #40
2727

2828
## 0.2.4
2929

src/Page.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,18 @@ public function getSession(): Session
8989
}
9090

9191
/**
92-
* @param $url
92+
* @param string $url
93+
* @param array $options
94+
* - strict: make waitForNAvigation to fail if a new navigation is initiated. Default: false
95+
*
9396
* @return PageNavigation
97+
* @throws Exception\CommunicationException
9498
*/
95-
public function navigate($url)
99+
public function navigate(string $url, array $options = [])
96100
{
97101
$this->assertNotClosed();
98102

99-
return new PageNavigation($this, $url);
103+
return new PageNavigation($this, $url, $options['strict'] ?? false);
100104
}
101105

102106
/**

src/PageUtils/PageNavigation.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,24 @@ class PageNavigation
5050
*/
5151
protected $page;
5252

53+
/**
54+
* @var bool
55+
*/
56+
protected $strict;
57+
5358
/**
5459
* PageNavigation constructor.
5560
* @param Page $page
5661
* @param string $url
62+
* @param bool $strict by default this method will wait for the page to load even if a new navigation occurs
63+
* (ie: a new loader replaced the initial navigation). Passing $string to true will make the navigation to fail
64+
* if a new loader is generated
65+
*
5766
* @throws Exception\CommunicationException
5867
* @throws Exception\CommunicationException\CannotReadResponse
5968
* @throws Exception\CommunicationException\InvalidResponse
60-
* @throws Exception\NoResponseAvailable
6169
*/
62-
public function __construct(Page $page, string $url)
70+
public function __construct(Page $page, string $url, bool $strict = false)
6371
{
6472

6573
// make sure latest loaderId was pulled
@@ -76,6 +84,7 @@ public function __construct(Page $page, string $url)
7684
$this->page = $page;
7785
$this->frame = $page->getFrameManager()->getMainFrame();
7886
$this->url = $url;
87+
$this->strict = $strict;
7988
}
8089

8190
/**
@@ -96,7 +105,7 @@ public function __construct(Page $page, string $url)
96105
* ```
97106
*
98107
* @param string $eventName
99-
* @param int $timeout
108+
* @param int $timeout time in ms to wait for the navigation to complete. Default 30000 (30 seconds)
100109
* @return mixed|null
101110
* @throws Exception\CommunicationException\CannotReadResponse
102111
* @throws Exception\CommunicationException\InvalidResponse
@@ -105,15 +114,19 @@ public function __construct(Page $page, string $url)
105114
* @throws NavigationExpired
106115
* @throws ResponseHasError
107116
*/
108-
public function waitForNavigation($eventName = Page::LOAD, $timeout = 30000)
117+
public function waitForNavigation($eventName = Page::LOAD, int $timeout = null)
109118
{
119+
if (null === $timeout) {
120+
$timeout = 30000;
121+
}
110122
return Utils::tryWithTimeout($timeout * 1000, $this->navigationComplete($eventName));
111123
}
112124

113125
/**
114126
* To be used with @see Utils::tryWithTimeout
115127
*
116-
* @param $eventName
128+
* @param string $eventName
129+
*
117130
* @return bool|\Generator
118131
* @throws Exception\CommunicationException\CannotReadResponse
119132
* @throws Exception\CommunicationException\InvalidResponse
@@ -126,6 +139,7 @@ private function navigationComplete($eventName)
126139
$delay = 500;
127140

128141
while (true) {
142+
// read the response only if it was not read already
129143
if (!$this->navigateResponseReader->hasResponse()) {
130144
$this->navigateResponseReader->checkForResponse();
131145
if ($this->navigateResponseReader->hasResponse()) {
@@ -163,9 +177,14 @@ private function navigationComplete($eventName)
163177

164178
// else if a new loader is present that means that a new navigation started
165179
} else {
166-
throw new NavigationExpired(
167-
'The page has navigated to an other page and this navigation expired'
168-
);
180+
// if strict then throw or else replace the old navigation with the new one
181+
if ($this->strict) {
182+
throw new NavigationExpired(
183+
'The page has navigated to an other page and this navigation expired'
184+
);
185+
} else {
186+
$this->currentLoaderId = $this->frame->getLatestLoaderId();
187+
}
169188
}
170189

171190
$this->page->getSession()->getConnection()->readData();

test/suites/BrowsingTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,18 @@ public function testGetCurrentUrl()
8585

8686
$this->assertEquals($this->sitePath('a.html'), $page->getCurrentUrl());
8787
}
88+
89+
public function testPageNavigationLocalNotFoundUrl()
90+
{
91+
$factory = new BrowserFactory();
92+
$browser = $factory->createBrowser();
93+
94+
$page = $browser->createPage();
95+
96+
// for some reasons chrome creates a new loader when we navigate to a local non-existent file
97+
// here we are testing that feature with strict and non strict modes
98+
$page->navigate('file:///does-not-exist')->waitForNavigation();
99+
100+
$this->assertTrue(true);
101+
}
88102
}

0 commit comments

Comments
 (0)