2016-03-02 54 views
3

我在搜索bot爬蟲程序的項目中有一些特性/限制,我想使用代碼來測試它們,並且我發現有一個選項可以使用setHeader方法設置標題。在我的情況下,它不工作。我得到[RuntimeException] Call to undefined method FunctionalTester::setHeader錯誤。在代碼中設置用戶代理cept

代碼波紋管:

<?php 
/** @var \Codeception\Scenario $scenario */ 
$I = new FunctionalTester($scenario); 

$I->setHeader('User-Agent', 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'); 

FileDetailPage::of($I)->amOnFileDetail(96972088, 'raped.zip'); 

根據Authorization header not making it through in Codeception API testing我不知道是否要設置用戶代理頭爲每個請求。是否有方法或配置選項永久更改用戶代理

+1

的setHeader方法只用PhpBrowser模塊工作,你能嗎? – Naktibalda

+0

我使用基於phpbrowser的模塊,但很可能不支持設置標題,因爲我可以看到。謝謝你的幫助。 – Northys

回答

2

您可能會感到困惑功能測試驗收測試。只有驗收測試使用PhpBrowser。功能測試直接運行PHP代碼而不發出HTTP請求。

而是試圖回答統一設置標題的問題...

根據我的doc page here的理解,我覺得你應該能夠設置用戶代理在您的配置文件,像這樣:

class_name: AcceptanceTester 
modules: 
    enabled: 
     - PhpBrowser: 
      url: http://ames.dev/ 
      headers: 
       User-Agent: Codeception Custom User Agent 
     - \Helper\Acceptance 

但我還沒有完全正常工作呢。如果您想更正,請編輯我的文章。

+0

此示例對我無效 – ghego1

2

在我的情況,我做了這樣的工作:

class_name: AcceptanceTester 
modules: 
    enabled: 
     - PhpBrowser: 
      url: http://xxxx.xx 
      headers: 
       header-testing: 
        value: someValue 
     - \Helper\Acceptance 

這使得數組值在$_SERVER變量:

HTTP_HEADER_TESTING => "someValue" 
+0

此示例適用於我 – ghego1