Skip to content

Commit 17d0602

Browse files
committed
MOBILE-4842 behat: Internationalize standard button presses
1 parent f1652e0 commit 17d0602

File tree

8 files changed

+146
-51
lines changed

8 files changed

+146
-51
lines changed

local_moodleappbehat/tests/behat/behat_app.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ public function i_enter_the_activity_in_the_app(string $activity, string $activi
476476
/**
477477
* Presses standard buttons in the app.
478478
*
479-
* @When /^I press the (back|more menu|page menu|user menu) button in the app$/
479+
* @When /^I press the (back|more menu|page context menu|user menu) button in the app$/
480480
* @param string $button Button type
481481
* @throws DriverException If the button push doesn't work
482482
*/
@@ -485,7 +485,30 @@ public function i_press_the_standard_button_in_the_app(string $button) {
485485
$result = $this->runtime_js("pressStandard('$button')");
486486

487487
if ($result !== 'OK') {
488-
throw new DriverException('Error pressing standard button - ' . $result);
488+
throw new DriverException("Error pressing $button button - $result");
489+
}
490+
491+
return true;
492+
});
493+
494+
$this->wait_for_pending_js();
495+
}
496+
497+
/**
498+
* Presses deprecated page menu button in the app.
499+
*
500+
* @When /^I press the page menu button in the app$/
501+
* @throws DriverException If the button push doesn't work
502+
* @deprecated since 5.1 Use "I press the page context menu button in the app" instead.
503+
*/
504+
public function i_press_the_page_menu_button_in_the_app() {
505+
$button = 'page context menu';
506+
507+
$this->spin(function() use ($button) {
508+
$result = $this->runtime_js("pressStandard('$button')");
509+
510+
if ($result !== 'OK') {
511+
throw new DriverException('Error pressing deprecated page menu button - ' . $result);
489512
}
490513

491514
return true;

src/addons/calendar/tests/behat/create_events.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Feature: Test creation of calendar events in app
5757
And I should find "Barcelona" in the app
5858
But I should not find "Ending time" in the app
5959

60-
When I press "Display options" in the app
60+
When I press the page context menu button in the app
6161
Then I should find "Edit" in the app
6262
And I should find "Delete" in the app
6363

src/addons/competency/tests/behat/navigation.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ Feature: Test competency navigation
240240
And I set the field "Search" to "student" in the app
241241
And I press "Search" "button" in the app
242242
And I press "Student first" in the app
243-
And I press "Display options" in the app
243+
And I press the page context menu button in the app
244244
And I press "User info" in the app
245245
And I press "Learning plans" in the app
246246
Then I should find "Cookery" in the app
@@ -428,7 +428,7 @@ Feature: Test competency navigation
428428
And I press "Search people and messages" in the app
429429
And I set the field "Search" to "student" in the app
430430
And I press "Search" "button" in the app
431-
And I press "Display options" in the app
431+
And I press the page context menu button in the app
432432
And I press "User info" in the app
433433
And I press "Learning plans" in the app
434434
Then I should find "Cookery is important" in the app

src/addons/messages/tests/behat/basic_usage.feature

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Feature: Test basic usage of messages in app
3131
And I press "Student1 student1" in the app
3232
And I set the field "New message" to "heeey student" in the app
3333
And I press "Send" in the app
34-
And I press "Display options" in the app
34+
And I press the page context menu button in the app
3535
And I press "Add to contacts" in the app
3636
And I press "Add" near "Are you sure you want to add Student1 student1 to your contacts?" in the app
3737
Then I should find "Contact request sent" in the app
@@ -133,7 +133,7 @@ Feature: Test basic usage of messages in app
133133
And I press "Send" in the app
134134
Then I should find "heeey student" in the app
135135

136-
When I press "Display options" in the app
136+
When I press the page context menu button in the app
137137
And I press "Add to contacts" in the app
138138
And I press "Add" in the app
139139
Then I should find "Contact request sent" in the app
@@ -148,20 +148,20 @@ Feature: Test basic usage of messages in app
148148
When I press "Accept and add to contacts" in the app
149149
Then I should not find "Teacher teacher would like to contact you" in the app
150150

151-
When I press "Display options" in the app
151+
When I press the page context menu button in the app
152152
And I press "User info" in the app
153153
And I press "Message" in the app
154154
And I set the field "New message" to "hi" in the app
155155
And I press "Send" "button" in the app
156156
Then I should find "heeey student" in the app
157157
And I should find "hi" in the app
158158

159-
When I press "Display options" in the app
159+
When I press the page context menu button in the app
160160
And I press "Remove from contacts" in the app
161161
And I press "Remove" in the app
162162
And I wait loading to finish in the app
163163
And I go back 2 times in the app
164-
And I press "Display options" in the app
164+
And I press the page context menu button in the app
165165
Then I should find "Add to contacts" in the app
166166

167167
When I press "Delete conversation" in the app
@@ -276,7 +276,7 @@ Feature: Test basic usage of messages in app
276276
And I should find "Starred (1)" in the app
277277

278278
When I press "star message" in the app
279-
And I press "Display options" in the app
279+
And I press the page context menu button in the app
280280
And I press "Star conversation" in the app
281281
And I go back in the app
282282
Then I should find "Private (1)" in the app
@@ -291,7 +291,7 @@ Feature: Test basic usage of messages in app
291291
When I press "Participants" in the app
292292
And I press "Student1 student1" in the app
293293
And I press "Message" in the app
294-
And I press "Display options" in the app
294+
And I press the page context menu button in the app
295295
And I press "Block user" in the app
296296
And I press "Block user" near "Are you sure you want to block Student1 student1?" in the app
297297
Then I should find "You have blocked this user" in the app
@@ -306,7 +306,7 @@ Feature: Test basic usage of messages in app
306306
When I press "Participants" in the app
307307
And I press "Student1 student1" in the app
308308
And I press "Message" in the app
309-
And I press "Display options" in the app
309+
And I press the page context menu button in the app
310310
Then I should find "Unblock user" in the app
311311
But I should not find "Block user" in the app
312312

@@ -332,15 +332,15 @@ Feature: Test basic usage of messages in app
332332
And I press "Send" in the app
333333
Then I should find "test message" in the app
334334

335-
When I press "Display options" in the app
335+
When I press the page context menu button in the app
336336
And I press "Mute" in the app
337337
Then I should find "Muted conversation" in the app
338338

339-
When I press "Display options" in the app
339+
When I press the page context menu button in the app
340340
And I press "Unmute" in the app
341341
Then I should not find "Muted conversation" in the app
342342

343-
When I press "Display options" in the app
343+
When I press the page context menu button in the app
344344
When I press "Mute" in the app
345345
Then I should find "Muted conversation" in the app
346346

@@ -369,12 +369,12 @@ Feature: Test basic usage of messages in app
369369
When I switch network connection to wifi
370370
And I go back in the app
371371
And I press "Student1 student1" in the app
372-
And I press "Display options" in the app
372+
And I press the page context menu button in the app
373373
Then I should find "Show delete messages" in the app
374374
And I should find "Delete conversation" in the app
375375

376376
When I press "Unstar conversation" in the app
377-
And I press "Display options" in the app
377+
And I press the page context menu button in the app
378378
Then I should find "Star conversation" in the app
379379
And I should find "Delete conversation" in the app
380380

@@ -388,7 +388,7 @@ Feature: Test basic usage of messages in app
388388
Then I should find "self conversation online" in the app
389389
But I should not find "self conversation offline" in the app
390390

391-
When I press "Display options" in the app
391+
When I press the page context menu button in the app
392392
And I press "Delete conversation" in the app
393393
And I press "Delete" near "Are you sure you would like to delete this entire personal conversation?" in the app
394394
Then I should not find "self conversation online" in the app

src/addons/messages/tests/behat/navigation.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ Feature: Test messages navigation in the app
2626
And I press "Send" in the app
2727
Then I should find "Hi there" in the app
2828

29-
When I press "Display options" in the app
29+
When I press the page context menu button in the app
3030
And I press "User info" in the app
3131
Then I should find "Details" in the app
3232

3333
When I press "Message" in the app
3434
Then I should find "Hi there" in the app
3535

36-
When I press "Display options" in the app
36+
When I press the page context menu button in the app
3737
Then I should not find "User info" in the app
3838

3939
When I close the popup in the app

src/core/features/comments/tests/behat/basic_usage.feature

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Feature: Test basic usage of comments in app
8484
When I go back in the app
8585
And I press "Comments (0)" in the app
8686
And I switch network connection to wifi
87-
And I press "Display options" in the app
87+
And I press the page context menu button in the app
8888
And I press "Synchronise now" in the app
8989
And I close the popup in the app
9090
Then I should find "comment test" in the app
@@ -104,7 +104,7 @@ Feature: Test basic usage of comments in app
104104
When I go back in the app
105105
And I press "Comments (1)" in the app
106106
And I switch network connection to wifi
107-
And I press "Display options" in the app
107+
And I press the page context menu button in the app
108108
And I press "Synchronise now" in the app
109109
And I close the popup in the app
110110
Then I should not see "There are offline comments to be synchronised."
@@ -171,7 +171,7 @@ Feature: Test basic usage of comments in app
171171
When I go back in the app
172172
And I press "Comments (0)" in the app
173173
And I switch network connection to wifi
174-
And I press "Display options" in the app
174+
And I press the page context menu button in the app
175175
And I press "Synchronise now" in the app
176176
And I close the popup in the app
177177
Then I should find "comment test" in the app
@@ -191,7 +191,7 @@ Feature: Test basic usage of comments in app
191191
When I go back in the app
192192
And I press "Comments (1)" in the app
193193
And I switch network connection to wifi
194-
And I press "Display options" in the app
194+
And I press the page context menu button in the app
195195
And I press "Synchronise now" in the app
196196
And I close the popup in the app
197197
Then I should not see "There are offline comments to be synchronised."
@@ -253,7 +253,7 @@ Feature: Test basic usage of comments in app
253253
When I go back in the app
254254
And I press "Comments (0)" in the app
255255
And I switch network connection to wifi
256-
And I press "Display options" in the app
256+
And I press the page context menu button in the app
257257
And I press "Synchronise now" in the app
258258
And I close the popup in the app
259259
Then I should find "comment test" in the app
@@ -273,7 +273,7 @@ Feature: Test basic usage of comments in app
273273
When I go back in the app
274274
And I press "Comments (1)" in the app
275275
And I switch network connection to wifi
276-
And I press "Display options" in the app
276+
And I press the page context menu button in the app
277277
And I press "Synchronise now" in the app
278278
And I close the popup in the app
279279
Then I should not see "There are offline comments to be synchronised."

src/testing/services/behat-dom.ts

Lines changed: 84 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -373,11 +373,11 @@ export class TestingBehatDomUtilsService {
373373
* Get closest element matching a selector, without traversing up a given container.
374374
*
375375
* @param element Element.
376-
* @param selector Selector.
376+
* @param selector Ancestor selector.
377377
* @param container Topmost container to search within.
378378
* @returns Closest matching element.
379379
*/
380-
protected getClosestMatching(element: HTMLElement, selector: string, container: HTMLElement | null): HTMLElement | null {
380+
protected getClosestMatching(element: HTMLElement, selector: string, container: HTMLElement): HTMLElement | null {
381381
if (element.matches(selector)) {
382382
return element;
383383
}
@@ -390,6 +390,27 @@ export class TestingBehatDomUtilsService {
390390
return this.getClosestMatching(parent, selector, container);
391391
}
392392

393+
/**
394+
* For a list of elements, get the closest matching ancestor for each of them, if matches.
395+
*
396+
* @param elements List of elements.
397+
* @param selector Ancestor selector.
398+
* @param container Topmost container to search within.
399+
* @returns Closest matching elements.
400+
*/
401+
protected getClosestMatchingElements(elements: HTMLElement[], selector: string, container: HTMLElement): HTMLElement[] {
402+
const closestMatchingElements: HTMLElement[] = [];
403+
404+
elements.forEach((element) => {
405+
const closest = this.getClosestMatching(element, selector, container);
406+
if (closest) {
407+
closestMatchingElements.push(closest);
408+
}
409+
});
410+
411+
return closestMatchingElements;
412+
}
413+
393414
/**
394415
* Function to find top container elements.
395416
*
@@ -520,6 +541,29 @@ export class TestingBehatDomUtilsService {
520541
}
521542
}
522543

544+
/**
545+
* Function to find element based on a CSS selector.
546+
*
547+
* @param selector Element selector.
548+
* @param options Search options.
549+
* @returns First found element.
550+
*/
551+
findElementBasedOnSelector(
552+
selector: string,
553+
options: TestingBehatFindOptions = {},
554+
): HTMLElement | undefined {
555+
const topContainers = this.getCurrentTopContainerElements(options.containerName);
556+
let elements: HTMLElement[] = [];
557+
558+
topContainers.some((container) => {
559+
elements = this.findElementsBasedOnSelectorInContainer(selector, container, options);
560+
561+
return elements.length > 0;
562+
});
563+
564+
return elements[0];
565+
}
566+
523567
/**
524568
* Function to find element based on their text or Aria label.
525569
*
@@ -672,20 +716,49 @@ export class TestingBehatDomUtilsService {
672716

673717
const elements = this.findElementsBasedOnTextWithin(container, locator.text, options);
674718

675-
let filteredElements: HTMLElement[] = elements;
719+
let filteredElements = elements;
676720

677721
if (locator.selector) {
678-
filteredElements = [];
679-
const selector = locator.selector;
722+
filteredElements = this.getClosestMatchingElements(elements, locator.selector, container);
723+
}
680724

681-
elements.forEach((element) => {
682-
const closest = this.getClosestMatching(element, selector, container);
683-
if (closest) {
684-
filteredElements.push(closest);
685-
}
686-
});
725+
if (filteredElements.length > 0) {
726+
return filteredElements;
687727
}
688728

729+
} while (container !== topContainer && (container = this.getParentElement(container)) && container !== topContainer);
730+
731+
return [];
732+
}
733+
734+
/**
735+
* Function to find elements based on a CSS selector.
736+
*
737+
* @param selector Element selector.
738+
* @param topContainer Container to search in.
739+
* @param options Search options.
740+
* @returns Found elements
741+
*/
742+
protected findElementsBasedOnSelectorInContainer(
743+
selector: string,
744+
topContainer: HTMLElement,
745+
options: TestingBehatFindOptions = {},
746+
): HTMLElement[] {
747+
let container: HTMLElement | null = topContainer;
748+
749+
do {
750+
if (!container) {
751+
break;
752+
}
753+
754+
const elements = Array.from(container.querySelectorAll<HTMLElement>(selector))
755+
.filter(
756+
element => this.isElementVisible(element, topContainer) &&
757+
(!options.onlyClickable || this.isElementClickable(element)),
758+
);
759+
760+
const filteredElements = this.getClosestMatchingElements(elements, selector, container);
761+
689762
if (filteredElements.length > 0) {
690763
return filteredElements;
691764
}

0 commit comments

Comments
 (0)