From 04732a5817c739b9e1f1efbd556cbf9ddc8e3db5 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Wed, 24 Jul 2024 12:08:40 +0100 Subject: [PATCH 1/3] Fix file uploads on new selenium with firefox --- lib/WebDriver/Session.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/WebDriver/Session.php b/lib/WebDriver/Session.php index 4ab60a5..59d383a 100644 --- a/lib/WebDriver/Session.php +++ b/lib/WebDriver/Session.php @@ -559,6 +559,33 @@ protected function getIdentifierPath($identifier) return sprintf('%s/element/%s', $this->url, $identifier); } + /** + * Upload a file: /session/:sessionId/se/file (POST) + * : /session/:sessionId/file (POST) + * + * @param array $arguments + * + * @return mixed + */ + public function file($arguments) + { + // Since Selenium 4.17 the file URL has been prefixed with /se because + // it is not a W3C command. + if ($this->isW3C()) { + try { + $result = $this->curl('POST', '/se/file', $arguments); + } catch (Exception $e) { + } + } + + // Fallback to pre Selenium 4.17 behaviour and non W3C behaviour. + if (!isset($result)) { + $result = $this->curl('POST', '/file', $arguments); + } + + return $result['value']; + } + /** * {@inheritdoc} */ From cfd933d192bde1c551b50f0022500e8d0f44b657 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Wed, 24 Jul 2024 16:37:28 +0100 Subject: [PATCH 2/3] Add docs and fix docs --- lib/WebDriver/Session.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/WebDriver/Session.php b/lib/WebDriver/Session.php index 59d383a..ffda310 100644 --- a/lib/WebDriver/Session.php +++ b/lib/WebDriver/Session.php @@ -33,7 +33,6 @@ * @method void doubleclick() Double-clicks at the current mouse coordinates (set by moveto). * @method array execute_sql($jsonQuery) Execute SQL. * @method array execute_async($jsonScript) Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. - * @method string file($jsonFile) Upload file. * @method void forward() Navigates forward in the browser history, if possible. * @method void keys($jsonKeys) Send a sequence of key strokes to the active element. * @method array getLocation() Get the current geo location. @@ -570,7 +569,9 @@ protected function getIdentifierPath($identifier) public function file($arguments) { // Since Selenium 4.17 the file URL has been prefixed with /se because - // it is not a W3C command. + // it is not a W3C command. See + // https://github.com/w3c/webdriver/issues/1355 for discussions about + // the W3C spec and file uploads. if ($this->isW3C()) { try { $result = $this->curl('POST', '/se/file', $arguments); From f1f1487361d6630cedf9e331e6f0b9244ce6955e Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Wed, 24 Jul 2024 17:48:44 +0100 Subject: [PATCH 3/3] Better docs --- lib/WebDriver/Session.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/WebDriver/Session.php b/lib/WebDriver/Session.php index ffda310..7ea3aaf 100644 --- a/lib/WebDriver/Session.php +++ b/lib/WebDriver/Session.php @@ -563,10 +563,12 @@ protected function getIdentifierPath($identifier) * : /session/:sessionId/file (POST) * * @param array $arguments + * An array with a single key/value. The key should be 'file' and the + * value should be a string containing base64 encoded contents of a file. * * @return mixed */ - public function file($arguments) + public function file(array $arguments) { // Since Selenium 4.17 the file URL has been prefixed with /se because // it is not a W3C command. See