我正在使用我的测试在 estore 中创建产品,并且需要在提交表单后获取 url。
提交按钮后是否可以在测试范围内获取url ?
$I->click('#formSubmit');
$I->wait(5); // wait for redirect to new url
$url = $I->someFunctionToGetCurrentUrl()`
我从控制台运行这个测试,而不是从网络浏览器,所以我无权访问服务器端的 $_SERVER。
但是如果我有一些方法,比如$I->canSeeCurrentUrlEquals()
在 codeception 框架中,那么我应该能够以某种方式访问当前的 url ......
怎么做?
解决方案是在 _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();
我只得到 URI/URL 的路径部分,从来没有完整的东西(包括 http)。
我希望它对某人有用,用协议等获取完整的 URL。用这个替换 return 语句: return $this->getModule('WebDriver')->_getUrl().$this->getModule('WebDriver')- >_getCurrentUri();