This thread is trying to answer question "Why am I getting a status code of -1 when using mock requests in my tests and how can I fix it?"
// test example
test.describe.parallel('Test elements ', () => {
test.use({
storageState: AuthStorage.ADMIN,
})
let mainPage: MainPage
let testPage: TestPage
test.beforeEach(async ({ page, context }) => {
mainPage = new MainPage(page)
testPage = new TestPage(page)
await Promise.all([
await mockActiveElementList(context, 'e2e/mocks/data/mockActiveElementList.json'),
await mockElement(context, 'e2e/mocks/data/mockElement.json'),
await mockItems(context),
])
})
test('1st test elements', async () => {
await mainPage.open()
await mainPage.openElements(1) // jst click on 1st element on list
await compPage.checkTestPage()
})
// mock
import type { BrowserContext } from '@playwright/test'
export async function mockActiveElementList(context: BrowserContext, path: string): Promise<void> {
await context.route('**/v1/elements?page=1&pageSize=10&hasError=false&isArchived=false*', (route) => {
route.fulfill({
status: 200,
path: path,
headers: {
'Connection': 'keep-alive',
'Content-Type': 'application/json',
},
})
})
}
Might suggest, while it may seem logical and the right thing to put it in your beforeEach(), might suggest moving your call to mockActiveElementList into you specific test() method... Especially if you are doing parallel processing with more then one test at the same time? Say you have test("1") test("2") test("3") All firing off an running at the same time. while mainPage/testpage all be referenced by test1, test2, test3 all at the same time?. The same one....? And have a feeling that is your issue... Don't see anything to syncronized access to anything for the two members testPage, mainPage..
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].