Skip to content

Commit

Permalink
chore(tests): refactor acceptance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaseberle committed Mar 6, 2024
1 parent adf188b commit 6bb021e
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 143 deletions.
59 changes: 59 additions & 0 deletions Tests/Acceptance/Frontend/JavaScriptApiCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package dmind/cookieman.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace Dmind\Cookieman\Tests\Acceptance\Frontend;

use Codeception\Exception\ModuleException;
use Dmind\Cookieman\Tests\Acceptance\Support\AcceptanceTester;
use Dmind\Cookieman\Tests\Acceptance\Support\Constants;
use Exception;

/**
* Tests clicking through some frontend pages
*/
class JavaScriptApiCest
{
/**
* @param AcceptanceTester $I
* @throws ModuleException
* @throws Exception
*/
public function onScriptLoadedEventHandler(AcceptanceTester $I)
{
$I->amOnPage(Constants::PATH_root);
$I->setCookie(
Constants::COOKIENAME,
$this->cookieValueForGroups(
[Constants::GROUP_keyMandatory, Constants::GROUP_keyTestgroup]
)
);
$I->reloadPage();

// test onScriptLoaded() (once as a callback and once when already loaded)
foreach ([0, 1] as $iScript) {
$onScriptLoadedArgs = [Constants::TRACKINGOBJECT_inTestgroupWith2Scripts, $iScript];
$I->executeJS(
Constants::JS_onScriptLoaded,
$onScriptLoadedArgs
);
$I->waitForText($onScriptLoadedArgs[0] . ':' . $onScriptLoadedArgs[1] . ' loaded');
}
}

/**
* @param array $groupKeys
* @return string
*/
protected function cookieValueForGroups(array $groupKeys)
{
return implode(Constants::COOKIE_separator, $groupKeys);
}
}
39 changes: 18 additions & 21 deletions Tests/Acceptance/Frontend/OpenPopupCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,38 @@

namespace Dmind\Cookieman\Tests\Acceptance\Frontend;

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

use Codeception\Util\Locator;
use Dmind\Cookieman\Tests\Acceptance\Support\AcceptanceTester;
use Dmind\Cookieman\Tests\Acceptance\Support\Constants;
use Exception;

/**
* Tests opening the popup
*/
class OpenPopupCest
{
public const PATH_imprint = '/imprint';
public const SELECTOR_modal = '#cookieman-modal';
public const SELECTOR_btnDataCookiemanShow = '[data-cookieman-show]';

/**
* @param AcceptanceTester $I
* @throws Exception
*/
public function openViaClickOnDataCookiemanShowElement(AcceptanceTester $I)
{
$I->amOnPage(self::PATH_imprint);
$I->amOnPage(Constants::PATH_imprint);
$I->waitForJS('return typeof cookieman === "object"', 10);
$I->clickWithLeftButton(['css' => Constants::SELECTOR_btnDataCookiemanShow]);
$I->waitForElementVisible(Constants::SELECTOR_modal);
}

/**
* @param AcceptanceTester $I
* @throws Exception
*/
public function notShownOnImprint(AcceptanceTester $I)
{
$I->amOnPage(Constants::PATH_imprint);
$I->waitForJS('return typeof cookieman === "object"', 10);
$I->clickWithLeftButton(['css' => self::SELECTOR_btnDataCookiemanShow]);
$I->waitForElementVisible(self::SELECTOR_modal);
$I->wait(Constants::WAITFOR_timeout);
$I->dontSeeElement(Constants::SELECTOR_modal);
$I->executeJS(Constants::JS_showCookieman);
$I->waitForElementVisible(Constants::SELECTOR_modal, Constants::WAITFOR_timeout);
}
}
158 changes: 49 additions & 109 deletions Tests/Acceptance/Frontend/PopupInteractionsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,78 +11,32 @@

namespace Dmind\Cookieman\Tests\Acceptance\Frontend;

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

use Codeception\Util\Locator;
use Dmind\Cookieman\Tests\Acceptance\Support\AcceptanceTester;
use Dmind\Cookieman\Tests\Acceptance\Support\Constants;
use Exception;

