Skip to content

Commit

Permalink
test: combine login + data creation in one step
Browse files Browse the repository at this point in the history
Hopefully that reduces test flakyness

RISDEV-0000
  • Loading branch information
andreasphil committed Feb 14, 2025
1 parent 9b646df commit f106742
Showing 1 changed file with 52 additions and 45 deletions.
97 changes: 52 additions & 45 deletions frontend/e2e/globalSetup/login-and-create-sample-data.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,56 @@ import { test as setup } from "@e2e/utils/test-with-auth"
import fs from "fs"
import path from "node:path"

setup("login", async ({ page }) => {
await page.goto("/")
await page.waitForURL(/localhost:8443/)

await page
.getByRole("textbox", { name: "Username or email" })
.fill("jane.doe")

await page.getByRole("textbox", { name: "Password" }).fill("test")

await page.getByRole("button", { name: "Sign In" }).click()

await page.context().storageState({ path: `e2e/storage/state.json` })

await page.waitForURL("/")
await page.unrouteAll({ behavior: "wait" })
})

setup("create sample data", async ({ authenticatedRequest: request }) => {
const files = [
"bgbl-1_1001_2_mods_01/aenderungsgesetz.xml",
"bgbl-1_1002_2_mods-subsitution_01/aenderungsgesetz.xml",
"bgbl-1_2017_s419/aenderungsgesetz.xml",
"bgbl-1_2023_413/aenderungsgesetz.xml",
]

for (const file of files) {
const filePath = path.join(samplesDirectory, file)
const fileContent = fs.readFileSync(filePath) // Read the file content

const formData = new FormData()
formData.append("file", new Blob([fileContent], { type: "text/xml" }), file)
formData.append("force", String(true))

const response = await request.post(
`${process.env.E2E_BASE_URL}/api/v1/announcements`,
{ multipart: formData },
)

if (!response.ok()) {
throw new Error(`Failed to set up test data: ${response.statusText()}`)
setup(
"login and create sample data",
async ({ page, authenticatedRequest: request }) => {
// Login
await page.goto("/")
await page.waitForURL(/localhost:8443/)

await page
.getByRole("textbox", { name: "Username or email" })
.fill("jane.doe")

await page.getByRole("textbox", { name: "Password" }).fill("test")

await page.getByRole("button", { name: "Sign In" }).click()

await page.context().storageState({ path: `e2e/storage/state.json` })

await page.waitForURL("/")
await page.unrouteAll({ behavior: "wait" })

// Create sample data
const files = [
"bgbl-1_1001_2_mods_01/aenderungsgesetz.xml",
"bgbl-1_1002_2_mods-subsitution_01/aenderungsgesetz.xml",
"bgbl-1_2017_s419/aenderungsgesetz.xml",
"bgbl-1_2023_413/aenderungsgesetz.xml",
]

for (const file of files) {
const filePath = path.join(samplesDirectory, file)
const fileContent = fs.readFileSync(filePath) // Read the file content

const formData = new FormData()
formData.append(
"file",
new Blob([fileContent], { type: "text/xml" }),
file,
)
formData.append("force", String(true))

const response = await request.post(
`${process.env.E2E_BASE_URL}/api/v1/announcements`,
{ multipart: formData },
)

if (!response.ok()) {
throw new Error(`Failed to set up test data: ${response.statusText()}`)
}

console.log(`Imported ${file} successfully.`)
}

console.log(`Imported ${file} successfully.`)
}
})
},
)

0 comments on commit f106742

Please sign in to comment.