I'm writing some acceptance tests in Codeception, and I want to check if a cookie exists. Normally you can do that with the seeCookie() method (e.g., $I->seeCookie( 'auth_logged_in' )
, but in my case, the name of the cookie includes a user-specific hash (e.g., auth_logged_in_49d4ab732056d505c2c751e2f7a5d842
).
It looks like the existing methods -- like WebGuy->seeCookie(), WebGuy->grabCookie(), etc -- all expect me to pass the exact name of the cookie, rather than being able to check for a partial name.
I looked at how the WebDriver->seeCookie()
method works, and it calls $this->webDriver->manage()->getCookies();
, so I tried adding a WebHelper method to create a WebGuy->seeCookieMatches()
method that uses a regex to check the cookie name, but it doesn't have a $this->webDriver
member like WebDriver
does.
ErrorException: Undefined property: Codeception\Module\WebHelper::$webDriver
Is there a way for the WebHelper
class to access the members of the WebDriver
class?
Or is there a better way to do this?
I figured out how to do this by using the getModule() method.
So now my WebHelper method looks like this:
public function seeCookieMatches( $pattern ) {
// Codeception 2.x
$cookies = $this->getModule( 'PhpBrowser' )->client->getCookieJar()->all();
// Codeception 1.x
$cookies = $this->getModule( 'PhpBrowser' )->session->getDriver()->getClient()->getCookieJar()->all();
// [...]
}