dshmvqnl98119 2019-03-08 14:31
浏览 176

Codeception和Selenium:无法设置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?

  • 写回答

2条回答 默认 最新

  • dps43633 2019-03-08 14:39
    关注

    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');
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度