Search code examples
phpcodeception

How to attach a file using SubmitForm() in Codeception


I'm testing a form through the SubmitForm() function because the form uses javascript to cycle through each individual item.

example:

$I->submitForm('#form', array(
    'feet' => '1',
    'inches' => '2',

), 'submit');

This works fine but I'm having trouble with a file upload input.

$I->submitForm('#form', array(
    'feet' => '1',
    'inches' => '2',
    'file' => ???

 ), 'submit');

I tried sending an array to mimic the $_FILES array but that obviously isn't the right way to do it. Is this possible?


Solution

  • I realise this is old and already marked as answer but it doesn't answer the specific question, which is still a problem for @alexleonard, who posted the accepted answer.

    You can user attachFile in conjunction with submitForm. You just have to call is first. For example:

    $I->attachFile('#form input[type=file]', <pathtofile> );
    $I->submitForm('#form', array(
        'feet' => '1',
        'inches' => '2',
    ), 'submit');