|
| 1 | +import { BrowserContext, Page } from '@playwright/test'; |
| 2 | +import {test, expect} from '../fixtures'; |
| 3 | +import { MablFixtures, MablToolset } from '@mablhq/playwright-tools'; |
| 4 | + |
| 5 | +test.describe.configure({ mode: 'serial' }); |
| 6 | + |
| 7 | +test('Create an invoice', async ({context, page, mabl}) => { |
| 8 | + await login(context, page, mabl); |
| 9 | + await page.getByLabel('Invoices').click(); |
| 10 | + await page.getByRole('link', { name: 'New Invoice', exact: true }).click(); |
| 11 | + |
| 12 | + await page.getByLabel('Invoices', {exact: true}).click(); |
| 13 | + await page.getByRole('link', { name: 'New Invoice', exact: true }).click(); |
| 14 | + const invoiceNumber = await page.getByPlaceholder('Enter an Invoice #').inputValue(); |
| 15 | + |
| 16 | + await page.getByPlaceholder('Select a Client').click(); |
| 17 | + await page.locator('[data-option-index="0"]').click(); |
| 18 | + await page.getByText('$').first().click(); |
| 19 | + |
| 20 | + await page.locator('.invoice-addLineItem').click(); |
| 21 | + await page.getByText('Item 1').click(); |
| 22 | + await expect(page.getByText('$').first()).toHaveText('$750.00'); |
| 23 | + |
| 24 | + |
| 25 | + await page.getByLabel('Save').click(); |
| 26 | + |
| 27 | + // See details |
| 28 | + await page.locator(`[data-invoicenumber="${invoiceNumber}"]`).click(); |
| 29 | + |
| 30 | + await expect(page.getByRole('heading', { name: `Invoice ${invoiceNumber}`, exact: true })).toBeVisible(); |
| 31 | + |
| 32 | + // TODO: How we test PDF? |
| 33 | + await page.getByLabel('More Actions').click(); |
| 34 | + |
| 35 | + const downloadPromise = page.waitForEvent('download'); |
| 36 | + |
| 37 | + await page.locator('.js-download-pdf-invoice-more-actions').click(); |
| 38 | + |
| 39 | + const download = await downloadPromise; |
| 40 | + await download.saveAs(`./downloads/${invoiceNumber}.pdf`); |
| 41 | + |
| 42 | + // create a Page for the downloaded PDF to open in |
| 43 | + const pageForPdf = await context.newPage(); |
| 44 | + |
| 45 | + // use the mabl tools to open the file in a new Page |
| 46 | + await mabl.openPdfFile(`./downloads/${invoiceNumber}.pdf`, pageForPdf); |
| 47 | + |
| 48 | + const aiResult = await mabl.evaluateGenAiAssertion( |
| 49 | + pageForPdf, |
| 50 | + ` |
| 51 | + This is an invoice for $750.00 |
| 52 | + The invoice is in English. |
| 53 | + The invoice has no images at all. |
| 54 | + `); |
| 55 | + |
| 56 | + expect(aiResult.success).toBeTruthy(); |
| 57 | +}); |
| 58 | + |
| 59 | +async function login(context: BrowserContext, page: Page, mabl: MablToolset) { |
| 60 | + // Print friendly current time |
| 61 | + console.log(`Running test at ${new Date().toISOString()}`); |
| 62 | + const credential = await mabl.getCredentials(process.env.MABL_CREDENTIALS_ID!); |
| 63 | + const emailPage = await context.newPage(); |
| 64 | + |
| 65 | + const mablEmailPromise = mabl.waitForEmail(credential.username, emailPage, { |
| 66 | + lookbackTimeMs: 1, |
| 67 | + }); |
| 68 | + |
| 69 | + await page.goto("/"); |
| 70 | + await page.getByLabel("Email").fill(credential.username); |
| 71 | + await page.getByLabel("Password").fill(credential.password); |
| 72 | + |
| 73 | + await page.getByRole("button", { name: "Log In" }).click(); |
| 74 | + |
| 75 | + const mablEmail = await mablEmailPromise; |
| 76 | + await mablEmail.open(); |
| 77 | + |
| 78 | + const code = (await mablEmail |
| 79 | + .getBodyLocator() |
| 80 | + .locator(".two-factor-auth-code-container") |
| 81 | + .textContent())!; |
| 82 | + await page.getByLabel("Verification code digit 1").pressSequentially(code); |
| 83 | + await emailPage.close(); |
| 84 | +} |
| 85 | + |
0 commit comments