xref: /plugin/combo/vendor/php-webdriver/webdriver/lib/Remote/RemoteTargetLocator.php (revision 04fd306c7c155fa133ebb3669986875d65988276)
1*04fd306cSNickeau<?php
2*04fd306cSNickeau
3*04fd306cSNickeaunamespace Facebook\WebDriver\Remote;
4*04fd306cSNickeau
5*04fd306cSNickeauuse Facebook\WebDriver\Exception\UnsupportedOperationException;
6*04fd306cSNickeauuse Facebook\WebDriver\WebDriverAlert;
7*04fd306cSNickeauuse Facebook\WebDriver\WebDriverElement;
8*04fd306cSNickeauuse Facebook\WebDriver\WebDriverTargetLocator;
9*04fd306cSNickeau
10*04fd306cSNickeau/**
11*04fd306cSNickeau * Used to locate a given frame or window for RemoteWebDriver.
12*04fd306cSNickeau */
13*04fd306cSNickeauclass RemoteTargetLocator implements WebDriverTargetLocator
14*04fd306cSNickeau{
15*04fd306cSNickeau    /** @var RemoteExecuteMethod */
16*04fd306cSNickeau    protected $executor;
17*04fd306cSNickeau    /** @var RemoteWebDriver */
18*04fd306cSNickeau    protected $driver;
19*04fd306cSNickeau    /** @var bool */
20*04fd306cSNickeau    protected $isW3cCompliant;
21*04fd306cSNickeau
22*04fd306cSNickeau    public function __construct(RemoteExecuteMethod $executor, RemoteWebDriver $driver, $isW3cCompliant = false)
23*04fd306cSNickeau    {
24*04fd306cSNickeau        $this->executor = $executor;
25*04fd306cSNickeau        $this->driver = $driver;
26*04fd306cSNickeau        $this->isW3cCompliant = $isW3cCompliant;
27*04fd306cSNickeau    }
28*04fd306cSNickeau
29*04fd306cSNickeau    /**
30*04fd306cSNickeau     * @return RemoteWebDriver
31*04fd306cSNickeau     */
32*04fd306cSNickeau    public function defaultContent()
33*04fd306cSNickeau    {
34*04fd306cSNickeau        $params = ['id' => null];
35*04fd306cSNickeau        $this->executor->execute(DriverCommand::SWITCH_TO_FRAME, $params);
36*04fd306cSNickeau
37*04fd306cSNickeau        return $this->driver;
38*04fd306cSNickeau    }
39*04fd306cSNickeau
40*04fd306cSNickeau    /**
41*04fd306cSNickeau     * @param WebDriverElement|null|int|string $frame The WebDriverElement, the id or the name of the frame.
42*04fd306cSNickeau     * When null, switch to the current top-level browsing context When int, switch to the WindowProxy identified
43*04fd306cSNickeau     * by the value. When an Element, switch to that Element.
44*04fd306cSNickeau     * @return RemoteWebDriver
45*04fd306cSNickeau     */
46*04fd306cSNickeau    public function frame($frame)
47*04fd306cSNickeau    {
48*04fd306cSNickeau        if ($this->isW3cCompliant) {
49*04fd306cSNickeau            if ($frame instanceof WebDriverElement) {
50*04fd306cSNickeau                $id = [JsonWireCompat::WEB_DRIVER_ELEMENT_IDENTIFIER => $frame->getID()];
51*04fd306cSNickeau            } elseif ($frame === null) {
52*04fd306cSNickeau                $id = null;
53*04fd306cSNickeau            } elseif (is_int($frame)) {
54*04fd306cSNickeau                $id = $frame;
55*04fd306cSNickeau            } else {
56*04fd306cSNickeau                throw new \InvalidArgumentException(
57*04fd306cSNickeau                    'In W3C compliance mode frame must be either instance of WebDriverElement, integer or null'
58*04fd306cSNickeau                );
59*04fd306cSNickeau            }
60*04fd306cSNickeau        } else {
61*04fd306cSNickeau            if ($frame instanceof WebDriverElement) {
62*04fd306cSNickeau                $id = ['ELEMENT' => $frame->getID()];
63*04fd306cSNickeau            } elseif ($frame === null) {
64*04fd306cSNickeau                $id = null;
65*04fd306cSNickeau            } elseif (is_int($frame)) {
66*04fd306cSNickeau                $id = $frame;
67*04fd306cSNickeau            } else {
68*04fd306cSNickeau                $id = (string) $frame;
69*04fd306cSNickeau            }
70*04fd306cSNickeau        }
71*04fd306cSNickeau
72*04fd306cSNickeau        $params = ['id' => $id];
73*04fd306cSNickeau        $this->executor->execute(DriverCommand::SWITCH_TO_FRAME, $params);
74*04fd306cSNickeau
75*04fd306cSNickeau        return $this->driver;
76*04fd306cSNickeau    }
77*04fd306cSNickeau
78*04fd306cSNickeau    /**
79*04fd306cSNickeau     * Switch to the parent iframe.
80*04fd306cSNickeau     *
81*04fd306cSNickeau     * @return RemoteWebDriver This driver focused on the parent frame
82*04fd306cSNickeau     */
83*04fd306cSNickeau    public function parent()
84*04fd306cSNickeau    {
85*04fd306cSNickeau        $this->executor->execute(DriverCommand::SWITCH_TO_PARENT_FRAME, []);
86*04fd306cSNickeau
87*04fd306cSNickeau        return $this->driver;
88*04fd306cSNickeau    }
89*04fd306cSNickeau
90*04fd306cSNickeau    /**
91*04fd306cSNickeau     * @param string $handle The handle of the window to be focused on.
92*04fd306cSNickeau     * @return RemoteWebDriver
93*04fd306cSNickeau     */
94*04fd306cSNickeau    public function window($handle)
95*04fd306cSNickeau    {
96*04fd306cSNickeau        if ($this->isW3cCompliant) {
97*04fd306cSNickeau            $params = ['handle' => (string) $handle];
98*04fd306cSNickeau        } else {
99*04fd306cSNickeau            $params = ['name' => (string) $handle];
100*04fd306cSNickeau        }
101*04fd306cSNickeau
102*04fd306cSNickeau        $this->executor->execute(DriverCommand::SWITCH_TO_WINDOW, $params);
103*04fd306cSNickeau
104*04fd306cSNickeau        return $this->driver;
105*04fd306cSNickeau    }
106*04fd306cSNickeau
107*04fd306cSNickeau    /**
108*04fd306cSNickeau     * Creates a new browser window and switches the focus for future commands of this driver to the new window.
109*04fd306cSNickeau     *
110*04fd306cSNickeau     * @see https://w3c.github.io/webdriver/#new-window
111*04fd306cSNickeau     * @param string $windowType The type of a new browser window that should be created. One of [tab, window].
112*04fd306cSNickeau     * The created window is not guaranteed to be of the requested type; if the driver does not support the requested
113*04fd306cSNickeau     * type, a new browser window will be created of whatever type the driver does support.
114*04fd306cSNickeau     * @throws UnsupportedOperationException
115*04fd306cSNickeau     * @return RemoteWebDriver This driver focused on the given window
116*04fd306cSNickeau     */
117*04fd306cSNickeau    public function newWindow($windowType = self::WINDOW_TYPE_TAB)
118*04fd306cSNickeau    {
119*04fd306cSNickeau        if ($windowType !== self::WINDOW_TYPE_TAB && $windowType !== self::WINDOW_TYPE_WINDOW) {
120*04fd306cSNickeau            throw new \InvalidArgumentException('Window type must by either "tab" or "window"');
121*04fd306cSNickeau        }
122*04fd306cSNickeau
123*04fd306cSNickeau        if (!$this->isW3cCompliant) {
124*04fd306cSNickeau            throw new UnsupportedOperationException('New window is only supported in W3C mode');
125*04fd306cSNickeau        }
126*04fd306cSNickeau
127*04fd306cSNickeau        $response = $this->executor->execute(DriverCommand::NEW_WINDOW, ['type' => $windowType]);
128*04fd306cSNickeau
129*04fd306cSNickeau        $this->window($response['handle']);
130*04fd306cSNickeau
131*04fd306cSNickeau        return $this->driver;
132*04fd306cSNickeau    }
133*04fd306cSNickeau
134*04fd306cSNickeau    public function alert()
135*04fd306cSNickeau    {
136*04fd306cSNickeau        return new WebDriverAlert($this->executor);
137*04fd306cSNickeau    }
138*04fd306cSNickeau
139*04fd306cSNickeau    /**
140*04fd306cSNickeau     * @return RemoteWebElement
141*04fd306cSNickeau     */
142*04fd306cSNickeau    public function activeElement()
143*04fd306cSNickeau    {
144*04fd306cSNickeau        $response = $this->driver->execute(DriverCommand::GET_ACTIVE_ELEMENT, []);
145*04fd306cSNickeau        $method = new RemoteExecuteMethod($this->driver);
146*04fd306cSNickeau
147*04fd306cSNickeau        return new RemoteWebElement($method, JsonWireCompat::getElement($response), $this->isW3cCompliant);
148*04fd306cSNickeau    }
149*04fd306cSNickeau}
150