@@ -128,6 +128,95 @@ public function getTransientOptions()
128128 return $ this ->transientOptions ;
129129 }
130130
131+ /**
132+ * Serialize script arguments (containing web elements and/or shadow roots)
133+ *
134+ * @see https://w3c.github.io/webdriver/#executing-script
135+ *
136+ * @param array $arguments
137+ *
138+ * @return array
139+ */
140+ protected function serializeArguments (array $ arguments )
141+ {
142+ foreach ($ arguments as $ key => $ value ) {
143+ if ($ value instanceof LegacyElement) {
144+ $ arguments [$ key ] = [LegacyElement::LEGACY_ELEMENT_ID => $ value ->getID ()];
145+ } elseif ($ value instanceof Element) {
146+ $ arguments [$ key ] = [Element::WEB_ELEMENT_ID => $ value ->getID ()];
147+ } elseif ($ value instanceof Shadow) {
148+ $ arguments [$ key ] = [Shadow::SHADOW_ROOT_ID => $ value ->getID ()];
149+ } elseif (is_array ($ value )) {
150+ $ arguments [$ key ] = $ this ->serializeArguments ($ value );
151+ }
152+ }
153+
154+ return $ arguments ;
155+ }
156+
157+ /**
158+ * Unserialize result (containing web elements and/or shadow roots)
159+ *
160+ * @param mixed $result
161+ *
162+ * @return mixed
163+ */
164+ protected function unserializeResult ($ result )
165+ {
166+ $ element = is_array ($ result ) ? $ this ->makeElement ($ result ) : null ;
167+
168+ if ($ element !== null ) {
169+ return $ element ;
170+ }
171+
172+ if (is_array ($ result )) {
173+ foreach ($ result as $ key => $ value ) {
174+ $ result [$ key ] = $ this ->unserializeResult ($ value );
175+ }
176+ }
177+
178+ return $ result ;
179+ }
180+
181+ /**
182+ * Factory method for elements
183+ *
184+ * @param array $value
185+ *
186+ * @return \WebDriver\Element|\WebDriver\Shadow|null
187+ */
188+ protected function makeElement ($ value )
189+ {
190+ if (array_key_exists (LegacyElement::LEGACY_ELEMENT_ID , $ value )) {
191+ $ identifier = $ value [LegacyElement::LEGACY_ELEMENT_ID ];
192+
193+ return new LegacyElement (
194+ $ this ->getIdentifierPath ('/element/ ' . $ identifier ),
195+ $ identifier
196+ );
197+ }
198+
199+ if (array_key_exists (Element::WEB_ELEMENT_ID , $ value )) {
200+ $ identifier = $ value [Element::WEB_ELEMENT_ID ];
201+
202+ return new Element (
203+ $ this ->getIdentifierPath ('/element/ ' . $ identifier ),
204+ $ identifier
205+ );
206+ }
207+
208+ if (array_key_exists (Shadow::SHADOW_ROOT_ID , $ value )) {
209+ $ identifier = $ value [Shadow::SHADOW_ROOT_ID ];
210+
211+ return new Shadow (
212+ $ this ->getIdentifierPath ('/shadow/ ' . $ identifier ),
213+ $ identifier
214+ );
215+ }
216+
217+ return null ;
218+ }
219+
131220 /**
132221 * Curl request to webdriver server.
133222 *
0 commit comments