Simpletest on select field loaded by ajax I have searched through Google and haven't found an answer. Stack Exchange is my last resort.
In my form, I have two select fields:
$form['system']['foo'] = array(
  ...
  '#type' =&gt; 'select',
  '#options' =&gt; array('A', 'B', 'C'),
  ...
  '#ajax' =&gt; array(
    'event' =&gt; 'change',
    ...
    'method' =&gt; 'replace',
  ),
);

$form['system']['bar'] = array(
  ...
  '#type' =&gt; 'select',
  '#options' =&gt; array(),
  '#prefix' =&gt; ...
  '#suffix' =&gt; ...
);

If user selects any of the options in foo field, the bar field dynamically loads the options through ajax. Example: If 'A' is selected on foo, bar field generates options like 'aaa', 'bbb', 'ccc' and can be selected by the user.
This form works and I need to provide its unit test using 'Simpletest'.
On my test case:
$edit = array(
  'system[foo]' =&gt; 1, // value for 'A'
  'system[bar]' =&gt; 2, // value for 'bbb'
);
$this-&gt;drupalPost('mymodule/mypath', $edit, t('Submit'));

gives an error saying system[bar] cannot be set to 2 (because it has no value of 2 yet?)
I need to emulate ajax call on system[foo] before setting system[bar].
I have tried drupalPostAjax:
$edit = array(
  'system[foo]' =&gt; 1, // value for A
);
$this-&gt;drupalPostAjax('mymodule/mypath', $edit, 'system[foo]');

and gives me a success result on ajax. On the next lines of code, I tried calling drupalPost() again using the previous code snippet but gives me the same error saying system[bar] cannot be set to 2.
How can I solve my problem? Please provide sample code as it can help me a lot..

EDIT:
Ajax works on the actual page (i.e the values are dynamically loaded to system['bar']). So I can assume my code in the forms is working.
However, I just noticed that ajax does not work on the generated page from Simpletest (the one found in sites/default/files/simpletest/verbose). Console from Google Chrome does not show any errors. Ajax returns status 200 but does not have any response.
I am using the default theme upon installation.

EDIT: The form and simpletest code
The form: I copied the form posted by znerol.
The simpletest code:
class MyModuleTestCase extends DrupalWebTestCase {
  protected $admin_user;

  public static function getInfo() {
    return array(
      'name' =&gt; 'Test Case name',
      'description' =&gt; 'Test Case Description',
      'group' =&gt; 'My Module',
    );
  }

  public function setUp() {
    // Enable module
    parent::setUp(array('mymodule'));

    // Get permissions
    $permissions = mymodule_get_permissions();
    // Permissions obtained correctly.

    // Create and login admin user
    $this-&gt;admin_user = $this-&gt;drupalCreateUser($permissions);
    $this-&gt;drupalLogin($this-&gt;admin_user);
  }

  // Test section
  public function testSystem() {
    $this-&gt;drupalGet('mymodule/mypath');
    $edit = array(
      'system[foo]' =&gt; 1, // Selects 'A'. Tested!
    );
    $this-&gt;drupalPostAjax(NULL, $edit, 'system[foo]');
    // Bar after ajax is loaded
    $edit = array(
      'system[bar]' =&gt; 1, // Selects 'aaa'
    );
    $this-&gt;drupalPost(NULL, $edit, t('Submit')); // Error here.
    $this-&gt;assertText(t('Submitted!'));
  }
}

However, when running simpletest, simpletest complains that system['bar'] cannot be set to 1 (aaa).
I have noticed simpletest shows the following warning before it calls ajax.

Undefined offset: 0 Notice drupal_web_test_case.php 2147 DrupalWebTestCase &gt;drupalPostAJAX()
Undefined index: Notice drupal_web_test_case.php 2148 DrupalWebTestCase-&gt;drupalPostAJAX()
Undefined index: #ajax Notice ajax.inc 379 ajax_form_callback()

Since system['bar'] is loaded dynamically through ajax, it has an empty array initially array(). From the warning above, I tried adding a temporary option array('1', '2') in system['bar']. The same error and warning occurs.
You should set the $path parameter to NULL when submitting to the same form. Otherwise drupalPost / drupalPostAJAX will first retrieve the page again, and thus generating a new form instead of rebuilding the current one.

The following test should do what you want:

// First retrieve the page once
$this-&gt;drupalGet('mymodule/mypath');

// Set $path to NULL on subsequent requests to the same form
$edit = array('system[foo]' =&gt; 1);
$this-&gt;drupalPostAJAX(NULL, $edit, 'system[foo]');

$edit = array('system[bar]' =&gt; 2);
$this-&gt;drupalPost(NULL, $edit, t('Submit'));


The test case succeeds when performed on the following form:

function mymodule_myform($form, &amp;$form_state) {
  $foo_options = array('A', 'B', 'C');
  $foo_default = isset($form_state['values']['system']['foo']) ? $form_state['values']['system']['foo'] : NULL;

  $form['system']['#tree'] = TRUE;

  $form['system']['foo'] = array(
    '#type' =&gt; 'select',
    '#options' =&gt; $foo_options,
    '#default_value' =&gt; $foo_default,
    '#ajax' =&gt; array(
      'callback' =&gt; 'mymodule_myform_ajax_callback',
      'wrapper' =&gt; 'bar-div',
      'method' =&gt; 'replace',
    ),
  );

  $bar_choices = array(
    0 =&gt; array('aaa', 'bbb', 'ccc'),
    1 =&gt; array('ddd', 'eee', 'fff'),
    2 =&gt; array('ggg', 'hhh', 'iii'),
  );
  $form['system']['bar'] = array(
    '#prefix' =&gt; '&lt;div id="bar-div"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
    '#type' =&gt; 'select',
    '#options' =&gt; isset($bar_choices[$foo_default]) ? $bar_choices[$foo_default] : array(),
  );

  $form['submit'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; t('Submit'),
  );
  return $form;
}

function mymodule_myform_ajax_callback($form, $form_state) {
  return $form['system']['bar'];
}