使用symfony domcrawler获取选择选项值的列表

时间:2014-09-05 10:53:12

标签: 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-&gt; html()和form2-&gt; text()为我提供了表单按钮的详细信息。

1 个答案:

答案 0 :(得分: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));