Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 165... Línea 165...
165
            // we should not fail a test because of this. We continue if there are not expandable fields.
165
            // we should not fail a test because of this. We continue if there are not expandable fields.
166
        }
166
        }
Línea 167... Línea 167...
167
 
167
 
168
        // Different try & catch as we can have expanded fieldsets with advanced fields on them.
168
        // Different try & catch as we can have expanded fieldsets with advanced fields on them.
169
        try {
-
 
-
 
169
        try {
170
 
170
            $this->wait_for_pending_js();
171
            // Expand all fields xpath.
171
            // Expand all fields xpath.
172
            $showmorexpath = "//a[normalize-space(.)='" . get_string('showmore', 'form') . "']" .
172
            $showmorexpath = "//a[normalize-space(.)='" . get_string('showmore', 'form') . "']" .
Línea 173... Línea 173...
173
                "[contains(concat(' ', normalize-space(@class), ' '), ' moreless-toggler')]";
173
                "[contains(concat(' ', normalize-space(@class), ' '), ' moreless-toggler')]";
174
 
174
 
175
            // We don't wait here as we already waited when getting the expand fieldsets links.
175
            // We don't wait here as we already waited when getting the expand fieldsets links.
176
            if (!$showmores = $this->getSession()->getPage()->findAll('xpath', $showmorexpath)) {
176
            if (!$showmores = $this->getSession()->getPage()->findAll('xpath', $showmorexpath)) {
Línea 177... Línea 177...
177
                return;
177
                return;
178
            }
178
            }
179
 
179
 
180
            if ($this->getSession()->getDriver() instanceof \DMore\ChromeDriver\ChromeDriver) {
180
            $js = <<<EOF
181
                // Chrome Driver produces unique xpaths for each element.
-
 
182
                foreach ($showmores as $showmore) {
-
 
183
                    $showmore->click();
181
            require(['core/pending'], function(Pending) {
184
                }
182
                const query = document.evaluate("{$showmorexpath}", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
185
            } else {
183
                if (query.snapshotLength > 0) {
186
                // Funny thing about this, with findAll() we specify a pattern and each element matching the pattern
184
                    const pendingPromise = new Pending('showmore:expand');
187
                // is added to the array with of xpaths with a [0], [1]... sufix, but when we click on an element it
185
                    for (let i = 0, length = query.snapshotLength; i < length; ++i) {
188
                // does not matches the specified xpath anymore (now is a "Show less..." link) so [1] becomes [0],
186
                        query.snapshotItem(i).click();
189
                // that's why we always click on the first XPath match, will be always the next one.
187
                        if (i === length - 1) {
190
                $iterations = count($showmores);
188
                            pendingPromise.resolve();
191
                for ($i = 0; $i < $iterations; $i++) {
189
                        }
-
 
190
                    }
Línea -... Línea 191...
-
 
191
                }
-
 
192
            });
192
                    $showmores[0]->click();
193
            EOF;
193
                }
194
 
194
            }
195
            $this->execute_script($js);
Línea 195... Línea 196...
195
 
196
            $this->wait_for_pending_js();
Línea 832... Línea 833...
832
                "The select menu should contain \"{$option}\" but it does not.",
833
                "The select menu should contain \"{$option}\" but it does not.",
833
                $this->getSession()
834
                $this->getSession()
834
            );
835
            );
835
        }
836
        }
836
    }
837
    }
-
 
838
 
-
 
839
    /**
-
 
840
     * Check that the validationMessage property on a form field element includes the given text.
-
 
841
     *
-
 
842
     * @Then the :field field validation message should contain :text
-
 
843
     * @param string $field The css selector for the input field
-
 
844
     * @param string $text The text which should be found in the validation message
-
 
845
     */
-
 
846
    public function the_field_validation_message_should_contain(string $field, string $text): void {
-
 
847
 
-
 
848
        // We can't use this assertion if javascript is not running.
-
 
849
        $this->require_javascript();
-
 
850
 
-
 
851
        // Check that the element exists.
-
 
852
        // This is fail and go no further, if the element does not exist.
-
 
853
        $node = $this->get_selected_node('field', $field);
-
 
854
 
-
 
855
        // Get the validity result.
-
 
856
        $wdelement = $this->get_webdriver_element_from_node_element($node);
-
 
857
        $webdriver = $this->getSession()->getDriver()->getWebDriver();
-
 
858
        $message = $webdriver->executeScript("return arguments[0].validationMessage;", [$wdelement]);
-
 
859
        if (strpos($message, $text) === false) {
-
 
860
            throw new ExpectationException(
-
 
861
                '"' . $field . '" validation message does not contain "' . $text . '"', $this->getSession()
-
 
862
            );
-
 
863
        }
-
 
864
 
-
 
865
    }
-
 
866
 
-
 
867
    /**
-
 
868
     * Check that the result of calling the checkValidity API on a form field element matches the expected result.
-
 
869
     *
-
 
870
     * @Then the :field field validity check should return :result
-
 
871
     * @param string $field The css selector for the input field
-
 
872
     * @param string $expected "true" or "false"
-
 
873
     */
-
 
874
    public function the_field_validity_check_should_return(string $field, string $expected): void {
-
 
875
 
-
 
876
        // We can't use this assertion if javascript is not running.
-
 
877
        $this->require_javascript();
-
 
878
 
-
 
879
        // Expected value can only be 'true' or 'false'.
-
 
880
        $expected = strtolower($expected);
-
 
881
        if (!in_array($expected, ['true', 'false'])) {
-
 
882
            throw new ExpectationException(
-
 
883
                'Invalid value for expected value "' . $expected . '". Should be "true" or "false".',
-
 
884
                $this->getSession());
-
 
885
        }
-
 
886
 
-
 
887
        // Convert the expected result from a string to bool.
-
 
888
        $expected = ($expected === "true");
-
 
889
 
-
 
890
        // Check that the element exists.
-
 
891
        // This is fail and go no further, if the element does not exist.
-
 
892
        $node = $this->get_selected_node('field', $field);
-
 
893
 
-
 
894
        // Get the validity result.
-
 
895
        $wdelement = $this->get_webdriver_element_from_node_element($node);
-
 
896
        $webdriver = $this->getSession()->getDriver()->getWebDriver();
-
 
897
        $result = $webdriver->executeScript("return arguments[0].checkValidity();", [$wdelement]);
-
 
898
        if ($result !== $expected) {
-
 
899
 
-
 
900
            // Convert booleans to strings for the exception message.
-
 
901
            $result = ($result) ? "true" : "false";
-
 
902
            $expected = ($expected) ? "true" : "false";
-
 
903
 
-
 
904
            throw new ExpectationException(
-
 
905
                '"' . $field . '" validation check was "' . $result . '". Expected: "' .
-
 
906
                $expected . '"', $this->getSession()
-
 
907
            );
-
 
908
 
-
 
909
        }
-
 
910
 
-
 
911
    }
-
 
912
 
837
}
913
}