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 21:50, 11 June 2024
, 11 June→Global Setup and Teardown
m (→Test Tags) |
|||
Line 1,849: | Line 1,849: | ||
=== Global Setup and Teardown === | === Global Setup and Teardown === | ||
1. Create file <code>global-setup.ts</code> with more or less same code as file <code>tests\newArticle.setup.ts</code> | |||
<nowiki> | |||
import { expect, request } from "@playwright/test" | |||
import user from "./.auth/user.json" | |||
import fs from "fs" | |||
async function globalSetup() { | |||
const authfile = ".auth/user.json" | |||
const context = await request.newContext() | |||
const responseToken = await context.post(... ) | |||
const responseBody = await responseToken.json() | |||
const accessToken = responseBody.user.token | |||
user.origins[0].localStorage[0].value = accessToken | |||
fs.writeFileSync(authfile, JSON.stringify(user)) | |||
process.env["ACCESS_TOKEN"] = accessToken | |||
const articleResponse = await context.post(...) | |||
expect(articleResponse.status()).toEqual(201) | |||
const articleResponseBody = await articleResponse.json() | |||
const slugId = articleResponseBody.article.slug | |||
process.env["SLUGID"] = slugId | |||
} | |||
export default globalSetup</nowiki> | |||
2. Create file <code>global-teardown.ts</code> | |||
<nowiki> | |||
import { request, expect } from "@playwright/test" | |||
async function globalTeardown() { | |||
const context = await request.newContext() | |||
// Clean up | |||
// delete the article using the slug extracted earlier | |||
const articleDeleteResponse = await context.delete( | |||
`https://conduit-api.bondaracademy.com/api/articles/${process.env.SLUGID}`, | |||
{ | |||
headers: { Authorization: `Token ${process.env.ACCESS_TOKEN}` }, | |||
} | |||
) | |||
expect(articleDeleteResponse.status()).toEqual(204) | |||
} | |||
export default globalTeardown</nowiki> | |||
3. Create file <code>tests\likesCounterGlobal.spec.ts</code> | |||
<nowiki> | |||
import { test, expect, request } from "@playwright/test" | |||
test("Like counter increase", async ({ page }) => { | |||
await page.goto("https://conduit.bondaracademy.com/") | |||
await page.getByText("Global Feed").click() | |||
const firstLikeButton = page | |||
.locator("app-article-preview") | |||
.first() | |||
.locator("button") | |||
await expect(firstLikeButton).toContainText("0") | |||
await firstLikeButton.click() | |||
await expect(firstLikeButton).toContainText("1") | |||
})</nowiki> | |||
4. Update file <code>playwright.config.ts</code> | |||
<nowiki> | |||
export default defineConfig({ | |||
... | |||
globalSetup: require.resolve("./global-setup.ts"), | |||
globalTeardown: require.resolve("./global-teardown.ts"), | |||
projects: [ | |||
... | |||
{ | |||
name: "likeCounterGlobal", | |||
testMatch: "likesCounterGlobal.spec.ts", | |||
use: { ...devices["Desktop Chrome"], storageState: ".auth/user.json" }, | |||
}, | |||
], | |||
})</nowiki> | |||
=== Test Tags === | === Test Tags === | ||