Rayrun
← Back to Discord Forum

API response redirect

.jimbonicolesmithposted in #help-playwright
Open in Discord
.jimbonicolesmith
.jimbonicolesmith

Hello! I am trying to write tests in staging for a CMS asset that uses a staging API to display images/descriptions. However, a part of the staging API is inoperable and I need to redirect to the production API for these particular tests to work. I tried using Requestly but wasn't able to figure out how to extend coverage of their redirect rules to more than one Chrome profile.

Does anyone have any recs on how I can redirect to the prod API instead of the staging API?

This thread is trying to answer question "How can I redirect to the production API instead of the staging API?"

3 replies
garland2953

You could mock the responses in the test? I have used this with GraphQl so the information in the UI of our application is static and can be reliably pixel matched.

`import { Page } from "@playwright/test";

const getQueryOperationName = (query: string): string => { const operationNameRegex = /(?<=query\s).*(?=\s{)/; const operationNameList: string[] | null = query.match(operationNameRegex); return operationNameList ? operationNameList[0] : ``; };

const getMutationOperationName = (query: string): string => { const operationNameRegex = /(?<=muatation\s).*(?=\s{)/; const operationNameList: string[] | null = query.match(operationNameRegex); return operationNameList ? operationNameList[0] : ``; };

const hasOperationName = (query: string, operationName: string): boolean => getQueryOperationName(query) === operationName || getMutationOperationName(query) === operationName;

export const mockGraphQlRequest = async ( page: Page, operationName: string, operationResponse: object ) => { await page.route(youerndpoint, async (route) => { const body: { query: string } = route.request().postDataJSON(); const query = body.query;

if (hasOperationName(query, operationName)) {
  route.fulfill({
    status: 200,
    contentType: "application/json",
    body: JSON.stringify(operationResponse),
  });
} else {
  route.fulfill({
    status: 500,
    contentType: "application/json",
    body: JSON.stringify({ message: "Internal Server Error" }),
  });
}

}); }; `

garland2953

this code snippet could be a good starting point

.jimbonicolesmith
.jimbonicolesmith

i'll check this out, thank you for your help!

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

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 luc@ray.run.