|
| 1 | +import {browser, Key, element, by} from 'protractor'; |
| 2 | +import {screenshot} from '../../screenshot'; |
| 3 | +import {getScrollPosition} from '../../util/query'; |
| 4 | + |
| 5 | + |
| 6 | +describe('scroll blocking', () => { |
| 7 | + beforeEach(() => browser.get('/block-scroll-strategy')); |
| 8 | + afterEach(() => clickOn('disable')); |
| 9 | + |
| 10 | + it('should not be able to scroll programmatically along the x axis', async (done) => { |
| 11 | + scrollPage(0, 100); |
| 12 | + expect((await getScrollPosition()).y).toBe(100, 'Expected the page to be scrollable.'); |
| 13 | + |
| 14 | + clickOn('enable'); |
| 15 | + scrollPage(0, 200); |
| 16 | + expect((await getScrollPosition()).y).toBe(100, 'Expected the page not to be scrollable.'); |
| 17 | + |
| 18 | + clickOn('disable'); |
| 19 | + scrollPage(0, 300); |
| 20 | + expect((await getScrollPosition()).y).toBe(300, 'Exected page to be scrollable again.'); |
| 21 | + |
| 22 | + screenshot(); |
| 23 | + done(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should not be able to scroll programmatically along the y axis', async (done) => { |
| 27 | + scrollPage(100, 0); |
| 28 | + expect((await getScrollPosition()).x).toBe(100, 'Expected the page to be scrollable.'); |
| 29 | + |
| 30 | + clickOn('enable'); |
| 31 | + scrollPage(200, 0); |
| 32 | + expect((await getScrollPosition()).x).toBe(100, 'Expected the page not to be scrollable.'); |
| 33 | + |
| 34 | + clickOn('disable'); |
| 35 | + scrollPage(300, 0); |
| 36 | + expect((await getScrollPosition()).x).toBe(300, 'Exected page to be scrollable again.'); |
| 37 | + |
| 38 | + screenshot(); |
| 39 | + done(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should not be able to scroll via the keyboard along the y axis', async (done) => { |
| 43 | + const body = element(by.tagName('body')); |
| 44 | + |
| 45 | + scrollPage(0, 100); |
| 46 | + expect((await getScrollPosition()).y).toBe(100, 'Expected the page to be scrollable.'); |
| 47 | + |
| 48 | + clickOn('enable'); |
| 49 | + await body.sendKeys(Key.ARROW_DOWN); |
| 50 | + await body.sendKeys(Key.ARROW_DOWN); |
| 51 | + await body.sendKeys(Key.ARROW_DOWN); |
| 52 | + expect((await getScrollPosition()).y).toBe(100, 'Expected the page not to be scrollable.'); |
| 53 | + |
| 54 | + clickOn('disable'); |
| 55 | + await body.sendKeys(Key.ARROW_DOWN); |
| 56 | + await body.sendKeys(Key.ARROW_DOWN); |
| 57 | + await body.sendKeys(Key.ARROW_DOWN); |
| 58 | + expect((await getScrollPosition()).y) |
| 59 | + .toBeGreaterThan(100, 'Expected the page to be scrollable again.'); |
| 60 | + |
| 61 | + screenshot(); |
| 62 | + done(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should not be able to scroll via the keyboard along the x axis', async (done) => { |
| 66 | + const body = element(by.tagName('body')); |
| 67 | + |
| 68 | + scrollPage(100, 0); |
| 69 | + expect((await getScrollPosition()).x).toBe(100, 'Expected the page to be scrollable.'); |
| 70 | + |
| 71 | + clickOn('enable'); |
| 72 | + await body.sendKeys(Key.ARROW_RIGHT); |
| 73 | + await body.sendKeys(Key.ARROW_RIGHT); |
| 74 | + await body.sendKeys(Key.ARROW_RIGHT); |
| 75 | + expect((await getScrollPosition()).x).toBe(100, 'Expected the page not to be scrollable.'); |
| 76 | + |
| 77 | + clickOn('disable'); |
| 78 | + await body.sendKeys(Key.ARROW_RIGHT); |
| 79 | + await body.sendKeys(Key.ARROW_RIGHT); |
| 80 | + await body.sendKeys(Key.ARROW_RIGHT); |
| 81 | + expect((await getScrollPosition()).x) |
| 82 | + .toBeGreaterThan(100, 'Expected the page to be scrollable again.'); |
| 83 | + |
| 84 | + screenshot(); |
| 85 | + done(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should not be able to scroll the page after reaching the end of an element along the y axis', |
| 89 | + async (done) => { |
| 90 | + const scroller = element(by.id('scroller')); |
| 91 | + |
| 92 | + browser.executeScript(`document.getElementById('scroller').scrollTop = 200;`); |
| 93 | + scrollPage(0, 100); |
| 94 | + expect((await getScrollPosition()).y).toBe(100, 'Expected the page to be scrollable.'); |
| 95 | + |
| 96 | + clickOn('enable'); |
| 97 | + scroller.sendKeys(Key.ARROW_DOWN); |
| 98 | + scroller.sendKeys(Key.ARROW_DOWN); |
| 99 | + scroller.sendKeys(Key.ARROW_DOWN); |
| 100 | + expect((await getScrollPosition()).y).toBe(100, 'Expected the page not to have scrolled.'); |
| 101 | + |
| 102 | + screenshot(); |
| 103 | + done(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should not be able to scroll the page after reaching the end of an element along the x axis', |
| 107 | + async (done) => { |
| 108 | + const scroller = element(by.id('scroller')); |
| 109 | + |
| 110 | + browser.executeScript(`document.getElementById('scroller').scrollLeft = 200;`); |
| 111 | + scrollPage(100, 0); |
| 112 | + expect((await getScrollPosition()).x).toBe(100, 'Expected the page to be scrollable.'); |
| 113 | + |
| 114 | + clickOn('enable'); |
| 115 | + scroller.sendKeys(Key.ARROW_RIGHT); |
| 116 | + scroller.sendKeys(Key.ARROW_RIGHT); |
| 117 | + scroller.sendKeys(Key.ARROW_RIGHT); |
| 118 | + expect((await getScrollPosition()).x).toBe(100, 'Expected the page not to have scrolled.'); |
| 119 | + |
| 120 | + screenshot(); |
| 121 | + done(); |
| 122 | + }); |
| 123 | +}); |
| 124 | + |
| 125 | +// Clicks on a button programmatically. Note that we can't use Protractor's `.click`, because |
| 126 | +// it performs a real click, which will scroll the button into view. |
| 127 | +function clickOn(id: string) { |
| 128 | + browser.executeScript(`document.getElementById('${id}').click()`); |
| 129 | +} |
| 130 | + |
| 131 | +// Scrolls the page to the specified coordinates. |
| 132 | +function scrollPage(x: number, y: number) { |
| 133 | + return browser.executeScript(`window.scrollTo(${x}, ${y});`); |
| 134 | +} |
0 commit comments