|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright 2025 Anthon Pang |
| 5 | + * @license Apache-2.0 |
| 6 | + * |
| 7 | + * @author Anthon Pang <apang@softwaredevelopment.ca> |
| 8 | + */ |
| 9 | + |
| 10 | +namespace WebDriver; |
| 11 | + |
| 12 | +/** |
| 13 | + * WebDriver\Actions class |
| 14 | + */ |
| 15 | +class Actions extends AbstractWebDriver |
| 16 | +{ |
| 17 | + /** |
| 18 | + * singleton |
| 19 | + * |
| 20 | + * @var \WebDriver\Actions |
| 21 | + */ |
| 22 | + private static $instance; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var array |
| 26 | + */ |
| 27 | + private $inputSources = [ |
| 28 | + NullInput::TYPE => [], |
| 29 | + KeyInput::TYPE => [], |
| 30 | + PointerInput::TYPE => [], |
| 31 | + WheelInput::TYPE => [], |
| 32 | + ]; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var array |
| 36 | + */ |
| 37 | + private $actions; |
| 38 | + |
| 39 | + /** |
| 40 | + * {@inheritdoc} |
| 41 | + */ |
| 42 | + private function __construct($url) |
| 43 | + { |
| 44 | + parent::__construct($url); |
| 45 | + |
| 46 | + $this->clearAllActions(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Get singleton instance |
| 51 | + * |
| 52 | + * @param string $url |
| 53 | + * |
| 54 | + * @return \WebDriver\Actions |
| 55 | + */ |
| 56 | + public static function getInstance($url) |
| 57 | + { |
| 58 | + if (self::$instance === null) { |
| 59 | + self::$instance = new self($url); |
| 60 | + } |
| 61 | + |
| 62 | + return self::$instance; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Get Null Input Source |
| 67 | + * |
| 68 | + * @return \WebDriver\NullInput |
| 69 | + */ |
| 70 | + public function getNullInput($id = 0) |
| 71 | + { |
| 72 | + if (! array_key_exists($id, $this->inputSources[NullInput::TYPE])) { |
| 73 | + $inputSource = new NullInput($id); |
| 74 | + |
| 75 | + $this->inputSources[NullInput::TYPE][$id] = $inputSource; |
| 76 | + } |
| 77 | + |
| 78 | + return $this->inputSources[NullInput::TYPE][$id]; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Get Key Input Source |
| 83 | + * |
| 84 | + * @return \WebDriver\KeyInput |
| 85 | + */ |
| 86 | + public function getKeyInput($id = 0) |
| 87 | + { |
| 88 | + if (! array_key_exists($id, $this->inputSources[KeyInput::TYPE])) { |
| 89 | + $inputSource = new KeyInput($id); |
| 90 | + |
| 91 | + $this->inputSources[KeyInput::TYPE][$id] = $inputSource; |
| 92 | + } |
| 93 | + |
| 94 | + return $this->inputSources[KeyInput::TYPE][$id]; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Get Pointer Input Source |
| 99 | + * |
| 100 | + * @return \WebDriver\PointerInput |
| 101 | + */ |
| 102 | + public function getPointerInput($id = 0, $subType = PointerInput::MOUSE) |
| 103 | + { |
| 104 | + if (! array_key_exists($id, $this->inputSources[PointerInput::TYPE])) { |
| 105 | + $inputSource = new PointerInput($id, $subType); |
| 106 | + |
| 107 | + $this->inputSources[PointerInput::TYPE][$id] = $inputSource; |
| 108 | + } |
| 109 | + |
| 110 | + return $this->inputSources[PointerInput::TYPE][$id]; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Get Wheel Input Source |
| 115 | + * |
| 116 | + * @return \WebDriver\WheelInput |
| 117 | + */ |
| 118 | + public function getWheelInput($id = 0) |
| 119 | + { |
| 120 | + if (! array_key_exists($id, $this->inputSources[WheelInput::TYPE])) { |
| 121 | + $inputSource = new WheelInput($id); |
| 122 | + |
| 123 | + $this->inputSources[WheelInput::TYPE][$id] = $inputSource; |
| 124 | + } |
| 125 | + |
| 126 | + return $this->inputSources[WheelInput::TYPE][$id]; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Perform actions: /session/:sessionId/actions (POST) |
| 131 | + * |
| 132 | + * @return mixed |
| 133 | + */ |
| 134 | + public function perform() |
| 135 | + { |
| 136 | + $actions = $this->actions; |
| 137 | + $parameters = ['actions' => $actions]; |
| 138 | + |
| 139 | + $this->clearAllActions(); |
| 140 | + |
| 141 | + $result = $this->curl('POST', '', $parameters); |
| 142 | + |
| 143 | + return $result['value']; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Release all action state: /session/:sessionId/actions (DELETE) |
| 148 | + * |
| 149 | + * @return mixed |
| 150 | + */ |
| 151 | + public function releaseActions() |
| 152 | + { |
| 153 | + $result = $this->curl('DELETE', ''); |
| 154 | + |
| 155 | + return $result['value']; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Clear all actions from the builder |
| 160 | + */ |
| 161 | + public function clearAllActions() |
| 162 | + { |
| 163 | + $this->actions = []; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Add action |
| 168 | + * |
| 169 | + * @param array $action |
| 170 | + * |
| 171 | + * @return \WebDriver\Actions |
| 172 | + */ |
| 173 | + public function addAction($action) |
| 174 | + { |
| 175 | + if (($last = count($this->actions)) && |
| 176 | + $this->actions[$last - 1]['id'] === $action['id'] && |
| 177 | + $this->actions[$last - 1]['type'] === $action['type'] |
| 178 | + ) { |
| 179 | + foreach ($action['actions'] as $item) { |
| 180 | + $this->actions[$last - 1]['actions'][] = $item; |
| 181 | + } |
| 182 | + } else { |
| 183 | + $this->actions[] = $action; |
| 184 | + } |
| 185 | + |
| 186 | + return $this; |
| 187 | + } |
| 188 | +} |
0 commit comments