使用PHP在Selenium WebDriver上远程上传文件 [英] Uploading files remotely on Selenium WebDriver using PHP

查看:864
本文介绍了使用PHP在Selenium WebDriver上远程上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索有关如何使用PHP远程上传Selenium WebDriver上的文件的StackOverflow(和其他资源)。我已阅读此 http ://saucelabs.com/blog/index.php/2012/03/selenium-tips-uploading-files-in-remote-webdriver/ ,它提到你需要使用setFileDetector方法改变你使用的WebDriver库的工作方式。



如果我使用Ruby或Java,这应该可以正常工作。另一方面,大多数PHP框架都没有这个方法。



有人可以告诉我如何在PHP中执行此操作吗?具体来说,我使用phpwebdriver库 http://code.google.com/p / php-webdriver-bindings /

解决方案

我能够确定上传文件的JsonWireProtocol通过检查SauceLabs.com博客文章( / session /< sessionId> / file /1a408cf60af0601f49052f66fa37812c/selenium-server.logrel =noreferrer> https://saucelabs.com/jobs/1a408cf60af0601f49052f66fa37812c/selenium-server.log )所以,我创建了这个函数来加载

  / ** 
*发送文件 php-webdriver-bindings 到您的远程WebDriver服务器
*这将返回您上传的文件的本地URL,然后
*让您使用文件输入元素中的sendKeys
* @params字符串$ value - a本地或远程文件发送
* @retur n String $ resopnseValue - 文件驻留在远程服务器上的本地目录
* /
public function sendFile($ value){
$ file = @file_get_contents($ value);

if($ file === false){
return false;
}

$ file = base64_encode($ file);
$ request = $ this-> requestURL。 /文件;
$ session = $ this-> curlInit($ request);
$ args = array('file'=> $ file);
$ postargs = json_encode($ args);
$ this-> preparePOST($ session,$ postargs);
$ response = trim(curl_exec($ session));

$ responseValue = $ this-> extractValueFromJsonResponse($ response);
返回$ responseValue;



$ b

添加到 WebDriver.php 文件中。



使用方法如下:

  .. 。
$ file_location = $ webdriver-> sendFile('http://test.com/some/file.zip');
$ file_input = $ webdriver-> findElementBy(LocatorStrategy :: id,'uploadfile');
$ file_input-> sendKeys(array($ file_location));

我希望这会帮助其他开发者,花费3个小时寻找答案。

更新:

由于发生这个错误,我不得不改变它:

 预计只有1个文件。这里有:0 

希望把这个放在这里会得到谷歌的结果(我试图寻找错误信息谷歌和它可以找到的唯一结果是在谷歌代码的源代码引用)。

为了解决这个问题,我能够推断出你发送的文件实际上需要压缩。所以我增加了源代码来使用PHP的 ZipArchive 库。我将保留旧的代码在记录保持上,但请使用这里的新代码:

pre $ {
$ zip = new ZipArchive();

$ filename_hash = sha1(time()。$ value);

$ zip_filename ={$ filename_hash} _zip.zip;
if($ zip-> open($ zip_filename,ZIPARCHIVE :: CREATE)=== false){
echo'WebDriver sendFile $ zip-> open failed \\\
';
返回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){
抛出新异常('WebDriver sendFile file_put_contents failed');


$ zip-> addFile($ filename,{$ filename_hash}。{$ file_extension});
$ zip-> 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-> requestURL。 /文件;
$ session = $ this-> curlInit($ request);
$ args = array('file'=> $ file);
$ postargs = json_encode($ args);
$ this-> preparePOST($ session,$ postargs);
$ response = trim(curl_exec($ session));

return $ this-> extractValueFromJsonResponse($ response);

更新:原来,您需要设置$ zip-> addFile()方法中的两个参数。编辑上述代码以反映更改。


I've been searching around StackOverflow (and other resources) on how to remotely upload files on Selenium WebDriver with PHP. I've read this http://saucelabs.com/blog/index.php/2012/03/selenium-tips-uploading-files-in-remote-webdriver/, and it mentions that you need to use a "setFileDetector" method somehow to change the way the WebDriver library you're using works.

This should work fine if I was using Ruby or Java. Most PHP frameworks on the other hand don't have this method.

Can anybody tell me how to do this in PHP? Specifically, I'm using the phpwebdriver library http://code.google.com/p/php-webdriver-bindings/

解决方案

I was able to determine that the JsonWireProtocol to upload a file would be /session/<sessionId>/file by checking out the raw log on the SauceLabs.com blog post (https://saucelabs.com/jobs/1a408cf60af0601f49052f66fa37812c/selenium-server.log) so with that, I created this function to add-in to the php-webdriver-bindings library:

/**
 * 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->requestURL . "/file";
    $session = $this->curlInit($request);
    $args = array( 'file' => $file );
    $postargs = json_encode($args);
    $this->preparePOST($session, $postargs);
    $response = trim(curl_exec($session));

    $responseValue = $this->extractValueFromJsonResponse($response);
    return $responseValue;
}

Add this to the WebDriver.php file.

To use, just do something like this:

...
$file_location = $webdriver->sendFile('http://test.com/some/file.zip');
$file_input = $webdriver->findElementBy(LocatorStrategy::id, 'uploadfile');
$file_input->sendKeys(array($file_location));

I hope this will help other developers, spent like 3 hours looking for the answer to this.

Update:

I had to change this due to getting this error:

Expected there to be only 1 file. There were: 0

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).

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 ZipArchive library. I will keep the old code on top for record-keeping, but please use the new code here:

public function sendFile($value, $file_extension = '')
{   
    $zip = new ZipArchive();

    $filename_hash = sha1(time().$value);

    $zip_filename = "{$filename_hash}_zip.zip";
    if( $zip->open($zip_filename, ZIPARCHIVE::CREATE) === false ) {
        echo 'WebDriver sendFile $zip->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->addFile($filename, "{$filename_hash}.{$file_extension}");
    $zip->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->requestURL . "/file";
    $session = $this->curlInit($request);
    $args = array( 'file' => $file );
    $postargs = json_encode($args);
    $this->preparePOST($session, $postargs);
    $response = trim(curl_exec($session));

    return $this->extractValueFromJsonResponse($response);
}

Update: Turns out, you need to set two parameters on the $zip->addFile() method. Edited the above code to reflect the changes.

这篇关于使用PHP在Selenium WebDriver上远程上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