Skip to content

Commit

Permalink
Restructure tests to run faster in parallel. All tests are passing.
Browse files Browse the repository at this point in the history
  • Loading branch information
l-you committed Jan 22, 2024
1 parent 4b15d1e commit 8d7237e
Showing 1 changed file with 41 additions and 37 deletions.
78 changes: 41 additions & 37 deletions __tests__/ScrollRestorer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {test, expect,} from '@playwright/test'
import {test, expect, Page,} from '@playwright/test'

const highPage = 1300
const mainPage = 2600
Expand All @@ -9,105 +9,110 @@ const resolveTimeout = (time: number) => (async () => {
}, time)
})
})()
test('End to end testing of scroll restorer', async ({page, browserName}) => {
const getScrollY = (page:Page) => page.evaluate((): number => window.scrollY)

const expectScrollToBe = async (page:Page,value: number) => {
await resolveTimeout(50)
await expect(getScrollY(page)).resolves.toBeGreaterThanOrEqual(value - 2)
await expect(getScrollY(page)).resolves.toBeLessThanOrEqual(value + 2)
}
const initTests = async (page:Page)=>{
// Start from the index page (the baseURL is set via the webServer in the playwright.config.ts)
page.on('console', (msg) => {
console.log(`${msg.type()}: ${msg.text()}`)
})

await page.goto('/')
// Find an element with the text 'About' and click on it
// Default behavior to start.
const el = page.getByText('Lets-go to low-page')
const getScrollY = () => page.evaluate((): number => window.scrollY)
const expectScrollToBe = async (value: number) => {
await resolveTimeout(50)
await expect(getScrollY()).resolves.toBeGreaterThanOrEqual(value - 2)
await expect(getScrollY()).resolves.toBeLessThanOrEqual(value + 2)
}
await expectScrollToBe(0)
await expectScrollToBe(page,0)
await el.scrollIntoViewIfNeeded()
await expectScrollToBe(mainPage)
await expectScrollToBe(page,mainPage)
await el.click()

// The new URL should be "/about" (baseURL is used there)
await expect(page).toHaveURL('/low-page')
// The new page should contain an h1 with "About"
await expect(page.locator('h1')).toContainText('This is the low page')
await expectScrollToBe(0)
await expectScrollToBe(page,0)
await page.goBack()
}
test('End to end testing of scroll restorer', async ({page, browserName}) => {

await initTests(page)
await expect(page).toHaveURL('/')
await resolveTimeout(1000) //Check if Next.js does not brake scroll position later


await expectScrollToBe(mainPage)
await expectScrollToBe(page,mainPage)
await page.goForward()
await expectScrollToBe(0)
await expectScrollToBe(page,0)
//A little bit of stress for app
await page.goBack()

await expectScrollToBe(mainPage)
await expectScrollToBe(page,mainPage)
for (let i = 0; i < 10; i++) {
await page.goForward()
await resolveTimeout(10)//Sometimes browsers struggle to restore the same millisecond
await page.goBack()
}

await expectScrollToBe(mainPage)
await expectScrollToBe(page,mainPage)
await page.goForward()

await expectScrollToBe(0)
await expectScrollToBe(page,0)
await page.goBack()
await expectScrollToBe(mainPage)
await expectScrollToBe(page,mainPage)

//End of a little bit of stress for app

//Test when both pages are high
await page.goto('/high')
await expectScrollToBe(0)
await expectScrollToBe(page,0)

const mainEl = page.getByText('Lets-go to main')
await mainEl.scrollIntoViewIfNeeded()
await page.waitForURL('/high')
await expectScrollToBe(highPage)
await expectScrollToBe(page,highPage)
await mainEl.click()
await expectScrollToBe(0)
await expectScrollToBe(page,0)
await page.waitForURL('/')


await page.getByText('Lets-go to low-page').scrollIntoViewIfNeeded()
await expectScrollToBe(mainPage)
await expectScrollToBe(page,mainPage)

await page.getByText('Lets-go to low-page').click()

await expectScrollToBe(0)
await expectScrollToBe(page,0)
await page.waitForURL('/low-page')

await page.goBack()

await expectScrollToBe(mainPage)
await expectScrollToBe(page,mainPage)
await page.waitForURL('/')
await page.goBack()
await expectScrollToBe(highPage)
await expectScrollToBe(page,highPage)
await page.waitForURL('/high')
await page.goForward()
await page.waitForURL('/')
await expectScrollToBe(mainPage)
await expectScrollToBe(page,mainPage)


await page.reload()
if (browserName === "firefox") {
await page.goBack() //Firefox pushed new history entry history after reload https://github.com/microsoft/playwright/issues/22640
}
await resolveTimeout(1000)//Sometimes browsers struggle to restore the same millisecond
await expectScrollToBe(mainPage)
await expectScrollToBe(page,mainPage)

})

//Test for a scroll to not produce any errors. https://github.com/sveltejs/kit/issues/365
test('Smooth scrolling',async ({page}) => {
await initTests(page)

//Test for a scroll to not produce any errors. Https://github.com/sveltejs/kit/issues/365
let error: string | null = null
page.on("console", (msg) => {
if (msg.type() === 'error') {
error = msg.text()
}
})

await resolveTimeout(2000) // Timeout for safari scroll bug navigation workaround.

const smoothScrollTop = async () => {
Expand All @@ -128,7 +133,7 @@ test('End to end testing of scroll restorer', async ({page, browserName}) => {
polling: 500

})
await expectScrollToBe(0)
await expectScrollToBe(page,0)
}
const smoothScrollBottom = async () => {
console.log('Scrolling to bottom.')
Expand All @@ -144,7 +149,7 @@ test('End to end testing of scroll restorer', async ({page, browserName}) => {
polling: 500
})

await expectScrollToBe(mainPage)
await expectScrollToBe(page,mainPage)
}
//This is a very important test to stress out 'scroll' event
// DO NOT CHANGE THIS TEST IN ANY WAY!
Expand All @@ -155,5 +160,4 @@ test('End to end testing of scroll restorer', async ({page, browserName}) => {
}

expect(error).toBe(null)

})

0 comments on commit 8d7237e

Please sign in to comment.