Skip to content

Commit 100b4cf

Browse files
committed
add ProcessAwareBrowser::getSocketUri #42
1 parent 7108699 commit 100b4cf

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
> *20xx-xx-xx* (not released)
1717
1818
> Make a crawl instance sharable among multiple scripts
19-
19+
2020
* Features:
2121
* Added option ``keepAlive`` for browser factory.
22+
* Added methods ``BrowserProcess::getSocketUri`` and ``ProcessAwareBrowser::getSocketUri``
2223
* Bug fixes:
2324
* none
2425

src/Browser/BrowserProcess.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ class BrowserProcess implements LoggerAwareInterface
6363
*/
6464
protected $wasStarted = false;
6565

66+
/**
67+
* @var string
68+
*/
69+
protected $wsUri;
70+
6671
/**
6772
* BrowserProcess constructor.
6873
* @param LoggerInterface $logger
@@ -122,13 +127,13 @@ public function start($binaries, $options)
122127

123128
// wait for start and retrieve ws uri
124129
$startupTimeout = $options['startupTimeout'] ?? 30;
125-
$ws = $this->waitForStartup($process, $startupTimeout * 1000 * 1000);
130+
$this->wsUri = $this->waitForStartup($process, $startupTimeout * 1000 * 1000);
126131

127132
// log
128-
$this->logger->debug('process: connecting using ' . $ws);
133+
$this->logger->debug('process: connecting using ' . $this->wsUri);
129134

130135
// connect to browser
131-
$connection = new Connection($ws, $this->logger, $options['sendSyncDefaultTimeout'] ?? 3000);
136+
$connection = new Connection($this->wsUri, $this->logger, $options['sendSyncDefaultTimeout'] ?? 3000);
132137
$connection->connect();
133138

134139
// connection delay
@@ -154,6 +159,14 @@ public function getBrowser()
154159
return $this->browser;
155160
}
156161

162+
/**
163+
* @return string
164+
*/
165+
public function getSocketUri()
166+
{
167+
return $this->wsUri;
168+
}
169+
157170
/**
158171
* Kills the process and clean temporary files
159172
* @throws OperationTimedOut

src/Browser/ProcessAwareBrowser.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,12 @@ public function close()
3131
{
3232
$this->browserProcess->kill();
3333
}
34+
35+
/**
36+
* @return string
37+
*/
38+
public function getSocketUri()
39+
{
40+
return $this->browserProcess->getSocketUri();
41+
}
3442
}

src/BrowserFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Apix\Log\Logger\Stream as StreamLogger;
99
use HeadlessChromium\Browser\BrowserProcess;
10+
use HeadlessChromium\Browser\ProcessAwareBrowser;
1011
use Symfony\Component\Process\Process;
1112

1213
class BrowserFactory
@@ -50,9 +51,9 @@ public function __construct(string $chromeBinaries = null)
5051
* - userDataDir: chrome user data dir (default: a new empty dir is generated temporarily)
5152
* - windowSize: size of the window, ex: [1920, 1080] (default: none)
5253
*
53-
* @return Browser a Browser instance to interact with the new chrome process
54+
* @return ProcessAwareBrowser a Browser instance to interact with the new chrome process
5455
*/
55-
public function createBrowser(array $options = []): Browser
56+
public function createBrowser(array $options = []): ProcessAwareBrowser
5657
{
5758

5859
// prepare logger

test/suites/BrowserFactoryTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,13 @@ public function testUserAgentOption()
4343

4444
$this->assertEquals('foo bar baz', $response);
4545
}
46+
47+
public function testBrowserFactory()
48+
{
49+
$factory = new BrowserFactory();
50+
51+
$browser = $factory->createBrowser();
52+
53+
$this->assertRegExp('#^ws://#', $browser->getSocketUri());
54+
}
4655
}

0 commit comments

Comments
 (0)