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

问题描述 投票:8回答:2

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

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

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

我从控制台运行此测试,而不是从Web浏览器运行,因此我无法访问服务器端的$ _​​SERVER。

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

怎么做?

php phantomjs codeception
2个回答
8
投票

解决方案是在_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();

2
投票

如果您没有AcceptanceHelper / FunctionalHelper类(因此$ this-> getModule或$ this-> client未定义),那么AcceptanceTester / FunctionalTester类中的以下内容应该有效:

public function getCurrentUrl() {
    return $this->executeJS("return location.href");
}
© www.soinside.com 2019 - 2024. All rights reserved.