Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was able to determine that the JsonWireProtocol to upload a file would be <code>/session/&lt;sessionId&gt;/file</code> by checking out the raw log on the SauceLabs.com blog post (<a href="https://saucelabs.com/jobs/1a408cf60af0601f49052f66fa37812c/selenium-server.log" rel="noreferrer">https://saucelabs.com/jobs/1a408cf60af0601f49052f66fa37812c/selenium-server.log</a>) so with that, I created this function to add-in to the <strong>php-webdriver-bindings</strong> library:</p> <pre><code>/** * Send a file to your Remote WebDriver server * This will return the local URL of the file you uploaded, which will then * let you use sendKeys in file input elements * @params String $value - a local or remote file to send * @return String $resopnseValue - the local directory where the file resides on the remote server */ public function sendFile($value) { $file = @file_get_contents($value); if( $file === false ) { return false; } $file = base64_encode($file); $request = $this-&gt;requestURL . "/file"; $session = $this-&gt;curlInit($request); $args = array( 'file' =&gt; $file ); $postargs = json_encode($args); $this-&gt;preparePOST($session, $postargs); $response = trim(curl_exec($session)); $responseValue = $this-&gt;extractValueFromJsonResponse($response); return $responseValue; } </code></pre> <p>Add this to the <strong>WebDriver.php</strong> file.</p> <p>To use, just do something like this:</p> <pre><code>... $file_location = $webdriver-&gt;sendFile('http://test.com/some/file.zip'); $file_input = $webdriver-&gt;findElementBy(LocatorStrategy::id, 'uploadfile'); $file_input-&gt;sendKeys(array($file_location)); </code></pre> <p>I hope this will help other developers, spent like 3 hours looking for the answer to this.</p> <p>Update:</p> <p>I had to change this due to getting this error:</p> <pre><code>Expected there to be only 1 file. There were: 0 </code></pre> <p>Hopefully putting this here would get Google results (I tried searching for the error message on Google and the only results it could find were the references to the source code on Google Code).</p> <p>To solve this problem, I was able to deduce that the file you send actually needs to be zipped. So I've augmented the source code to use PHP's <strong>ZipArchive</strong> library. I will keep the old code on top for record-keeping, but please use the new code here:</p> <pre><code>public function sendFile($value, $file_extension = '') { $zip = new ZipArchive(); $filename_hash = sha1(time().$value); $zip_filename = "{$filename_hash}_zip.zip"; if( $zip-&gt;open($zip_filename, ZIPARCHIVE::CREATE) === false ) { echo 'WebDriver sendFile $zip-&gt;open failed\n'; return false; } $file_data = @file_get_contents($value); if( $file_data === false ) { throw new Exception('WebDriver sendFile file_get_contents failed'); } $filename = "{$filename_hash}.{$file_extension}"; if( @file_put_contents($filename, $file_data) === false ) { throw new Exception('WebDriver sendFile file_put_contents failed'); } $zip-&gt;addFile($filename, "{$filename_hash}.{$file_extension}"); $zip-&gt;close(); $zip_file = @file_get_contents($zip_filename); if( $zip_file === false ) { throw new Exception('WebDriver sendFile file_get_contents for $zip_file failed'); } $file = base64_encode($zip_file); $request = $this-&gt;requestURL . "/file"; $session = $this-&gt;curlInit($request); $args = array( 'file' =&gt; $file ); $postargs = json_encode($args); $this-&gt;preparePOST($session, $postargs); $response = trim(curl_exec($session)); return $this-&gt;extractValueFromJsonResponse($response); } </code></pre> <p><strong>Update:</strong> Turns out, you need to set two parameters on the $zip->addFile() method. Edited the above code to reflect the changes.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload