diff --git a/behat.yml.dist b/behat.yml.dist index b3c68034..71fe0e7b 100644 --- a/behat.yml.dist +++ b/behat.yml.dist @@ -10,7 +10,9 @@ default: - Drupal\DrupalExtension\Context\MessageContext - Drupal\Tests\oe_webtools\Behat\WebtoolsAnalyticsMinkContext - Drupal\Tests\oe_webtools\Behat\WebtoolsConfigContext - - Drupal\Tests\oe_webtools\Behat\WebtoolsCookieConsentContext + - Drupal\Tests\oe_webtools\Behat\WebtoolsCookieConsentContext: + selectors: + oembed_video: 'iframe' - Drupal\Tests\oe_webtools\Behat\WebtoolsCleanupContext - Drupal\Tests\oe_webtools\Behat\WebtoolsExtensionsContext - Drupal\Tests\oe_webtools\Behat\WebtoolsGlobanContext diff --git a/tests/Behat/WebtoolsCookieConsentContext.php b/tests/Behat/WebtoolsCookieConsentContext.php index 3e4d4a9b..7f5a435f 100644 --- a/tests/Behat/WebtoolsCookieConsentContext.php +++ b/tests/Behat/WebtoolsCookieConsentContext.php @@ -25,6 +25,23 @@ class WebtoolsCookieConsentContext extends RawDrupalContext { */ protected $configContext; + /** + * A list of css selectors needed by this context, keyed by name. + * + * @var array + */ + protected $selectors = []; + + /** + * WebtoolsCookieConsentContext constructor. + * + * @param array $selectors + * An array of css selectors, keyed by name. + */ + public function __construct(array $selectors) { + $this->selectors = $selectors; + } + /** * Gathers some other contexts. * @@ -94,7 +111,7 @@ public function iVisitTheRemoteVideoEntityPage(TableNode $mediasTable): void { * @Then I should see the oEmbed video iframe with Cookie Consent */ public function assertOembedIframeWithCckUsage(): void { - $iframe_url = $this->getSession()->getPage()->find('css', 'iframe')->getAttribute('src'); + $iframe_url = $this->getSession()->getPage()->find('css', $this->getSelector('oembed_video'))->getAttribute('src'); $this->visitPath(str_replace(rtrim($this->getDrupalParameter('drupal')['drupal_root'], '/'), '', $iframe_url)); $this->assertSession()->elementExists('css', "iframe[src^='" . OE_WEBTOOLS_COOKIE_CONSENT_EMBED_COOKIE_URL . "?oriurl=']"); } @@ -105,7 +122,7 @@ public function assertOembedIframeWithCckUsage(): void { * @Then I should not see the oEmbed video iframe with Cookie Consent */ public function assertNoOembedIframeWithCckUsage(): void { - $iframe_url = $this->getSession()->getPage()->find('css', 'iframe')->getAttribute('src'); + $iframe_url = $this->getSession()->getPage()->find('css', $this->getSelector('oembed_video'))->getAttribute('src'); $this->visitPath(str_replace(rtrim($this->getDrupalParameter('drupal')['drupal_root'], '/'), '', $iframe_url)); $this->assertSession()->elementNotExists('css', "iframe[src^='" . OE_WEBTOOLS_COOKIE_CONSENT_EMBED_COOKIE_URL . "?oriurl=']"); } @@ -172,4 +189,24 @@ protected function webtoolsJsonSnippetExists(string $snippet): bool { return FALSE; } + /** + * Returns a css selector from the context configuration. + * + * @param string $name + * The selector name. + * + * @return string + * The css selector. + * + * @throws \Exception + * Thrown when the selector is not found. + */ + protected function getSelector(string $name): string { + if (!array_key_exists($name, $this->selectors)) { + throw new \Exception(sprintf('Missing selector "%s".', $name)); + } + + return $this->selectors[$name]; + } + }