/**
  * Guesses the element type we are dealing with in case is not a text-based element.
  *
  * This class is the generic field type, behat_field_manager::get_form_field()
  * should be able to find the appropiate class for the field type, but
  * in cases like moodle form group elements we can not find the type of
  * the field through the DOM so we also need to take care of the
  * different field types from here. If we need to deal with more complex
  * moodle form elements we will need to refactor this simple HTML elements
  * guess method.
  *
  * @return behat_form_field
  */
 private function guess_type()
 {
     global $CFG;
     // We default to the text-based field if nothing was detected.
     if (!($type = behat_field_manager::guess_field_type($this->field, $this->session))) {
         $type = 'text';
     }
     $classname = 'behat_form_' . $type;
     $classpath = $CFG->dirroot . '/lib/behat/form_field/' . $classname . '.php';
     require_once $classpath;
     return new $classname($this->session, $this->field);
 }
 /**
  * Checks grade values with or without a edit box.
  *
  * @Then /^the grade for "([^"]*)" in grade item "([^"]*)" should match "([^"]*)"$/
  * @throws Exception
  * @throws ElementNotFoundException
  * @param string $student
  * @param string $itemname
  * @param string $value
  * @return Then
  */
 public function the_grade_should_match($student, $itemname, $value)
 {
     $xpath = $this->get_student_and_grade_value_selector($student, $itemname);
     $gradefield = $this->getSession()->getPage()->find('xpath', $xpath);
     if (!empty($gradefield)) {
         // Get the field.
         $fieldtype = behat_field_manager::guess_field_type($gradefield, $this->getSession());
         if (!$fieldtype) {
             throw new Exception('Could not get field type for grade field "' . $itemname . '"');
         }
         $field = behat_field_manager::get_field_instance($fieldtype, $gradefield, $this->getSession());
         if (!$field->matches($value)) {
             $fieldvalue = $field->get_value();
             throw new ExpectationException('The "' . $student . '" and "' . $itemname . '" grade is "' . $fieldvalue . '", "' . $value . '" expected', $this->getSession());
         }
     } else {
         // If there isn't a form field, just search for contents.
         $valueliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($value);
         $xpath = $this->get_student_and_grade_cell_selector($student, $itemname);
         $xpath .= "[contains(normalize-space(.)," . $valueliteral . ")]";
         $node = $this->getSession()->getDriver()->find($xpath);
         if (empty($node)) {
             $locatorexceptionmsg = 'Cell for "' . $student . '" and "' . $itemname . '" with value "' . $value . '"';
             throw new ElementNotFoundException($this->getSession(), $locatorexceptionmsg, null, $xpath);
         }
     }
 }