public function fillInFieldsIfPresent($fields) { // shorthand $formId = $this->formId; // what are we doing? $log = usingLog()->startAction("fill in " . count($fields) . " field(s) in form '{$formId}' if present"); foreach ($fields as $labelText => $fieldValue) { // find the element $element = $log->addStep("finding field with label, id or name '{$labelText}'", function ($log) use($formId, $labelText) { try { return fromForm($formId)->get()->elementByLabelIdOrName($labelText); } catch (Exception $e) { $log->endAction("field '{$labelText}' not present; ignoring!"); return null; } }); // did we get one? if ($element == null) { // missing field continue; } $tag = strtolower($element->name()); switch ($tag) { case 'input': case 'textarea': $this->type($fieldValue)->intoElement($element); break; case 'select': $this->select($fieldValue)->fromElement($element); break; default: $log->endAction("* field '{$labelText}' has unexpected tag '{$tag}' *"); throw new E5xx_ActionFailed(__METHOD__); } } // all done $log->endAction(); }
// ------------------------------------------------------------------------ // ======================================================================== // // PRE-TEST INSPECTION // // ------------------------------------------------------------------------ // ======================================================================== // // POSSIBLE ACTION(S) // // ------------------------------------------------------------------------ $story->addAction(function () { // get the checkpoint, to store data in $checkpoint = getCheckpoint(); // load our test page usingBrowser()->gotoPage("file://" . __DIR__ . '/../../testpages/WorkingWithForms.html'); // get a field from a form $checkpoint->field1 = fromForm("test_form")->getValue()->fieldLabelled('Page Name'); }); // ======================================================================== // // POST-TEST INSPECTION // // ------------------------------------------------------------------------ $story->addPostTestInspection(function () { // get the checkpoint $checkpoint = getCheckpoint(); // do we have the title we expected? assertsObject($checkpoint)->hasAttribute('field1'); assertsString($checkpoint->field1)->equals("Storyplayer: Working With Forms"); });