Skip to content

Commit e009954

Browse files
coraxsterdunglas
authored andcommitted
Selenium (symfony#15)
* Selenium manager * Selenium manager create method * fix cs * simple test * fix selenium manager test * fix travis ci test, fix cs * fix cs * fix cs, refactor constructor * fix constructor * fix test
1 parent dd7c85c commit e009954

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

src/Client.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515

1616
use Facebook\WebDriver\WebDriver;
1717
use Facebook\WebDriver\WebDriverBy;
18+
use Facebook\WebDriver\WebDriverCapabilities;
1819
use Facebook\WebDriver\WebDriverExpectedCondition;
1920
use Panthere\Cookie\CookieJar;
2021
use Panthere\DomCrawler\Crawler;
2122
use Panthere\DomCrawler\Form as PanthereForm;
2223
use Panthere\DomCrawler\Link as PanthereLink;
2324
use Panthere\ProcessManager\BrowserManagerInterface;
2425
use Panthere\ProcessManager\ChromeManager;
26+
use Panthere\ProcessManager\SeleniumManager;
2527
use Symfony\Component\BrowserKit\Client as BaseClient;
2628
use Symfony\Component\BrowserKit\Request;
2729
use Symfony\Component\BrowserKit\Response;
@@ -49,6 +51,11 @@ public static function createChromeClient(?string $chromeDriverBinary = null, ?a
4951
return new self(new ChromeManager($chromeDriverBinary, $arguments));
5052
}
5153

54+
public static function createSeleniumClient(?string $host = null, ?WebDriverCapabilities $capabilities = null): self
55+
{
56+
return new self(new SeleniumManager($host, $capabilities));
57+
}
58+
5259
public function __construct(BrowserManagerInterface $browserManager)
5360
{
5461
$this->browserManager = $browserManager;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Panthère project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Panthere\ProcessManager;
15+
16+
use Facebook\WebDriver\Remote\DesiredCapabilities;
17+
use Facebook\WebDriver\Remote\RemoteWebDriver;
18+
use Facebook\WebDriver\WebDriver;
19+
use Facebook\WebDriver\WebDriverCapabilities;
20+
21+
/**
22+
* @author Dmitry Kuzmin <rockwith@me.com>
23+
*/
24+
final class SeleniumManager implements BrowserManagerInterface
25+
{
26+
private $host;
27+
private $capabilities;
28+
29+
public function __construct(
30+
?string $host = 'http://127.0.0.1:4444/wd/hub',
31+
?WebDriverCapabilities $capabilities = null
32+
) {
33+
$this->host = $host;
34+
$this->capabilities = $capabilities ?? DesiredCapabilities::chrome();
35+
}
36+
37+
public function start(): WebDriver
38+
{
39+
return RemoteWebDriver::create(
40+
$this->host,
41+
$this->capabilities
42+
);
43+
}
44+
45+
public function quit(): void
46+
{
47+
// nothing
48+
}
49+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Panthère project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Panthere\Tests\ProcessManager;
15+
16+
use Facebook\WebDriver\Chrome\ChromeOptions;
17+
use Panthere\ProcessManager\ChromeManager;
18+
use Panthere\ProcessManager\SeleniumManager;
19+
use PHPUnit\Framework\TestCase;
20+
21+
/**
22+
* @author Dmitry Kuzmin <rockwith@me.com>
23+
*/
24+
class SeleniumManagerTest extends TestCase
25+
{
26+
/**
27+
* we can mock selenium with built-in ChromeManager.
28+
*
29+
* @var ChromeManager
30+
*/
31+
protected $chromeMockManager;
32+
33+
public function setUp(): void
34+
{
35+
$this->chromeMockManager = new ChromeManager();
36+
$this->chromeMockManager->start();
37+
}
38+
39+
public function tearDown(): void
40+
{
41+
$this->chromeMockManager->quit();
42+
}
43+
44+
public function testRun()
45+
{
46+
$co = new ChromeOptions();
47+
$co->addArguments(['--headless', 'window-size=1200,1100', '--disable-gpu', '--no-sandbox']);
48+
$manager = new SeleniumManager('http://localhost:9515', $co->toCapabilities());
49+
$client = $manager->start();
50+
$this->assertNotEmpty($client->getCurrentURL());
51+
$manager->quit();
52+
}
53+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy