14

I am creating product in estore with my test, and need to get url after submitting a form.

Is it possible to get url in the test scope after submitting button ?

$I->click('#formSubmit');
$I->wait(5); // wait for redirect to new url
$url = $I->someFunctionToGetCurrentUrl()`

I am running this test from console, not from the web browser, so I don't have access to $_SERVER that is on the server's side.

But if I have some methods like $I->canSeeCurrentUrlEquals() in codeception framework then i should somehow be able to access current url...

how to do it?

3
  • 1
    what do you mean by "in the test scope"?
    – David Wyly
    Commented Apr 14, 2016 at 15:13
  • I am running this test from console, not from the web browser, so i dont have access to $_SERVER that is on the server's side. But if I have some methods like $I->canSeeCurrentUrlEquals() in codeception framework then i should somehow be able to access current url... how to do it? that is the question...
    – Ilja
    Commented Apr 14, 2016 at 15:22
  • Please edit your question to include this additional information.
    – buczek
    Commented Apr 14, 2016 at 15:24

3 Answers 3

11

The solution was to add a helper method to AcceptanceHelper in _support/AcceptanceHelper.php file:

    class AcceptanceHelper extends \Codeception\Module
    {

        /**
         * Get current url from WebDriver
         * @return mixed
         * @throws \Codeception\Exception\ModuleException
         */
        public function getCurrentUrl()
        {
            return $this->getModule('WebDriver')->_getCurrentUri();
        }

    }

and then use it in test:

$url = $I->getCurrentUrl();
2
  • 1
    I only get the path part of the URI/URL, never the full thing (including http).
    – MSpreij
    Commented May 25, 2020 at 14:35
  • I hope it is useful to someone, to get complete URL with protocol etc. replace return statement with this: return $this->getModule('WebDriver')->_getUrl().$this->getModule('WebDriver')->_getCurrentUri();
    – hi0001234d
    Commented Jun 17, 2020 at 12:44
8

If you don't have an AcceptanceHelper/FunctionalHelper class (and so $this->getModule or $this->client are undefined) then the following in your AcceptanceTester/FunctionalTester class should work:

public function getCurrentUrl() {
    return $this->executeJS("return location.href");
}
0
1

You can use CodeCeption's PhpBrowser function grabFromCurrentUrl() to get the current URL.

https://codeception.com/docs/modules/PhpBrowser#grabFromCurrentUrl

This function accepts regex to return a specific portion of the current URL, however also...

If no parameters are provided, the full URI is returned.

So, use it like this

$uri = $I->grabFromCurrentUrl();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.