From 6bb021e3da1e2806d6c35cdd83d36f712ae3bd50 Mon Sep 17 00:00:00 2001 From: Jonas Eberle Date: Wed, 6 Mar 2024 19:10:57 +0100 Subject: [PATCH] chore(tests): refactor acceptance tests --- .../Acceptance/Frontend/JavaScriptApiCest.php | 59 +++++++ Tests/Acceptance/Frontend/OpenPopupCest.php | 39 ++--- .../Frontend/PopupInteractionsCest.php | 158 ++++++------------ Tests/Acceptance/Support/AcceptanceTester.php | 13 -- Tests/Acceptance/Support/Constants.php | 50 ++++++ 5 files changed, 176 insertions(+), 143 deletions(-) create mode 100644 Tests/Acceptance/Frontend/JavaScriptApiCest.php create mode 100644 Tests/Acceptance/Support/Constants.php diff --git a/Tests/Acceptance/Frontend/JavaScriptApiCest.php b/Tests/Acceptance/Frontend/JavaScriptApiCest.php new file mode 100644 index 00000000..f930e9ef --- /dev/null +++ b/Tests/Acceptance/Frontend/JavaScriptApiCest.php @@ -0,0 +1,59 @@ +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); + } +} diff --git a/Tests/Acceptance/Frontend/OpenPopupCest.php b/Tests/Acceptance/Frontend/OpenPopupCest.php index 0adee66e..ad4020db 100644 --- a/Tests/Acceptance/Frontend/OpenPopupCest.php +++ b/Tests/Acceptance/Frontend/OpenPopupCest.php @@ -11,21 +11,8 @@ 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; /** @@ -33,19 +20,29 @@ */ 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); } } diff --git a/Tests/Acceptance/Frontend/PopupInteractionsCest.php b/Tests/Acceptance/Frontend/PopupInteractionsCest.php index 92d05139..98ea6d7a 100644 --- a/Tests/Acceptance/Frontend/PopupInteractionsCest.php +++ b/Tests/Acceptance/Frontend/PopupInteractionsCest.php @@ -11,21 +11,9 @@ 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; /** @@ -33,56 +21,22 @@ */ 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]) ); } @@ -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); } /** @@ -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]) ); } @@ -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); - } } diff --git a/Tests/Acceptance/Support/AcceptanceTester.php b/Tests/Acceptance/Support/AcceptanceTester.php index 16535bee..7ec74a82 100644 --- a/Tests/Acceptance/Support/AcceptanceTester.php +++ b/Tests/Acceptance/Support/AcceptanceTester.php @@ -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; /** diff --git a/Tests/Acceptance/Support/Constants.php b/Tests/Acceptance/Support/Constants.php new file mode 100644 index 00000000..1cf84b40 --- /dev/null +++ b/Tests/Acceptance/Support/Constants.php @@ -0,0 +1,50 @@ + '//*[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; +}