我正在使用我的测试在estore中创建产品,并且需要在提交表单后获取URL.
提交按钮后是否可以在测试范围内获取URL?
- $I->click('#formSubmit');
- $I->wait(5); // wait for redirect to new url
- $url = $I->someFunctionToGetCurrentUrl()`
我从控制台运行此测试,而不是从Web浏览器运行,因此我无法访问服务器端的$_SERVER.
但是,如果我在代码框架中有一些像$I-> canSeeCurrentUrlEquals()这样的方法,那么我应该能以某种方式访问当前的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();