2014-09-05 113 views
1

在单元测试中,我想获取选择选项值的列表。使用symfony domcrawler获取选择选项值的列表

我的页面包含一个下拉列表

<form name='myform'> 
    <select id='list' name='formvalues[list]'> 
    <option value='1'>Option 1</option> 
    <option value='2'>Option 2</option> 
    </select> 
</form> 

在我的单元测试)形式,

$client = static::createClient();  
    $crawler = $client->request('GET', 'http://my.testapp.com/'); 

    // Try 1 
    $form1 = $crawler->selectButton('web_advert_search[search]')->form(); 
    // Try 2 
    // $form2 = $crawler->filter('#web_advert_search_search'); 

    // I want something liks this 
    $values = $form['formvalues[list]']->availableOptionValues(); 

form2-> HTML()和form2->文本(是给我的细节的窗体按钮。

回答

1

我设法通过逐步遍历返回的元素来解决这个问题。

$client = static::createClient(); 
$crawler = $client->request('GET', 'http://my.testapp.com/'); 
$response = $client->getResponse(); 

// web_advert_search[search] is the NAME of the submit button for the form. 
$searchForm = $crawler->selectButton('web_advert_search[search]')->form(); 
$searchFormCategorySelect = $searchForm['web_advert_search']['category']; 
$searchFormCategoryOptions = $searchFormCategorySelect->availableOptionValues(); 
$this->assertEquals(10, count($searchFormCategoryOptions)); 
相关问题