From c8486ec12bc4668b414b784a3362192f8a3fad2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edi=20Modri=C4=87?= Date: Mon, 6 May 2019 16:55:26 +0200 Subject: [PATCH] Add method to verify URL fragments This method proved useful in some of our tests for apps which use javascript based routing --- src/Page/Page.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Page/Page.php b/src/Page/Page.php index 5051241..1c20f8c 100644 --- a/src/Page/Page.php +++ b/src/Page/Page.php @@ -103,6 +103,26 @@ protected function verifyUrl(array $urlParameters = []): void } } + /** + * Overload to verify if the current url contains a provided fragment. Throw an exception otherwise. + * + * @throws UnexpectedPageException + */ + protected function verifyUrlFragment(string $fragment): void + { + $parsedUrl = parse_url($this->getDriver()->getCurrentUrl()); + + if (!is_array($parsedUrl) || !array_key_exists('fragment', $parsedUrl)) { + throw new UnexpectedPageException(sprintf('%s URL is not valid or does not contain a fragment', $this->getDriver()->getCurrentUrl())); + } + + if (mb_strpos($parsedUrl['fragment'], $fragment) !== false) { + return; + } + + throw new UnexpectedPageException(sprintf('Expected to have "%s" URL fragment but found "%s" instead', $fragment, $parsedUrl['fragment'])); + } + protected function getParameter(string $name): ?string { return $this->parameters[$name] ?? null;