【问题标题】:Get list of select option values using symfony domcrawler使用 symfony domcrawler 获取选择选项值列表
【发布时间】:2014-10-30 07:27:22
【问题描述】:

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

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

<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->text() 为我提供了表单按钮的详细信息。

【问题讨论】:

    标签: symfony domcrawler


    【解决方案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));
    

    【讨论】:

      猜你喜欢
      • 2014-05-18
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-16
      • 1970-01-01
      • 2017-10-21
      • 1970-01-01
      相关资源
      最近更新 更多