2016-04-14 89 views
6

我正在用我的測試在estore中創建產品,並且需要在提交表單後獲取網址。如何從codeception&phantomjs測試中獲取當前網址?

提交按鈕後可否在測試範圍中獲得url

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

我從控制檯運行此測試,而不是從網絡瀏覽器,所以我不能夠訪問$ _ SERVER是在服務器端。

但是,如果我有codeception框架的一些方法,如$I->canSeeCurrentUrlEquals()的話,我應該以某種方式能夠訪問當前網址...

怎麼辦呢?

+1

你是什麼意思「在測試範圍」? –

+0

我從控制檯運行此測試,而不是從Web瀏覽器運行,所以我沒有訪問服務器端的$ _​​SERVER。但是,如果我在代碼框架中有''I-> canSeeCurrentUrlEquals()''這樣的方法,那麼我應該能夠訪問當前的url ...怎麼做?這是個問題... –

+0

請編輯您的問題以包含此附加信息。 – buczek

回答

6

的解決方案是一個輔助方法在_support添加到AcceptanceHelper/AcceptanceHelper.php文件:

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(); 
1

如果你沒有一個AcceptanceHelper/FunctionalHelper類(並且$ this-> getModule或$ this-> client未定義),那麼AcceptanceTester/FunctionalTester類中的以下內容應該可以工作:

public function getCurrentUrl() { 
    return $this->executeJS("return location.href"); 
} 
+0

需要進行哪些編輯? – tschumann