Udemy Playwright: Web Automation Testing From Zero to Hero: Difference between revisions

Jump to navigation Jump to search
Line 1,769: Line 1,769:


=== Project Setup and Teardown ===
=== Project Setup and Teardown ===
1. Create <code>tests\newArticle.setup.ts</code> to create a new article via API
<nowiki>
import { expect, test as setup } from "@playwright/test"
setup("create new article", async ({ request }) => {
  const articleResponse = await request.post(
    "https://conduit-api.bondaracademy.com/api/articles/",
    {
      data: {
        article: {
          title: "Likes Test Article",
          description: "Test description",
          body: "Test body",
          tagList: [],
        },
      },
    }
  )
  expect(articleResponse.status()).toEqual(201)
  const response = await articleResponse.json()
  const slugId = response.article.slug
  process.env["SLUGID"] = slugId
})</nowiki>
2. Create <code>tests\likesCounter.spec.ts</code> to test the like counter
<nowiki>
...
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>
3. Create <code>tests\articleCleanUp.setup.ts</code> to clean up (ie. delete) the article created via API
<nowiki>
...
setup("delete article", async ({ request }) => {
  // Clean up
  // delete the article using the slug extracted earlier
  const articleDeleteResponse = await request.delete(
    `https://conduit-api.bondaracademy.com/api/articles/${process.env.SLUGID}`
  )
  expect(articleDeleteResponse.status()).toEqual(204)
})</nowiki>
4. Update <code>playwright.config.ts</code> to create new projects
:* articleSetup (with dependency on setup to fetch auth token, and using teardown)
:* likeCounter (with dependency on articleSetup)
:* articleCleanUp
<nowiki>
...
export default defineConfig({
  ...
  projects: [
    { name: "setup", testMatch: "auth.setup.ts" },
    {
      name: "articleSetup",
      testMatch: "newArticle.setup.ts",
      dependencies: ["setup"],
      teardown: "articleCleanUp",
    },
    {
      name: "articleCleanUp",
      testMatch: "articleCleanUp.setup.ts",
    },
    {
      name: "likeCounter",
      testMatch: "likesCounter.spec.ts",
      use: { ...devices["Desktop Chrome"], storageState: ".auth/user.json" },
      dependencies: ["articleSetup"],
    },
  ],
})</nowiki>
=== Test Tags ===
=== Test Tags ===
=== Mobile Device Emulator ===
=== Mobile Device Emulator ===