Skip to content

Commit

Permalink
MDL-50919 behat: Step for keypress event in behat
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Sep 2, 2015
1 parent 8b25614 commit 0cf7252
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/behat/form_field/behat_form_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ public function get_value() {
return $instance->get_value();
}

/**
* Presses specific keyboard key.
*
* @param mixed $char could be either char ('b') or char-code (98)
* @param string $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta')
*/
public function key_press($char, $modifier = null) {
// We delegate to the best guess, if we arrived here
// using the generic behat_form_field is because we are
// dealing with a fgroup element.
$instance = $this->guess_type();
return $instance->field->keyPress($char, $modifier);
}

/**
* Generic match implementation
*
Expand Down
28 changes: 28 additions & 0 deletions lib/tests/behat/behat_forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,34 @@ public function i_set_the_field_to($field, $value) {
$this->set_field_value($field, $value);
}

/**
* Press the key in the field to trigger the javascript keypress event
*
* Note that the character key will not actually be typed in the input field
*
* @Given /^I press key "(?P<key_string>(?:[^"]|\\")*)" in the field "(?P<field_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $key either char-code or character itself,
* may optionally be prefixed with ctrl-, alt-, shift- or meta-
* @param string $field
* @return void
*/
public function i_press_key_in_the_field($key, $field) {
if (!$this->running_javascript()) {
throw new DriverException('Key press step is not available with Javascript enabled');
}
$fld = behat_field_manager::get_form_field_from_label($field, $this);
$modifier = null;
$char = $key;
if (preg_match('/-/', $key)) {
list($modifier, $char) = preg_split('/-/', $key, 2);
}
if (is_numeric($char)) {
$char = (int)$char;
}
$fld->key_press($char, $modifier);
}

/**
* Sets the specified value to the field.
*
Expand Down

0 comments on commit 0cf7252

Please sign in to comment.