1<?php
2
3namespace Facebook\WebDriver\Firefox;
4
5use Facebook\WebDriver\Local\LocalWebDriver;
6use Facebook\WebDriver\Remote\DesiredCapabilities;
7use Facebook\WebDriver\Remote\Service\DriverCommandExecutor;
8use Facebook\WebDriver\Remote\WebDriverCommand;
9
10class FirefoxDriver extends LocalWebDriver
11{
12    const PROFILE = 'firefox_profile';
13
14    /**
15     * Creates a new FirefoxDriver using default configuration.
16     * This includes starting a new geckodriver process  each time this method is called. However this may be
17     * unnecessary overhead - instead, you can start the process once using FirefoxDriverService and pass
18     * this instance to startUsingDriverService() method.
19     *
20     * @return static
21     */
22    public static function start(DesiredCapabilities $capabilities = null)
23    {
24        $service = FirefoxDriverService::createDefaultService();
25
26        return static::startUsingDriverService($service, $capabilities);
27    }
28
29    /**
30     * Creates a new FirefoxDriver using given FirefoxDriverService.
31     * This is usable when you for example don't want to start new geckodriver process for each individual test
32     * and want to reuse the already started geckodriver, which will lower the overhead associated with spinning up
33     * a new process.
34     *
35     * @return static
36     */
37    public static function startUsingDriverService(
38        FirefoxDriverService $service,
39        DesiredCapabilities $capabilities = null
40    ) {
41        if ($capabilities === null) {
42            $capabilities = DesiredCapabilities::firefox();
43        }
44
45        $executor = new DriverCommandExecutor($service);
46        $newSessionCommand = WebDriverCommand::newSession(
47            [
48                'capabilities' => [
49                    'firstMatch' => [(object) $capabilities->toW3cCompatibleArray()],
50                ],
51            ]
52        );
53
54        $response = $executor->execute($newSessionCommand);
55
56        $returnedCapabilities = DesiredCapabilities::createFromW3cCapabilities($response->getValue()['capabilities']);
57        $sessionId = $response->getSessionID();
58
59        return new static($executor, $sessionId, $returnedCapabilities, true);
60    }
61}
62