【问题标题】:PHPUnit and WebDriver - How to get the current URL?PHPUnit 和 WebDriver - 如何获取当前 URL?
【发布时间】:2014-03-21 00:45:11
【问题描述】:

场景: 我打开 www.google.com,输入一些关键字,然后单击搜索按钮。 现在我进入结果页面。我想获取这个结果页的当前url,包括查询参数。

我在这里phpunit-selenium on github 找到了一个方法getBrowserUrl()。 410号线

但是这个方法返回了我在setUp函数中设置的值。

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;
}

我得到一个字符串 'http://www.google.com/' 而不是结果页面的整个 url。 请帮助我,谢谢!

【问题讨论】:

  • 看起来你可以在驱动程序上调用getLocation()as seen in source
  • @rutter 这是一种 selenium RC 方法。我不能将它用于 Selenium2 Webdriver。但是,我试过了,有一个错误“BadMethodCallException:命令'getLocation'不存在或尚不支持”。谢谢

标签: selenium phpunit


【解决方案1】:

答案是:

$currentURL = $this->url();

我也问过这个问题here

感谢@jaruzafa

【讨论】:

    【解决方案2】:
    【解决方案3】:

    你也可以使用

    $url=$this->getLocation();

    【讨论】:

    【解决方案4】:

    如果您使用的是旧版本的 PHPUnit Selenium,这可能会有所帮助:

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

    【讨论】:

      【解决方案5】:

      这对我有用

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

      【讨论】:

        猜你喜欢
        • 2013-04-05
        • 1970-01-01
        • 2012-07-17
        • 2017-02-13
        • 1970-01-01
        • 2022-07-10
        • 2014-01-27
        • 2021-08-02
        相关资源
        最近更新 更多