How to attach a file using attachFile() in Codeception

33 views Asked by At

I'm playing around with some PHP and Codeception acceptance tests and I want to upload a file in my acceptance test. I've done countless searching online, but I cannot get it working for me. Here below is my HTML form snippet where I have the file upload.

<label for="file">Upload File:</label>
<input type="file" id="file" name="file"><br> 

Here is how I handle it in process.php. Note that I only want a successful result if (among others) a file has been uploaded. process.php:

<?php
$password = $_POST['gen_pass'];
$car = $_POST['car'];

if(isset($_POST['refuse'])){
    $refuse_check = true;
}
else{
    $refuse_check = false;
}

if(isset($_POST['file'])){
    $file_check = true;
}
else{
    $file_check = false;
}

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['agree']) && $password == "password" && $car != "Subaru Vivio" && $file_check == true && $refuse_check == false){
    $firstname = $_POST["firstname"];
    $lastname = $_POST["lastname"];
    $age_group = $_POST['age'];
    $dob = $_POST['dob'];

    echo "Hello, " . $firstname . " " . $lastname . ". You are in this age group: " . $age_group . ". Born on " . $dob . ". You've selected the " . $car . ".";
    
}
elseif(is_null($_POST['agree'])){
    echo "Access denied! You need to agree";
}
elseif($password != "password"){
    echo "Access denied! Incorrect password";
}
elseif($car == "Subaru Vivio"){
    echo "Access denied! Wrong car selected";
}
elseif($file_check == false){
    echo "Access denied! No file added";
}
elseif($refuse_check == true){
    echo "Access denied! You need to refuse";
}
else{
    echo "Access denied! Something else went wrong.";
}
?>
<a href="return.php" id="return">Click</a>

Yes, I am aware that stuff like how I have set up the password makes no sense safety wise, but again, I'm just playing around with this to get familiar with how the tests work.

Finally, below here is my test code.

$I->amGoingTo("Fill in the form correctly, then return to form page");
        $I->amOnPage("/index.html");
        $I->seeInTitle("Register");
        $I->dontSeeInTitle("Log In");

        $I->seeNumberOfElements('form', 1);
        $I->seeNumberOfElements('input', 12);
        $I->seeNumberOfElements('label', 13);
        $I->seeNumberOfElements('option',3);
        $I->seeNumberOfElements('select', 1);

        $I->fillField("firstname", "Martin");
        $I->fillField("lastname", "Boersma");

        $I->fillField('input[name="dob"]', '2002-02-26');

        $I->seeInField('gen_pass','password');
        $password = $I->grabValueFrom('#gen_pass');
        $I->fillField('password', $password);

        $I->seeElement("#car");
        $I->selectOption('#car', 'Daihatsu Copen');
        $I->seeOptionIsSelected('#car', 'Daihatsu Copen');

        $I->seeElement('#young');
        $I->selectOption('age', '20-24');
        $I->seeOptionIsSelected('#young', '20-24');

        $I->attachFile('file', 'file.txt');

        $I->seeElement('#agree');
        $I->checkOption('#agree');
        $I->seeCheckboxIsChecked('#agree');

        $I->seeElement('#refuse');
        $I->seeCheckboxIsChecked('#refuse');
        $I->uncheckOption('#refuse');

        $I->click("Submit");
        
        $I->see("Hello, Martin Boersma. You are in this age group: 20-24. Born on 2002-02-26. You've selected the Daihatsu Copen.");
        $I->dontSee("Access denied!");

        $I->amOnPage("/process.php");

        $I->seeElement('#return');
        $I->seeLink('Click', 'return.php');
        $I->click("#return");

        $I->amOnPage("/return.php");
        $I->see("You're on the return page.");

So what does the test result in? I'm getting my error that I have set up in process.php, "Access denied! No file added.". So from that I can tell that the $I->attachFile('file', 'file.txt') line isn't working. The name of the HTML element that it's looking for, 'file', is also correct, otherwise the test error message would say that. Same goes for the file name 'file.txt', it exists, otherwise the test would tell me it doesn't.

I've read here: https://github.com/Codeception/Codeception/issues/5838#:~:text=Codeception%20attachFile%20doesn%27t%20do%20much that attachFile doesn't do much. I could perhaps be using it wrong, but i can't find how to otherwise properly upload a file to my form. Also when I test it in my browser, manually, it does work. I've tried echoing the $file_check variable and when I manually test it, it does get set to true, but with the acceptance test, it doesn't. Could this be a limitation of attachFile()? If so, what would you recommend me using to upload a file to this form?

0

There are 0 answers