Skip to content

Instantly share code, notes, and snippets.

@nickrealdini
Last active September 9, 2016 09:38
Show Gist options
  • Save nickrealdini/c8d0dd301680c2305e41e05ffedcd758 to your computer and use it in GitHub Desktop.
Save nickrealdini/c8d0dd301680c2305e41e05ffedcd758 to your computer and use it in GitHub Desktop.
Selenium attachFile yields empty file handle?
default:
extensions:
SensioLabs\Behat\PageObjectExtension: ~
Laracasts\Behat:
env_path: .env.behat
Behat\MinkExtension:
default_session: laravel
sessions:
default:
laravel: ~
base_url: 'http://localhost'
laravel: ~
files_path: /src/features/assets
emuse\BehatHTMLFormatter\BehatHTMLFormatterExtension:
name: html
renderer: Twig,Behat2
file_name: index
print_args: true
print_outp: true
loop_break: true
formatters:
pretty:
verbose: true
paths: false
snippets: true
smoke:
extensions:
Behat\MinkExtension:
base_url: http://localhost
browser_name: "chrome"
goutte: ~
default_session: selenium2
javascript_session: selenium2
selenium2:
browser: 'chrome'
wd_host: "http://192.168.98.180:4444/wd/hub" # fixed IP for standalone selenium
capabilities: { "browser": "chrome"}
suites:
smoke:
contexts:
- FeatureContext
- UploaderContext
filters:
tags: "@smoke"

When I attach a file manually my file has a size value, but if I run the behat test the file size is zero.

Manual Upload:

FileList {0: File, length: 1}
	0: File
		lastModified: 1472812418000
		lastModifiedDate: Fri Sep 02 2016 11:33:38 GMT+0100 (BST)
		name: "wrongCoverArt.jpg"
		size: 2903
		type: "image/jpeg"
		webkitRelativePath:""
	__proto__: File
	length: 1
	__proto__: FileList

Automatic Upload:

FileList {0: File, length: 1}
	0: File
		lastModified: 1472812418000
		lastModifiedDate: Fri Sep 02 2016 11:35:21 GMT+0100 (BST)
		name: "wrongCoverArt.jpg"
		size: 0
		type: "image/jpeg"
		webkitRelativePath:""
	__proto__: File
	length: 1
	__proto__: FileList
@smoke
Feature: test
Scenario:
Given I am on "upload.php"
When I add cover art "wrongCoverArt.jpg"
<?php
namespace Page;
use SensioLabs\Behat\PageObjectExtension\PageObject\Page;
class Uploader extends Page
{
/**
* @param string $filePath
*/
public function addCoverArt($filePath)
{
$uploadElement = $this->find('css', '#cover-art-uploader');
$uploadElement->attachFile($filePath);
// $this->waitFor(10000, function(){});
}
}
/**
* @When I add cover art :arg1
*
*/
public function iAddCoverArt($arg1)
{
$filePath = $this->getMinkParameter('files_path') . DIRECTORY_SEPARATOR . trim($arg1);
$this->uploader->addCoverArt($filePath);
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<div>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file" id="cover-art-uploader"/><br/>
</form>
</div>
<script type="text/javascript">
$(function() {
$('#cover-art-uploader').on('change', function(e) {
console.log($(this).get(0).files)
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment