2012-01-27 17 views
1

我有一個應用程序通過設置到我的應用程序域的cookie使用外部服務。在開發過程中,我手工創建了這個cookie,但在生產中,這個cookie將通過登錄生成。在我爲這個外部服務運行測試之前,有什麼方法可以使用我在開發期間設置的cookie?有沒有在使用PHPUnit_Extensions_SeleniumTestCase進行測試期間設置cookie的方法?

我有一個猜測,我可以使用捲曲來自動化這一點,但我想知道如果我在PHPUnit和/或Selenium中遺漏了一些隱藏的功能或技術。

[類延伸PHPUnit_Extensions_SeleniumTestCase的]

/** 
* Can get the current authenticated user. 
*/ 
public function testCanGetTheCurrentAuthenticatedUser() 
{ 
    $this->open('http://my/local/virtual/host/api/getCurrentUser'); 
    $json = json_decode($this->getBodyText()); 
    $this->assertEquals('25', $json->response->id); 

} 

回答

0

Selenium documentation具有createCookie(),並且它列爲PHPUnit_Extensions_SeleniumTestCase的方法。從文檔中,您可以使用以下內容將login Cookie設置爲frank.wilson五分鐘。

$this->createCookie('login=frank.wilson', 'max_age=600'); 
+0

信息是好的,但我的問題仍然沒有解決。 David,你有沒有使用createCookie的示例代碼? – ezraspectre 2012-01-27 20:21:49

+0

我還沒有使用它。我們的基於硒的測試非常有限。嘗試在'createCookie()'後面使用'getCookie()'或'getCookieByName()'來查看它是否被設置。另一種選擇是查看Selenium 2是否有效。 – 2012-01-27 22:14:39

1

對於需要cookie身份驗證的測試,使用Curl結束。

$curl = curl_init('http://my/local/virtual/host/api/getCurrentUser'); 
curl_setopt($curl, CURLOPT_COOKIE, 'mycookie=authentication'); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
$output = curl_exec($curl); 
$json = json_decode($output); 
$this->assertEquals('25', $json->response->id); 
相關問題