maxhb
maxhb

Reputation: 8865

Codeception and Selenium: Can't set cookie

For one of my Selenium Cests I want to set a cookie but this is simply not working. In order to find the problem I reduced my code to the absolut minimum and to my surprise setting cookies seems not to work at all.

 /**
 * Test if we can set simple cookies
 *
 * @param \AcceptanceTester $i
 * @throws Exception
 */
public function settingCookieSetsCookie(AcceptanceTester $i)
{
    $cookieDefaultParams = [
        'path' => '/',
        'secure' => false,
        'httpOnly' => false,
        'expiry' => 900,
        'domain' => 'www.testdomain.local'
    ];

    $i->amOnPage('/cookieCest.php');
    $i->setCookie('example', 'myvalue', $cookieDefaultParams);
    $i->amOnPage('/cookieCest.php'); // reload page
    $cookieValue = $i->grabCookie('example');
    $i->assertEquals('myvalue', $cookieValue);
}

For testing purposes I set up a local test domain www.testdomain.local which works fine.

Unfortunately the above test fails with the following error:

There was 1 failure:


1) BackendLoginCest: Setting cookie sets cookie Test codeception\acceptance\CookieCest.php:settingCookieSetsCookie Step Assert equals "myvalue",null Fail Failed asserting that null matches expected 'myvalue'.

Scenario Steps:

  1. $I->assertEquals("myvalue",null) at codeception\acceptance\CookieCest.php:36
  2. $I->grabCookie("example") at codeception\acceptance\CookieCest.php:35
  3. $I->amOnPage("/cookieCest.php") at codeception\acceptance\CookieCest.php:34
  4. $I->setCookie("example","myvalue",{"path":"/","secure":false,"httpOnly":false,"expiry":900,"domain":"www.new-ep...}) at codeception\acceptance\CookieCest.php:33
  5. $I->amOnPage("/cookieCest.php") at codeception\acceptance\CookieCest.php:32

FAILURES! Tests: 1, Assertions: 1, Failures: 1.`

As far as I can see the cookie params configuration looks good (and seems to be needed, as leaving it out results in an error).

I am currently using selenium-server-standalone-3.141.59.jar but tried older versions which produce the same problem.

The referenced file cookieCest.php is just a simple script which var_dumps $_COOKIE so I can see that there are no cookie values available in PHP.

Does anyone experience a similar problem and knows how to handle it?

Upvotes: 0

Views: 2403

Answers (2)

Andrei Usov
Andrei Usov

Reputation: 1

Try this test with older Chrome version (e.g. 66). I think all cookie related stuff are broken with last several Chrome versions.

Upvotes: 0

pr1nc3
pr1nc3

Reputation: 8338

Your code should work. What may be a solution is that the page is not loaded when you want to set the cookie so it fails there.

Also if you want to check your cookie you can use the function seeCookie like:

Try this solution and let me know if it did any change.

$i->amOnPage('/cookieCest.php');
$i->wait(5); 
$i->setCookie('example', 'myvalue', $cookieDefaultParams);
$i->seeCookie('example');

Upvotes: 1

Related Questions