Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 417
CobraBJ Avatar asked Feb 20 '14 08:02

CobraBJ


People also ask

How do I find the URL in Selenium?

Click on the “console” tab. In the console, type “document. URL” command and hit enter. You will give the current URL.

How do I get the current URL in Python?

You can get the current url by doing path_info = request. META. get('PATH_INFO') http_host = request.

Why we use Get current URL command in Selenium?

Get Current URL Command In WebDriver, this method fetches the string representing the Current URL of the current web page. It accepts nothing as parameter and returns a String value.

How do you select you drivers to launch a URL in Selenium?

location='url' which can help you to achieve this. String url = "https://www.google.com"; String script = "window. location = \'"+url+"\'"; System. setProperty("webdriver.


4 Answers

The answer is:

$currentURL = $this->url();

I also asked this question here

Thanks to @jaruzafa

like image 165
CobraBJ Avatar answered Oct 21 '22 13:10

CobraBJ


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

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

like image 28
MauricioOtta Avatar answered Oct 21 '22 11:10

MauricioOtta


You can also use

$url=$this->getLocation();

like image 45
Vidz Avatar answered Oct 21 '22 13:10

Vidz


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

like image 41
Adrian Antohe Avatar answered Oct 21 '22 13:10

Adrian Antohe