/** * Manually adds a reviewer for workshop participant. * * This step should start on manual allocation page. * * @When /^I add a reviewer "(?P<reviewer_name_string>(?:[^"]|\\")*)" for workshop participant "(?P<participant_name_string>(?:[^"]|\\")*)"$/ * @param string $reviewername * @param string $participantname */ public function i_add_a_reviewer_for_workshop_participant($reviewername, $participantname) { $participantnameliteral = behat_context_helper::escape($participantname); $xpathtd = "//table[contains(concat(' ', normalize-space(@class), ' '), ' allocations ')]/" . "tbody/tr[./td[contains(concat(' ', normalize-space(@class), ' '), ' peer ')]" . "[contains(.,{$participantnameliteral})]]/" . "td[contains(concat(' ', normalize-space(@class), ' '), ' reviewedby ')]"; $xpathselect = $xpathtd . "/descendant::select"; try { $selectnode = $this->find('xpath', $xpathselect); } catch (Exception $ex) { $this->find_button(get_string('showallparticipants', 'workshopallocation_manual'))->press(); $selectnode = $this->find('xpath', $xpathselect); } $selectformfield = behat_field_manager::get_form_field($selectnode, $this->getSession()); $selectformfield->set_value($reviewername); if (!$this->running_javascript()) { // Without Javascript we need to press the "Go" button. $go = behat_context_helper::escape(get_string('go')); $this->find('xpath', $xpathtd . "/descendant::input[@value={$go}]")->click(); } else { // With Javascript we just wait for the page to reload. $this->getSession()->wait(self::EXTENDED_TIMEOUT, self::PAGE_READY_JS); } // Check the success string to appear. $allocatedtext = behat_context_helper::escape(get_string('allocationadded', 'workshopallocation_manual')); $this->find('xpath', "//*[contains(.,{$allocatedtext})]"); }
/** * Tries to fill the current page form elements with the provided options. * * This step is slow as it spins over each provided option, we are * not expected to have lots of provided options, anyways, is better * to be conservative and wait for the elements to appear rather than * to have false failures. * * @param TableNode $options The backup and restore options or false if no options provided * @return void */ protected function fill_backup_restore_form($options) { // Nothing to fill if no options are provided. if (!$options) { return; } // Wait for the page to be loaded and the JS ready. $this->wait(); // If we find any of the provided options in the current form we should set the value. $datahash = $options->getRowsHash(); foreach ($datahash as $locator => $value) { try { // Using $this->find* to enforce stability over speed. $fieldnode = $this->find_field($locator); $field = behat_field_manager::get_form_field($fieldnode, $this->getSession()); $field->set_value($value); } catch (ElementNotFoundException $e) { // Next provided option then, this one should be part of another page's fields. } } }
/** * Checks, the field does not match the value. * * @Then /^the field with xpath "(?P<xpath_string>(?:[^"]|\\")*)" does not match value "(?P<field_value_string>(?:[^"]|\\")*)"$/ * @throws ExpectationException * @throws ElementNotFoundException Thrown by behat_base::find * @param string $fieldxpath * @param string $value * @return void */ public function the_field_with_xpath_does_not_match_value($fieldxpath, $value) { // Get the field. $fieldnode = $this->find('xpath', $fieldxpath); $formfield = behat_field_manager::get_form_field($fieldnode, $this->getSession()); // Checks if the provided value matches the current field value. if ($formfield->matches($value)) { throw new ExpectationException('The \'' . $fieldxpath . '\' value matches \'' . $value . '\' and it should not match it', $this->getSession()); } }
/** * Sets the specified value to the field with xpath. * * @Given /^I set the field with xpath "(?P<fieldxpath_string>(?:[^"]|\\")*)" to "(?P<field_value_string>(?:[^"]|\\")*)"$/ * @throws ElementNotFoundException Thrown by behat_base::find * @param string $field * @param string $value * @return void */ public function i_set_the_field_with_xpath_to($fieldxpath, $value) { $fieldNode = $this->find('xpath', $fieldxpath); $field = behat_field_manager::get_form_field($fieldNode, $this->getSession()); $field->set_value($value); }
/** * Sets the specified value to the field with xpath. * * @Given /^I set the field with xpath "(?P<fieldxpath_string>(?:[^"]|\\")*)" to "(?P<field_value_string>(?:[^"]|\\")*)"$/ * @throws ElementNotFoundException Thrown by behat_base::find * @param string $field * @param string $value * @return void */ public function i_set_the_field_with_xpath_to($fieldxpath, $value) { try { $fieldNode = $this->find('xpath', $fieldxpath); } catch (\Behat\Mink\Exception\ElementNotFoundException $e) { throw new ElementNotFoundException('Field with xpath ' . $fieldxpath . 'not found, so can\'t be set'); } $field = behat_field_manager::get_form_field($fieldNode, $this->getSession()); $field->set_value($value); }
/** * Checks that the form element field have the specified value. * * @Then /^the "(?P<field_string>(?:[^"]|\\")*)" field should match "(?P<value_string>(?:[^"]|\\")*)" value$/ * @throws ExpectationException * @throws ElementNotFoundException Thrown by behat_base::find * @param string $locator * @param string $value */ public function the_field_should_match_value($locator, $value) { $fieldnode = $this->find_field($locator); // Get the field. $field = behat_field_manager::get_form_field($fieldnode, $this->getSession()); $fieldvalue = $field->get_value(); // Checks if the provided value matches the current field value. if (trim($value) != trim($fieldvalue)) { throw new ExpectationException('The \'' . $locator . '\' value is \'' . $fieldvalue . '\', \'' . $value . '\' expected', $this->getSession()); } }
/** * Replaces text in the field's content. * The step begins in a form. * * @Given /^I replace in field "(?P<field_string>(?:[^"]|\\")*)" "(?P<text_string>(?:[^"]|\\")*)" with "(?P<replacement_string>(?:[^"]|\\")*)"$/ * @param string $locator * @param string $text * @param string $replacement */ public function i_replace_in_field_with($locator, $text, $replacement) { $node = $this->find_field($locator); $field = behat_field_manager::get_form_field($node, $this->getSession()); $value = $field->get_value(); $value = str_replace($text, $replacement, $value); // Hack to remove new line characters from editor field value. if (get_class($field) == 'behat_form_editor') { $value = str_replace(array("\n", "\r"), '', $value); } $field->set_value($value); }
/** * Uploads a file to filemanager * * @throws ExpectationException Thrown by behat_base::find * @param string $filepath * @param string $filemanagerelement * @param TableNode $data Data to fill in upload form * @param false|string $overwriteaction false if we don't expect that file with the same name already exists, * or button text in overwrite dialogue ("Overwrite", "Rename to ...", "Cancel") */ protected function upload_file_to_filemanager($filepath, $filemanagerelement, TableNode $data, $overwriteaction = false) { global $CFG; $filemanagernode = $this->get_filepicker_node($filemanagerelement); // Opening the select repository window and selecting the upload repository. $this->open_add_file_window($filemanagernode, get_string('pluginname', 'repository_upload')); // Ensure all the form is ready. $noformexception = new ExpectationException('The upload file form is not ready', $this->getSession()); $this->find('xpath', "//div[contains(concat(' ', normalize-space(@class), ' '), ' file-picker ')]" . "[contains(concat(' ', normalize-space(@class), ' '), ' repository_upload ')]" . "/descendant::div[@class='fp-content']" . "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-upload-form ')]" . "/descendant::form", $noformexception); // After this we have the elements we want to interact with. // Form elements to interact with. $file = $this->find_file('repo_upload_file'); // Attaching specified file to the node. // Replace 'admin/' if it is in start of path with $CFG->admin . $pos = strpos($filepath, 'admin/'); if ($pos === 0) { $filepath = $CFG->admin . DIRECTORY_SEPARATOR . substr($filepath, 6); } $filepath = str_replace('/', DIRECTORY_SEPARATOR, $filepath); $fileabsolutepath = $CFG->dirroot . DIRECTORY_SEPARATOR . $filepath; $file->attachFile($fileabsolutepath); // Fill the form in Upload window. $datahash = $data->getRowsHash(); // The action depends on the field type. foreach ($datahash as $locator => $value) { // Getting the node element pointed by the label. $fieldnode = $this->find_field($locator); // Gets the field type from a parent node. $field = behat_field_manager::get_form_field($fieldnode, $this->getSession()); // Delegates to the field class. $field->set_value($value); } // Submit the file. $submit = $this->find_button(get_string('upload', 'repository')); $submit->press(); // We wait for all the JS to finish as it is performing an action. $this->getSession()->wait(self::TIMEOUT, self::PAGE_READY_JS); if ($overwriteaction !== false) { $overwritebutton = $this->find_button($overwriteaction); $this->ensure_node_is_visible($overwritebutton); $overwritebutton->click(); // We wait for all the JS to finish. $this->getSession()->wait(self::TIMEOUT, self::PAGE_READY_JS); } }
/** * Picks the file from private files repository * * @throws ExpectationException Thrown by behat_base::find * @param string $filepath * @parma string $repository * @param string $filemanagerelement * @param TableNode $data Data to fill the form in Select file dialogue * @param false|string $overwriteaction false if we don't expect that file with the same name already exists, * or button text in overwrite dialogue ("Overwrite", "Rename to ...", "Cancel") */ protected function add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, TableNode $data, $overwriteaction = false) { $filemanagernode = $this->get_filepicker_node($filemanagerelement); // Opening the select repository window and selecting the upload repository. $this->open_add_file_window($filemanagernode, $repository); $this->open_element_contextual_menu($filepath); // Fill the form in Select window. $datahash = $data->getRowsHash(); // The action depends on the field type. foreach ($datahash as $locator => $value) { // Getting the node element pointed by the label. $fieldnode = $this->find_field($locator); // Gets the field type from a parent node. $field = behat_field_manager::get_form_field($fieldnode, $this->getSession()); // Delegates to the field class. $field->set_value($value); } $this->find_button(get_string('getfile', 'repository'))->click(); // We wait for all the JS to finish as it is performing an action. $this->getSession()->wait(self::TIMEOUT, self::PAGE_READY_JS); if ($overwriteaction !== false) { $overwritebutton = $this->find_button($overwriteaction); $this->ensure_node_is_visible($overwritebutton); $overwritebutton->click(); // We wait for all the JS to finish. $this->getSession()->wait(self::TIMEOUT, self::PAGE_READY_JS); } }
/** * Generic field setter. * * Internal API method, a generic *I set "VALUE" to "FIELD" field* * could be created based on it. * * @param string $fieldlocator The pointer to the field, it will depend on the field type. * @param string $value * @return void */ protected function set_field_value($fieldlocator, $value) { $node = $this->find_field($fieldlocator); // We delegate to behat_form_field class, it will // guess the type properly as it is a select tag. $field = behat_field_manager::get_form_field($node, $this->getSession()); $field->set_value($value); }