|
| 1 | +import ElementArrayFinder = protractor.ElementArrayFinder; |
| 2 | +import ElementFinder = protractor.ElementFinder; |
| 3 | + |
| 4 | +describe('tabs', () => { |
| 5 | + describe('basic behavior', () => { |
| 6 | + let tabGroup: ElementFinder; |
| 7 | + let tabLabels: ElementArrayFinder; |
| 8 | + let tabBodies: ElementArrayFinder; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + browser.get('/tabs'); |
| 12 | + tabGroup = element(by.css('md-tab-group')); |
| 13 | + tabLabels = element.all(by.css('.md-tab-label')); |
| 14 | + tabBodies = element.all(by.css('.md-tab-body')); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should change tabs when the label is clicked', () => { |
| 18 | + tabLabels.get(1).click(); |
| 19 | + expect(getActiveStates(tabLabels)).toEqual([false, true, false]); |
| 20 | + expect(getActiveStates(tabBodies)).toEqual([false, true, false]); |
| 21 | + |
| 22 | + tabLabels.get(0).click(); |
| 23 | + expect(getActiveStates(tabLabels)).toEqual([true, false, false]); |
| 24 | + expect(getActiveStates(tabBodies)).toEqual([true, false, false]); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should change focus with keyboard interaction', () => { |
| 28 | + tabLabels.get(0).click(); |
| 29 | + expect(getFocusStates(tabLabels)).toEqual([true, false, false]); |
| 30 | + |
| 31 | + pressKey(protractor.Key.RIGHT); |
| 32 | + expect(getFocusStates(tabLabels)).toEqual([false, true, false]); |
| 33 | + |
| 34 | + pressKey(protractor.Key.RIGHT); |
| 35 | + expect(getFocusStates(tabLabels)).toEqual([false, false, true]); |
| 36 | + |
| 37 | + pressKey(protractor.Key.RIGHT); |
| 38 | + expect(getFocusStates(tabLabels)).toEqual([false, false, true]); |
| 39 | + |
| 40 | + pressKey(protractor.Key.LEFT); |
| 41 | + expect(getFocusStates(tabLabels)).toEqual([false, true, false]); |
| 42 | + |
| 43 | + pressKey(protractor.Key.LEFT); |
| 44 | + expect(getFocusStates(tabLabels)).toEqual([true, false, false]); |
| 45 | + |
| 46 | + pressKey(protractor.Key.LEFT); |
| 47 | + expect(getFocusStates(tabLabels)).toEqual([true, false, false]); |
| 48 | + }); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +/** |
| 53 | + * A helper function to perform the sendKey action |
| 54 | + * @param key |
| 55 | + */ |
| 56 | +function pressKey(key: string) { |
| 57 | + browser.actions().sendKeys(key).perform(); |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Returns an array of true/false that represents the focus states of the provided elements |
| 62 | + * @param elements |
| 63 | + * @returns {webdriver.promise.Promise<Promise<boolean>[]>|webdriver.promise.Promise<T[]>} |
| 64 | + */ |
| 65 | +function getFocusStates(elements: ElementArrayFinder) { |
| 66 | + return elements.map(element => { |
| 67 | + return element.getText().then(elementText => { |
| 68 | + return browser.driver.switchTo().activeElement().getText().then(activeText => { |
| 69 | + return activeText === elementText; |
| 70 | + }); |
| 71 | + }); |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Returns an array of true/false that represents the active states for the provided elements |
| 77 | + * @param elements |
| 78 | + * @returns {webdriver.promise.Promise<Promise<boolean>[]>|webdriver.promise.Promise<T[]>} |
| 79 | + */ |
| 80 | +function getActiveStates(elements: ElementArrayFinder) { |
| 81 | + return getClassStates(elements, 'md-active'); |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Returns an array of true/false values that represents whether the provided className is on |
| 86 | + * each element |
| 87 | + * @param elements |
| 88 | + * @param className |
| 89 | + * @returns {webdriver.promise.Promise<Promise<boolean>[]>|webdriver.promise.Promise<T[]>} |
| 90 | + */ |
| 91 | +function getClassStates(elements: ElementArrayFinder, className: string) { |
| 92 | + return elements.map(element => { |
| 93 | + return element.getAttribute('class').then(classes => { |
| 94 | + return classes.split(/ +/g).indexOf(className) >= 0; |
| 95 | + }); |
| 96 | + }); |
| 97 | +} |
0 commit comments