@@ -44,27 +44,21 @@ class behat_app extends behat_app_helper {
4444 ],
4545 ];
4646
47+ protected $ featurepath = '' ;
4748 protected $ windowsize = '360x720 ' ;
4849
4950 /**
5051 * @BeforeScenario
5152 */
5253 public function before_scenario (ScenarioScope $ scope ) {
53- if (!$ scope ->getFeature ()->hasTag ('app ' )) {
54- return ;
55- }
56-
57- global $ CFG ;
58-
59- $ performanceLogs = $ CFG ->behat_profiles ['default ' ]['capabilities ' ]['extra_capabilities ' ]['goog:loggingPrefs ' ]['performance ' ] ?? null ;
54+ $ feature = $ scope ->getFeature ();
6055
61- if ($ performanceLogs !== ' ALL ' ) {
56+ if (! $ feature -> hasTag ( ' app ' ) ) {
6257 return ;
6358 }
6459
65- // Enable DB Logging only for app tests with performance logs activated.
66- $ this ->getSession ()->visit ($ this ->get_app_url () . '/assets/env.json ' );
67- $ this ->execute_script ("document.cookie = 'MoodleAppDBLoggingEnabled=true;path=/'; " );
60+ $ this ->featurepath = dirname ($ feature ->getFile ());
61+ $ this ->configure_performance_logs ();
6862 }
6963
7064 /**
@@ -89,6 +83,23 @@ public function i_enter_the_app(string $username = null) {
8983 $ this ->enter_site ();
9084 }
9185
86+ /**
87+ * Configure performance logs.
88+ */
89+ protected function configure_performance_logs () {
90+ global $ CFG ;
91+
92+ $ performanceLogs = $ CFG ->behat_profiles ['default ' ]['capabilities ' ]['extra_capabilities ' ]['goog:loggingPrefs ' ]['performance ' ] ?? null ;
93+
94+ if ($ performanceLogs !== 'ALL ' ) {
95+ return ;
96+ }
97+
98+ // Enable DB Logging only for app tests with performance logs activated.
99+ $ this ->getSession ()->visit ($ this ->get_app_url () . '/assets/env.json ' );
100+ $ this ->execute_script ("document.cookie = 'MoodleAppDBLoggingEnabled=true;path=/'; " );
101+ }
102+
92103 /**
93104 * Check whether the current page is the login form.
94105 */
@@ -778,6 +789,35 @@ public function i_set_the_following_fields_to_these_values_in_the_app(TableNode
778789 }
779790 }
780791
792+ /**
793+ * Uploads a file to a file input, the file path should be relative to a fixtures folder next to the feature file.
794+ * The ìnput locator can match a container with a file input inside, it doesn't have to be the input itself.
795+ *
796+ * @Given /^I upload "((?:[^"]|\\")+)" to (".+") in the app$/
797+ * @param string $filename
798+ * @param string $inputlocator
799+ */
800+ public function i_upload_a_file_in_the_app (string $ filename , string $ inputlocator ) {
801+ $ filepath = str_replace ('/ ' , DIRECTORY_SEPARATOR , "{$ this ->featurepath }/fixtures/ $ filename " );
802+ $ inputlocator = $ this ->parse_element_locator ($ inputlocator );
803+
804+ $ id = $ this ->spin (function () use ($ inputlocator ) {
805+ $ result = $ this ->runtime_js ("getFileInputId( $ inputlocator) " );
806+
807+ if (str_starts_with ($ result , 'ERROR ' )) {
808+ throw new DriverException ('Error finding input - ' . $ result );
809+ }
810+
811+ return $ result ;
812+ });
813+
814+ $ this ->wait_for_pending_js ();
815+
816+ $ fileinput = $ this ->getSession ()->getPage ()->findById ($ id );
817+
818+ $ fileinput ->attachFile ($ filepath );
819+ }
820+
781821 /**
782822 * Checks a field matches a certain value in the app.
783823 *
0 commit comments