2016-11-07 53 views
1

我在做功能測試和我得到的錯誤Symfony2的InvalidArgumentException:當前節點列表是空

InvalidArgumentException:當前節點列表爲空

這是我的代碼

public function testThis(){ 
    $requestContent = [ 
     'val1' => '324343', 
     'valname' => '"Benjamin"', 
     'valLast' => '"A"', 
     'valnum' => '44343', 
     'hndval1' => '0000', 
     'hdnval2' => '0000', 
     'hndval3' => '1111', 
     'hndref' => '"ThisIsAtest"', 
     'hdnMessage' => '"I am a message"' 

    ]; 

    $crawler = $this->client->request('GET', '/'); 
    $submitButton = $crawler->selectButton('btnSubmit'); 
    $form = $submitButton->form($requestContent); 

    print_r($form); 
    $this->client->followRedirects(true); 
    $crawler = $this->client->submit($form); 

    $response = $this->client->getResponse(); 
    $request = $this->client->getRequest(); 
    print_r($crawler->html()); 
    $this->assertRegExp('/\/nextPage', $request->getUri()); 
    print_r($request->getUri()); 
    $a = $crawler->filter('input[name="pageName"]'); 

    $this->assertContains(
     "True", 
     $a->attr('value') 
    ); 
} 

我認爲這是在這個錯誤:

$ form = $ submitButton-> form($ requestContent);

請注意,$ requestContent值來自隱藏的輸入類型,它們都在form標籤內。

回答

0

您還沒有發佈你的HTML所以現在我假定你的HTML如:

<html> 
    <form method='GET' action='your action here'> 

     /* 
     * all other html here 
     */ 

     <input type='submit' value='Submit' id='btnSubmit' name='btnSubmit'> 

    </form> 

</html> 

$submitButton = $crawler->selectButton('btnSubmit'); 

改變

$submitButton = $crawler->selectButton('Submit'); 

因爲selectButton()接受按鈕值而不是id或名稱。

確保這對您有幫助。

1

你必須提供按鈕的文字$crawler->selectButton()

// For a given HTML button: 
// <input type="button" value="Upload File" /> 
// or 
// <button type="button">Upload File</button> 

$crawler->selectButton("Upload File"); 

注意,你的正則表達式'/\/nextPage'是無效的。您需要添加結束/ - >'/\/nextPage/' // This will match the exact string: "/nextPage"

+0

你好,先生,我做了你所說的,仍然得到同樣的錯誤。 – momori14

+0

@ momori14,你可以在你的問題中添加「

相關問題