我一直对如何远程上传文件对硒的webdriver与PHP搜索周围的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上传的文件是/session/<sessionId>/file
通过在SauceLabs.com博客中检查出的原始日志( https://saucelabs.com/jobs/1a408cf60af0601f49052f66fa37812c/selenium- server.log中 ),因此这一说法,我创造了这个功能,加载到PHP-webdriver的-绑定库:
/**
* 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;
}
它添加到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小时寻找这个问题的答案。
更新:
我不得不改变这种由于收到此错误:
Expected there to be only 1 file. There were: 0
希望把这个在这里会得到谷歌的结果(我试图寻找谷歌和唯一的结果时,会发现是对谷歌代码源代码中引用的错误消息)。
为了解决这个问题,我是能够推导出您发送的文件需要实际拉上。 所以,我增强的源代码,使用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);
}
更新:原来,你需要设置在$ zip-> addFile()方法的两个参数。 编辑上面的代码以反映变化。