/**
* Tests clicking through some frontend pages
*/
class PopupInteractionsCest
{
public const PATH_root = '/';
public const PATH_imprint = '/imprint';

public const SELECTOR_modal = '#cookieman-modal';
public const SELECTOR_btnSaveNotSaveAll = '[data-cookieman-save]:not([data-cookieman-accept-all]):not([data-cookieman-accept-none])';
public const SELECTOR_btnSaveNone = '[data-cookieman-save][data-cookieman-accept-none]';
public const SELECTOR_btnSaveAll = '[data-cookieman-save][data-cookieman-accept-all]';
public const LOCATOR_settings = ['xpath' => '//*[self::button or self::a][contains(., "Settings")]'];

public const COOKIENAME = 'CookieConsent';
public const COOKIE_separator = '|';

public const JS_showCookieman = 'cookieman.show()';
public const JS_onScriptLoaded = "
cookieman.onScriptLoaded(
arguments[0],
arguments[1],
function (trackingObjectKey, scriptId) {
document.body.append(arguments[0] + ':' + arguments[1] + ' loaded; ')
}
);
";

public const GROUP_keyMandatory = 'mandatory';

public const GROUP_key2nd = 'marketing';
public const GROUP_title2nd = 'Marketing';
public const COOKIE_titleIn2ndGroup = '_gat';

public const GROUP_keyTestgroup = 'testgroup';
public const TRACKINGOBJECT_inTestgroupWith2Scripts = 'Crowdin';

public const WAITFOR_timeout = 5;

/**
* @param AcceptanceTester $I
* @throws Exception
*/
public function save(AcceptanceTester $I)
{
$I->amOnPage(self::PATH_root);
$I->amOnPage(Constants::PATH_root);
$I->waitForJS('return typeof cookieman === "object"', 10);
$I->waitForElementVisible(self::SELECTOR_modal);
$I->waitForElementClickable(self::SELECTOR_btnSaveNone);
$I->clickWithLeftButton(['css' => self::SELECTOR_btnSaveNone]);
$I->waitForElementNotVisible(self::SELECTOR_modal);
$I->seeCookie(self::COOKIENAME);
$I->waitForElementVisible(Constants::SELECTOR_modal);
$I->waitForElementClickable(Constants::SELECTOR_btnSaveNone);
$I->clickWithLeftButton(['css' => Constants::SELECTOR_btnSaveNone]);
$I->waitForElementNotVisible(Constants::SELECTOR_modal);
$I->seeCookie(Constants::COOKIENAME);
$I->assertEquals(
self::GROUP_keyMandatory,
$I->grabCookie(self::COOKIENAME, ['path' => self::PATH_root])
Constants::GROUP_keyMandatory,
$I->grabCookie(Constants::COOKIENAME, ['path' => Constants::PATH_root])
);
}

Expand All @@ -92,34 +46,29 @@ public function save(AcceptanceTester $I)
*/
public function saveAll(AcceptanceTester $I)
{
$I->amOnPage(self::PATH_root);
$I->amOnPage(Constants::PATH_root);
$I->waitForJS('return typeof cookieman === "object"', 10);
$I->waitForElementVisible(self::SELECTOR_modal);
$I->waitForElementClickable(self::LOCATOR_settings);
$I->clickWithLeftButton(self::LOCATOR_settings);
$I->scrollIntoView(self::SELECTOR_btnSaveAll);
$I->waitForElementClickable(self::SELECTOR_btnSaveAll);
$I->clickWithLeftButton(['css' => self::SELECTOR_btnSaveAll]);
$I->waitForElementNotVisible(self::SELECTOR_modal);
$I->seeCookie(self::COOKIENAME);
$I->waitForElementVisible(Constants::SELECTOR_modal);
$I->waitForElementClickable(Constants::LOCATOR_settings);
$I->clickWithLeftButton(Constants::LOCATOR_settings);
$I->scrollIntoView(Constants::SELECTOR_btnSaveAll);
$I->waitForElementClickable(Constants::SELECTOR_btnSaveAll);
$I->clickWithLeftButton(['css' => Constants::SELECTOR_btnSaveAll]);
$I->waitForElementNotVisible(Constants::SELECTOR_modal);
$I->seeCookie(Constants::COOKIENAME);
$I->assertStringStartsWith(
$this->cookieValueForGroups([self::GROUP_keyMandatory, self::GROUP_key2nd]),
$I->grabCookie(self::COOKIENAME, ['path' => self::PATH_root])
$this->cookieValueForGroups([Constants::GROUP_keyMandatory, Constants::GROUP_key2nd]),
$I->grabCookie(Constants::COOKIENAME, ['path' => Constants::PATH_root])
);
}

/**
* @param AcceptanceTester $I
* @throws Exception
* @param array $groupKeys
* @return string
*/
public function notShownOnImprint(AcceptanceTester $I)
protected function cookieValueForGroups(array $groupKeys)
{
$I->amOnPage(self::PATH_imprint);
$I->waitForJS('return typeof cookieman === "object"', 10);
$I->wait(self::WAITFOR_timeout);
$I->dontSeeElement(self::SELECTOR_modal);
$I->executeJS(self::JS_showCookieman);
$I->waitForElementVisible(self::SELECTOR_modal, self::WAITFOR_timeout);
return implode(Constants::COOKIE_separator, $groupKeys);
}

/**
Expand All @@ -130,31 +79,31 @@ public function notShownOnImprint(AcceptanceTester $I)
public function selectGroupAndSaveMobile(AcceptanceTester $I)
{
$I->resizeWindow(480, 800);
$I->amOnPage(self::PATH_root);
$I->amOnPage(Constants::PATH_root);
$I->waitForJS('return typeof cookieman === "object"', 10);
$I->waitForElementVisible(self::SELECTOR_modal, self::WAITFOR_timeout);
$I->waitForElementClickable(self::LOCATOR_settings);
$I->clickWithLeftButton(self::LOCATOR_settings);
$I->waitForElementVisible(Locator::contains('*', self::GROUP_title2nd), self::WAITFOR_timeout);
$I->waitForElementClickable(Locator::contains('*', self::GROUP_title2nd));
$I->clickWithLeftButton(Locator::contains('*', self::GROUP_title2nd));
$I->waitForElementVisible(Constants::SELECTOR_modal, Constants::WAITFOR_timeout);
$I->waitForElementClickable(Constants::LOCATOR_settings);
$I->clickWithLeftButton(Constants::LOCATOR_settings);
$I->waitForElementVisible(Locator::contains('*', Constants::GROUP_title2nd), Constants::WAITFOR_timeout);
$I->waitForElementClickable(Locator::contains('*', Constants::GROUP_title2nd));
$I->clickWithLeftButton(Locator::contains('*', Constants::GROUP_title2nd));
$I->waitForElementVisible(
Locator::contains('*', self::COOKIE_titleIn2ndGroup),
self::WAITFOR_timeout
Locator::contains('*', Constants::COOKIE_titleIn2ndGroup),
Constants::WAITFOR_timeout
); // a single row in the table
$I->scrollIntoView('[name=' . self::GROUP_key2nd . ']');
if (!$I->tryToCheckOption('[name=' . self::GROUP_key2nd . ']')) { // theme: *-modal
$I->executeJS('$("[name=' . self::GROUP_key2nd . ']").click()'); // theme: bootstrap3-banner
$I->scrollIntoView('[name=' . Constants::GROUP_key2nd . ']');
if (!$I->tryToCheckOption('[name=' . Constants::GROUP_key2nd . ']')) { // theme: *-modal
$I->executeJS('$("[name=' . Constants::GROUP_key2nd . ']").click()'); // theme: bootstrap3-banner
}
$I->seeCheckboxIsChecked('[name=' . self::GROUP_key2nd . ']');
$I->scrollIntoView(self::SELECTOR_btnSaveNotSaveAll);
$I->waitForElementClickable(self::SELECTOR_btnSaveNotSaveAll);
$I->clickWithLeftButton(['css' => self::SELECTOR_btnSaveNotSaveAll]);
$I->waitForElementNotVisible(self::SELECTOR_modal);
$I->seeCookie(self::COOKIENAME);
$I->seeCheckboxIsChecked('[name=' . Constants::GROUP_key2nd . ']');
$I->scrollIntoView(Constants::SELECTOR_btnSaveNotSaveAll);
$I->waitForElementClickable(Constants::SELECTOR_btnSaveNotSaveAll);
$I->clickWithLeftButton(['css' => Constants::SELECTOR_btnSaveNotSaveAll]);
$I->waitForElementNotVisible(Constants::SELECTOR_modal);
$I->seeCookie(Constants::COOKIENAME);
$I->assertEquals(
$this->cookieValueForGroups([self::GROUP_keyMandatory, self::GROUP_key2nd]),
$I->grabCookie(self::COOKIENAME, ['path' => self::PATH_root])
$this->cookieValueForGroups([Constants::GROUP_keyMandatory, Constants::GROUP_key2nd]),
$I->grabCookie(Constants::COOKIENAME, ['path' => Constants::PATH_root])
);
}

Expand All @@ -165,32 +114,23 @@ public function selectGroupAndSaveMobile(AcceptanceTester $I)
*/
public function onScriptLoadedEventHandler(AcceptanceTester $I)
{
$I->amOnPage(self::PATH_root);
$I->amOnPage(Constants::PATH_root);
$I->setCookie(
self::COOKIENAME,
Constants::COOKIENAME,
$this->cookieValueForGroups(
[self::GROUP_keyMandatory, self::GROUP_keyTestgroup]
[Constants::GROUP_keyMandatory, Constants::GROUP_keyTestgroup]
)
);
$I->reloadPage();

// test onScriptLoaded() (once as a callback and once when already loaded)
foreach ([0, 1] as $iScript) {
$onScriptLoadedArgs = [self::TRACKINGOBJECT_inTestgroupWith2Scripts, $iScript];
$onScriptLoadedArgs = [Constants::TRACKINGOBJECT_inTestgroupWith2Scripts, $iScript];
$I->executeJS(
self::JS_onScriptLoaded,
Constants::JS_onScriptLoaded,
$onScriptLoadedArgs
);
$I->waitForText($onScriptLoadedArgs[0] . ':' . $onScriptLoadedArgs[1] . ' loaded');
}
}

/**
* @param array $groupKeys
* @return string
*/
protected function cookieValueForGroups(array $groupKeys)
{
return implode(self::COOKIE_separator, $groupKeys);
}
}
13 changes: 0 additions & 13 deletions Tests/Acceptance/Support/AcceptanceTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,6 @@

namespace Dmind\Cookieman\Tests\Acceptance\Support;

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

use Dmind\Cookieman\Tests\Acceptance\Support\_generated\AcceptanceTesterActions;

/**
Expand Down
Loading

0 comments on commit 6bb021e

Please sign in to comment.