@@ -551,14 +551,49 @@ public function i_press_in_the_app($text, $near='') {
551551 * with JavaScript, and clicks may not work until they are initialized properly which may cause flaky tests due
552552 * to race conditions.
553553 *
554- * @Then /^I select "(?P<text_string>(?:[^"]|\\")*)"(?: near "(?P<near_string>(?:[^"]|\\")*)")? in the app$/
554+ * @Then /^I (?P<select_string>unselect|select) "(?P<text_string>(?:[^"]|\\")*)"(?: near "(?P<near_string>(?:[^"]|\\")*)")? in the app$/
555+ * @param string $selectedtext Select/unselect string
555556 * @param string $text Text identifying click target
556557 * @param string $near Text identifying a nearby unique piece of text
557558 * @throws DriverException If the press doesn't work
558559 */
559- public function i_select_in_the_app ($ text , $ near ='' ) {
560- $ this ->getSession ()->wait (100 );
561- $ this ->press ($ text , $ near );
560+ public function i_select_in_the_app (string $ selectedtext , string $ text , string $ near = '' ) {
561+ $ selected = $ selectedtext === 'select ' ? 'YES ' : 'NO ' ;
562+ $ text = addslashes_js ($ text );
563+ $ near = addslashes_js ($ near );
564+
565+ $ this ->spin (function () use ($ selectedtext , $ selected , $ text , $ near ) {
566+ // Don't do anything if the item is already in the expected state.
567+ $ result = $ this ->evaluate_script ("return window.behat.isSelected( \"$ text \", \"$ near \"); " );
568+
569+ if ($ result === $ selected ) {
570+ return true ;
571+ }
572+
573+ // Press item.
574+ $ result = $ this ->evaluate_script ("return window.behat.press( \"$ text \", \"$ near \"); " );
575+
576+ if ($ result !== 'OK ' ) {
577+ throw new DriverException ('Error pressing item - ' . $ result );
578+ }
579+
580+ // Check that it worked as expected.
581+ $ result = $ this ->evaluate_script ("return window.behat.isSelected( \"$ text \", \"$ near \"); " );
582+
583+ switch ($ result ) {
584+ case 'YES ' :
585+ case 'NO ' :
586+ if ($ result !== $ selected ) {
587+ throw new ExpectationException ("Item wasn't $ selectedtext after pressing it " , $ this ->getSession ()->getDriver ());
588+ }
589+
590+ return true ;
591+ default :
592+ throw new DriverException ('Error finding item - ' . $ result );
593+ }
594+ });
595+
596+ $ this ->wait_for_pending_js ();
562597 }
563598
564599 /**
@@ -584,17 +619,16 @@ protected function is_in_login_page(): bool {
584619 * @throws DriverException If the press doesn't work
585620 */
586621 protected function press (string $ text , string $ near = '' ) {
587- $ this ->spin (function ($ context , $ args ) use ($ text , $ near ) {
588- if ($ near !== '' ) {
589- $ nearbit = ', " ' . addslashes_js ($ near ) . '" ' ;
590- } else {
591- $ nearbit = '' ;
592- }
593- $ result = $ this ->evaluate_script ('return window.behat.press(" ' .
594- addslashes_js ($ text ) . '" ' . $ nearbit .'); ' );
622+ $ text = addslashes_js ($ text );
623+ $ near = addslashes_js ($ near );
624+
625+ $ this ->spin (function () use ($ text , $ near ) {
626+ $ result = $ this ->evaluate_script ("return window.behat.press( \"$ text \", \"$ near \"); " );
627+
595628 if ($ result !== 'OK ' ) {
596629 throw new DriverException ('Error pressing item - ' . $ result );
597630 }
631+
598632 return true ;
599633 });
600634 $ this ->wait_for_pending_js ();
0 commit comments