From 0b9edd280569154d6962f40e3cb37375375865fa Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Wed, 28 Jun 2023 13:01:45 +0800 Subject: [PATCH] fix(playwright): waitForResponse logic --- tests/playwright/pages/Base.ts | 40 +++++++++++++++------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/tests/playwright/pages/Base.ts b/tests/playwright/pages/Base.ts index 2cc75f72f9..7578fd6dec 100644 --- a/tests/playwright/pages/Base.ts +++ b/tests/playwright/pages/Base.ts @@ -28,29 +28,25 @@ export default abstract class BasePage { httpMethodsToMatch?: string[]; responseJsonMatcher?: ResponseSelector; }) { - const waitForResponsePromise = this.rootPage.waitForResponse( - async res => { - let isResJsonMatched = true; - if (responseJsonMatcher) { - try { - isResJsonMatched = responseJsonMatcher(await res.json()); - } catch (e) { - return false; - } - } - - return ( - res.request().url().includes(requestUrlPathToMatch) && - httpMethodsToMatch.includes(res.request().method()) && - isResJsonMatched - ); - }, - { - timeout: 120000, - } + // trigger UI action first + await uiAction(); + // wait for response + const res = await this.rootPage.waitForResponse( + res => + res.url().includes(requestUrlPathToMatch) && + res.status() === 200 && + httpMethodsToMatch.includes(res.request().method()) ); - - return await Promise.all([uiAction(), waitForResponsePromise]); + // handle JSON matcher if provided + let isResJsonMatched = true; + if (responseJsonMatcher) { + try { + isResJsonMatched = responseJsonMatcher(await res.json()); + } catch { + isResJsonMatched = false; + } + } + return isResJsonMatched; } async attachFile({ filePickUIAction, filePath }: { filePickUIAction: Promise; filePath: string[] }) {