Skip to content
This repository has been archived by the owner on Nov 7, 2022. It is now read-only.

Commit

Permalink
test: update to always get new cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
z4cki committed Aug 1, 2022
1 parent a27d176 commit 87c1d72
Showing 1 changed file with 35 additions and 38 deletions.
73 changes: 35 additions & 38 deletions tools/e2e-testing/global-setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { BrowserType, chromium, firefox, request, webkit } from '@playwright/test'
import path from 'path'
import fs from 'fs'

const browserList: { [key: string]: BrowserType } = {
chromium,
Expand All @@ -16,43 +15,41 @@ async function globalSetup(): Promise<void> {
const userParams = `name=${project}-${module}&email=${project}-${module}@mashcard.com`
const fileName = path.join(`./storage/${project}-${module}.json`)

if (!fs.existsSync(fileName)) {
const requestContext = await request.newContext()
const response = await requestContext.post(`${baseURL}$internal-apis/accounts/auth/developer/callback`, {
data: userParams,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})

await requestContext.storageState({ path: fileName })
await requestContext.dispose()

// When the user not exist, will redirect to sign up page to create it.
if (response.url().includes('/accounts/sign-up')) {
const browser = await browserList[project].launch()
const context = await browser.newContext({ baseURL, storageState: fileName })
const page = await context.newPage()
try {
await context.tracing.start({ screenshots: true, snapshots: true })

await page.goto('/accounts/sign-up')
await page.waitForSelector('main div h1:has-text("Sign Up with Developer")')
await page.locator('button[type="submit"]').click()
await page.waitForNavigation()
await page.context().storageState({ path: fileName })

await context.tracing.stop({
path: './test-results/setup-trace.zip'
})
await page.close()
} catch (error) {
await context.tracing.stop({
path: './test-results/failed-setup-trace.zip'
})
await page.close()
throw error
}
const requestContext = await request.newContext()
const response = await requestContext.post(`${baseURL}$internal-apis/accounts/auth/developer/callback`, {
data: userParams,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})

await requestContext.storageState({ path: fileName })
await requestContext.dispose()

// When the user not exist, will redirect to sign up page to create it.
if (response.url().includes('/accounts/sign-up')) {
const browser = await browserList[project].launch()
const context = await browser.newContext({ baseURL, storageState: fileName })
const page = await context.newPage()
try {
await context.tracing.start({ screenshots: true, snapshots: true })

await page.goto('/accounts/sign-up')
await page.waitForSelector('main div h1:has-text("Sign Up with Developer")')
await page.locator('button[type="submit"]').click()
await page.waitForNavigation()
await page.context().storageState({ path: fileName })

await context.tracing.stop({
path: './test-results/setup-trace.zip'
})
await page.close()
} catch (error) {
await context.tracing.stop({
path: './test-results/failed-setup-trace.zip'
})
await page.close()
throw error
}
}
}
Expand Down

0 comments on commit 87c1d72

Please sign in to comment.