From 8631458da757d6ad0030f9ef8e56972b51fe2107 Mon Sep 17 00:00:00 2001 From: Muhammed Mustafa Date: Fri, 21 Oct 2022 15:34:38 +0530 Subject: [PATCH] feat(testing): Added all tests for attachment and added download file support --- scripts/playwright/pages/Base.ts | 22 ++++++++++++++ .../pages/Dashboard/common/Cell/index.ts | 18 +++++++++++- .../Dashboard/common/Toolbar/Actions/index.ts | 4 +++ scripts/playwright/pages/SharedForm/index.ts | 12 +++++++- scripts/playwright/tests/attachments.spec.ts | 29 +++++++++++++++++-- 5 files changed, 80 insertions(+), 5 deletions(-) diff --git a/scripts/playwright/pages/Base.ts b/scripts/playwright/pages/Base.ts index 54a8eeef61..e403a0979e 100644 --- a/scripts/playwright/pages/Base.ts +++ b/scripts/playwright/pages/Base.ts @@ -66,4 +66,26 @@ export default abstract class BasePage { ]); await fileChooser.setFiles(filePath); } + + async downloadAndGetFile({downloadUIAction}:{ downloadUIAction: Promise,}) { + const [ download ] = await Promise.all([ + // It is important to call waitForEvent before click to set up waiting. + this.rootPage.waitForEvent('download'), + // Triggers the download. + downloadUIAction, + ]); + // wait for download to complete + if(await download.failure()) { + throw new Error('Download failed'); + } + + const file = await download.createReadStream(); + const data = await new Promise((resolve, reject) => { + let data = ''; + file?.on('data', chunk => data += chunk); + file?.on('end', () => resolve(data)); + file?.on('error', reject); + }); + return data as any; + } } diff --git a/scripts/playwright/pages/Dashboard/common/Cell/index.ts b/scripts/playwright/pages/Dashboard/common/Cell/index.ts index 659aaec7ae..d63423d847 100644 --- a/scripts/playwright/pages/Dashboard/common/Cell/index.ts +++ b/scripts/playwright/pages/Dashboard/common/Cell/index.ts @@ -49,12 +49,28 @@ export class CellPageObject extends BasePage { index, columnHeader, }: { - index: number; + index?: number; columnHeader: string; }) { return this.get({ index, columnHeader }).dblclick(); } + async fillText({ + index, + columnHeader, + text + }: { + index?: number; + columnHeader: string; + text: string; + }) { + await this.dblclick({ + index, + columnHeader, + }); + await this.get({ index, columnHeader }).locator("input").fill(text); + } + async inCellExpand({ index, columnHeader, diff --git a/scripts/playwright/pages/Dashboard/common/Toolbar/Actions/index.ts b/scripts/playwright/pages/Dashboard/common/Toolbar/Actions/index.ts index 5008cc24cd..82f373b164 100644 --- a/scripts/playwright/pages/Dashboard/common/Toolbar/Actions/index.ts +++ b/scripts/playwright/pages/Dashboard/common/Toolbar/Actions/index.ts @@ -19,4 +19,8 @@ export class ToolbarActionsPage extends BasePage { async click(label: string) { await this.get().locator(`span:has-text("${label}")`).click(); } + + async clickDownloadSubmenu(label: string) { + await this.rootPage.locator(`div[class="nc-project-menu-item"]:has-text("${label}")`).click(); + } } diff --git a/scripts/playwright/pages/SharedForm/index.ts b/scripts/playwright/pages/SharedForm/index.ts index 898026ee2c..e2e19802b4 100644 --- a/scripts/playwright/pages/SharedForm/index.ts +++ b/scripts/playwright/pages/SharedForm/index.ts @@ -16,6 +16,16 @@ export class SharedFormPage extends BasePage { } async submit() { - await this.get().locator('[pw-data="shared-form-submit-button"]').click(); + await this.waitForResponse({ + uiAction:this.get().locator('[pw-data="shared-form-submit-button"]').click(), + httpMethodsToMatch: ["POST"], + requestUrlPathToMatch: '/rows' + }); + } + + async verifySuccessMessage() { + await expect(await this.get().locator('.ant-alert-success', { + hasText: 'Successfully submitted form data' + })).toBeVisible(); } } diff --git a/scripts/playwright/tests/attachments.spec.ts b/scripts/playwright/tests/attachments.spec.ts index 3810e3efc0..831dae4a9f 100644 --- a/scripts/playwright/tests/attachments.spec.ts +++ b/scripts/playwright/tests/attachments.spec.ts @@ -1,4 +1,4 @@ -import { test } from "@playwright/test"; +import { expect, test } from "@playwright/test"; import { DashboardPage } from "../pages/Dashboard"; import { SharedFormPage } from "../pages/SharedForm"; import setup from "../setup"; @@ -12,7 +12,7 @@ test.describe("Attachment column", () => { dashboard = new DashboardPage(page, context.project); }); - test("Create duration column", async ({page, context}) => { + test.only("Create and verify atttachent column, verify it in shared form,", async ({page, context}) => { await dashboard.treeView.openTable({title: "Country"}); await dashboard.grid.column.create({ title: "testAttach", @@ -29,11 +29,34 @@ test.describe("Attachment column", () => { }); await dashboard.form.toolbar.clickShareView(); const sharedFormUrl = await dashboard.form.toolbar.shareView.getShareLink(); + await dashboard.form.toolbar.shareView.close(); + await dashboard.viewSidebar.openView({title: "Country"}); + const newPage = await context.newPage() await newPage.goto(sharedFormUrl); - const sharedForm = new SharedFormPage(newPage); + await sharedForm.cell.fillText({index: 0, columnHeader: "Country", text: "test"}); await sharedForm.cell.attachment.addFile({columnHeader: 'testAttach', filePath: `${process.cwd()}/fixtures/sampleFiles/1.json`}); await sharedForm.submit(); + await sharedForm.verifySuccessMessage(); + await newPage.close(); + + await dashboard.grid.toolbar.clickFields() + await dashboard.grid.toolbar.fields.click({title: "LastUpdate"}); + await dashboard.grid.toolbar.clickActions(); + await dashboard.grid.toolbar.actions.click('Download'); + + const csvFileData: string = await dashboard.downloadAndGetFile({ + downloadUIAction: dashboard.grid.toolbar.actions.clickDownloadSubmenu('Download as CSV') + }); + const csvArray = csvFileData.split('\r\n'); + const columns = csvArray[0]; + const rows = csvArray.slice(1); + const cells = rows[4].split(','); + + expect(columns).toBe('Country,City List,testAttach'); + expect(cells[0]).toBe('Anguilla'); + expect(cells[1]).toBe('South Hill'); + expect(cells[2].includes('4.json(http://localhost:8080/download/')).toBe(true); }); });