Nik*_*kko 7 php file-upload selenium-webdriver
我一直在搜索StackOverflow(以及其他资源)如何使用PHP远程上传Selenium WebDriver上的文件.我已经阅读了这篇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是/session/<sessionId>/file通过查看SauceLabs.com博客文章(https://saucelabs.com/jobs/1a408cf60af0601f49052f66fa37812c/selenium-server.log)上的原始日志来实现的,所以,我创建了这个函数来加载到php-webdriver-bindings库:
/**
* 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;
}
Run Code Online (Sandbox Code Playgroud)
将其添加到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));
Run Code Online (Sandbox Code Playgroud)
我希望这会有助于其他开发者,花3个小时寻找答案.
更新:
由于收到此错误,我不得不更改此设置:
Expected there to be only 1 file. There were: 0
Run Code Online (Sandbox Code Playgroud)
希望将其放在此处可以获得Google搜索结果(我尝试在Google上搜索错误消息,并且可以找到的唯一结果是Google代码上的源代码参考).
为了解决这个问题,我能够推断出你发送的文件实际上需要压缩.所以我扩充了源代码以使用PHP的ZipArchive库.我会保留旧代码以保存记录,但请在此处使用新代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
更新:结果,你需要在$ zip-> addFile()方法上设置两个参数.编辑上面的代码以反映更改.
| 归档时间: |
|
| 查看次数: |
4661 次 |
| 最近记录: |