Warm tip: This article is reproduced from serverfault.com, please click

php-如何从 codeception 和 phantomjs 测试中获取当前 url?

(php - how to get current url from codeception & phantomjs test?)

发布于 2016-04-14 15:05:41

我正在使用我的测试在 estore 中创建产品,并且需要在提交表单后获取 url。

提交按钮后是否可以在测试范围内获取url

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

我从控制台运行这个测试,而不是从网络浏览器,所以我无权访问服务器端的 $_SERVER。

但是如果我有一些方法,比如$I->canSeeCurrentUrlEquals()在 codeception 框架中,那么我应该能够以某种方式访问​​当前的 url ......

怎么做?

Questioner
Ilja
Viewed
0
Ilja 2016-04-15 02:45:06

解决方案是在 _support/AcceptanceHelper.php 文件中为 AcceptanceHelper 添加一个辅助方法:

    class AcceptanceHelper extends \Codeception\Module
    {

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

    }

然后在测试中使用它:

$url = $I->getCurrentUrl();