Search code examples
phpcookiesacceptance-testingcodeception

Removing cookie with Codeception


I just started using Codeception to write acceptance tests for my login application in PHP. What I need to do is to remove a cookie that has been set and I've been using resetCookie() to do that but it doesn't work.

Here's my code:

$I = new WebGuy($scenario);
$I->wantTo('ensure that login page is correct');
$I->amOnPage('/');
$I->click("Log in");
$I->see("cookie");
$I->resetCookie("test");
$I->reloadPage();
$I->dontSee("cookie");

So basically I set a cookie in my application when the Log in button is clicked and if that cookie is present I echo a string on the site that says "cookie". In the browser when the cookie is removed and the page is reloaded the "cookie" string disappears. This is what the test is trying to mimic.

For some reason, resetCookie() doesn't do anything because the test fails at the last step. Am I using it wrong or is there another way to do it? I would really appreciate your help with this!


Solution

  • Your test works 100%.

    Also try:

    $I->setCookie('test','cookie text');
    $I->seeCookie('test');
    $I->resetCookie('test');
    $I->seeCookie('test');
    

    If the above test passes, then something else might be blocking you from setting a cookie on the page you are testing.

    I tested using Webdriver. Here's my config file.

    class_name: WebGuy
    modules:
        enabled:
            - WebDriver
            - WebHelper
    
        config:
    
            WebDriver:
              url: http://www.example.com 
              host: localhost 
              port: 4444
              browser: firefox 
              delay: 1000