Udemy Playwright: Web Automation Testing From Zero to Hero: Difference between revisions
Jump to navigation
Jump to search
Udemy Playwright: Web Automation Testing From Zero to Hero (view source)
Revision as of 02:27, 7 June 2024
, 7 June→Intercept Browser API Response
Line 1,246: | Line 1,246: | ||
=== Intercept Browser API Response === | === Intercept Browser API Response === | ||
<nowiki> | |||
test("create article", async ({ page, request }) => { | |||
// Use the web ui to create the article | |||
await page.getByText("New Article").click() | |||
await page | |||
.getByRole("textbox", { name: "Article Title" }) | |||
.fill("Playwright is awesome") | |||
await page | |||
.getByRole("textbox", { name: "What's this article about?" }) | |||
.fill("About Playwright") | |||
await page | |||
.getByRole("textbox", { name: "Write your article (in markdown)" }) | |||
.fill("We like to use Playwright for automation") | |||
await page.getByRole("button", { name: "Publish Article" }).click() | |||
// intercept the API response to extract the slug which is needed for clean up later | |||
const articleResponse = await page.waitForResponse( | |||
"https://conduit-api.bondaracademy.com/api/articles/" | |||
) | |||
const articleresponseBody = await articleResponse.json() | |||
const slugID = articleresponseBody.article.slug | |||
// Assert that article was created | |||
await expect(page.locator(".article-page h1")).toContainText( | |||
"Playwright is awesome" | |||
) | |||
// Go to global feed | |||
await page.getByText("Home").click() | |||
await page.getByText("Global Feed").click() | |||
// Assert new article is listed | |||
await expect(page.locator("app-article-list h1").first()).toContainText( | |||
"Playwright is awesome" | |||
) | |||
// Clean up | |||
// Obtain access token for API calls | |||
const response = await request.post( | |||
"https://conduit-api.bondaracademy.com/api/users/login", | |||
{ | |||
data: { | |||
user: { | |||
email: "conduit@dirksonline.net", | |||
password: "qB85R86#ZMKME$jVEVq#vJMDr*A!cJk", | |||
}, | |||
}, | |||
} | |||
) | |||
const responseBody = await response.json() | |||
const accessToken = responseBody.user.token | |||
// delete the article using the slug extracted earlier | |||
const articleDeleteResponse = await request.delete( | |||
`https://conduit-api.bondaracademy.com/api/articles/${slugID}`, | |||
{ | |||
headers: { | |||
Authorization: `Token ${accessToken}`, | |||
}, | |||
} | |||
) | |||
expect(articleDeleteResponse.status()).toEqual(204) | |||
})</nowiki> | |||
Notes: | |||
* should really only login once and re-use the access token | |||
* should be refactored using page objects to improve readability | |||
=== Sharing Authentication State === | === Sharing Authentication State === | ||
=== API Authentication === | === API Authentication === |