'PHPUnit and WebDriver - How to get the current URL?

Scenario: I open www.google.com, input some keywords and click the search button. now i get to the result page. I want to get the current url of this result page, including the query parameters.

I found a method getBrowserUrl() here phpunit-selenium on github. Line 410

But this method returned the value which I set in the setUp function.

public function setUp(){
$this->setBrowser(testConfig::$browserName);
$this->setBrowserUrl('http://www.google.com/');
}

public function testGoogleSearch(){
$this->url('');
//input some keywords
.......
//click search button
.......
//want to get the url of result page
$resultUrl= $this->getBrowserUrl();
echo $resultUrl;
}

I got a string 'http://www.google.com/' instead of the whole url of result page. Please help me,thanks!



Solution 1:[1]

The answer is:

$currentURL = $this->url();

I also asked this question here

Thanks to @jaruzafa

Solution 2:[2]

From the source code, I'd say it's getCurrentURL()

https://github.com/facebook/php-webdriver/blob/787e71db74e42cdf13a41d500f75ea43da84bc75/lib/WebDriver.php#L43

Solution 3:[3]

You can also use

$url=$this->getLocation();

Solution 4:[4]

This is how I'll get the current URL

$resultUrl = $this->getSession()->getDriver()->getCurrentUrl();
echo $resultUrl;

or

$resultUrl = $this->getSession()->getCurrentUrl();
echo $resultUrl;

Both are from Behat - Mink

Solution 5:[5]

If you are using an older version of PHPUnit Selenium this might be helpful:

$url = $this->getEval('window.location.href;');
$this->assertEquals('EXPECTEDURL', $url);

Solution 6:[6]

This is what worked for me

$urlAry = $driver->executeScript('return window.location',array());
$currentURL = $urlAry['href'];

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CobraBJ
Solution 2 MauricioOtta
Solution 3 Vidz
Solution 4 Panagiotis Simakis
Solution 5 totas
Solution 6 K. Shahzad