From ef0d747f16922d3f408d88055a45c42b4c9fdbfe Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 9 Nov 2022 11:34:13 +0100 Subject: [PATCH] MOBILE-4081 course: Improve behat tests for hidden courses --- .../courses/tests/behat/basic_usage.feature | 6 +++-- src/testing/services/behat-dom.ts | 27 +++++++++++++++---- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/core/features/courses/tests/behat/basic_usage.feature b/src/core/features/courses/tests/behat/basic_usage.feature index ff9f939c2..59034c4ca 100755 --- a/src/core/features/courses/tests/behat/basic_usage.feature +++ b/src/core/features/courses/tests/behat/basic_usage.feature @@ -56,13 +56,15 @@ Feature: Test basic usage of courses in app Scenario: Hidden course is only accessible for teachers Given I entered the app as "teacher1" And I press "My courses" in the app + Then I should find "Hidden from students" within "Hidden course" "ion-item" in the app + When I press "Hidden course" in the app Then the header should be "Hidden course" in the app Given I entered the app as "student1" And I press "My courses" in the app - And I should not find "Hidden course" in the app - + Then I should not find "Hidden course" in the app + And I should not find "Hidden from students" in the app @lms_from4.0 Scenario: See my courses diff --git a/src/testing/services/behat-dom.ts b/src/testing/services/behat-dom.ts index f8bc231d6..aebcaafd3 100644 --- a/src/testing/services/behat-dom.ts +++ b/src/testing/services/behat-dom.ts @@ -24,18 +24,32 @@ import { TestingBehatElementLocator, TestingBehatFindOptions } from './behat-run @Injectable({ providedIn: 'root' }) export class TestingBehatDomUtilsService { + /** + * Check if an element is clickable. + * + * @param element Element. + * @return Whether the element is clickable or not. + */ + isElementClickable(element: HTMLElement): boolean { + return element.getAttribute('aria-disabled') !== 'true' && !element.hasAttribute('disabled'); + } + /** * Check if an element is visible. * * @param element Element. - * @param container Container. + * @param container Container. If set, the function will also check parent elements visibility. * @return Whether the element is visible or not. */ - isElementVisible(element: HTMLElement, container: HTMLElement): boolean { + isElementVisible(element: HTMLElement, container?: HTMLElement): boolean { if (element.getAttribute('aria-hidden') === 'true' || getComputedStyle(element).display === 'none') { return false; } + if (!container) { + return true; + } + const parentElement = this.getParentElement(element); if (parentElement === container) { return true; @@ -92,7 +106,10 @@ export class TestingBehatDomUtilsService { `img[alt*="${escapedText}"], [placeholder*="${escapedText}"]`; const elements = Array.from(container.querySelectorAll(attributesSelector)) - .filter((element => this.isElementVisible(element, container))) + .filter( + element => this.isElementVisible(element, container) && + (!options.onlyClickable || this.isElementClickable(element)), + ) .map((element) => { const exact = this.checkElementLabel(element, text); @@ -116,11 +133,11 @@ export class TestingBehatDomUtilsService { return NodeFilter.FILTER_ACCEPT; } - if (options.onlyClickable && (node.getAttribute('aria-disabled') === 'true' || node.hasAttribute('disabled'))) { + if (options.onlyClickable && !this.isElementClickable(node)) { return NodeFilter.FILTER_REJECT; } - if (node.getAttribute('aria-hidden') === 'true' || getComputedStyle(node).display === 'none') { + if (!this.isElementVisible(node)) { return NodeFilter.FILTER_REJECT; }