Skip to content

Commit

Permalink
feat: implement playwright tests for Web IDE
Browse files Browse the repository at this point in the history
  • Loading branch information
xpyctumo committed Feb 11, 2025
1 parent b427d23 commit ded49b1
Show file tree
Hide file tree
Showing 18 changed files with 902 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { all } = require("axios");

/* eslint-env node */
module.exports = {
extends: [
Expand All @@ -17,6 +19,8 @@ module.exports = {
"!.lintstagedrc.js",
".lintstagedrc.js",
"next.config.js",
"playwright.config.ts",
"tests/*",
],
plugins: ["@typescript-eslint"],
root: true,
Expand Down
27 changes: 27 additions & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Web IDE Browser Tests
on:
push:
branches: ['**']
pull_request:
branches: ['**']
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: lts/*
- name: Install dependencies
run: npm ci
- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Run Playwright tests
run: npx playwright test
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: playwright-report
path: playwright-report/
retention-days: 30
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

# Playwright
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"format:check": "prettier --check --ignore-path .prettierignore --ignore-path .gitignore .",
"format:fix": "prettier --write --ignore-path .prettierignore --ignore-path .gitignore .",
"postinstall": "husky",
"commit": "git-cz"
"commit": "git-cz",
"test": "npx playwright test"
},
"config": {
"commitizen": {
Expand Down Expand Up @@ -59,6 +60,7 @@
"monaco-vim": "^0.4.1",
"next": "13.0.7",
"onigasm": "^2.2.5",
"playwright": "^1.50.1",
"react": "18.2.0",
"react-dnd": "^16.0.1",
"react-dom": "18.2.0",
Expand All @@ -84,6 +86,7 @@
"@commitlint/cli": "^19.5.0",
"@commitlint/config-conventional": "^19.5.0",
"@commitlint/types": "^19.5.0",
"@playwright/test": "^1.50.1",
"@types/bn.js": "^5.1.1",
"@types/jsonwebtoken": "^9.0.1",
"@types/lodash.clonedeep": "^4.5.7",
Expand Down
79 changes: 79 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { defineConfig, devices } from '@playwright/test';

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// import dotenv from 'dotenv';
// import path from 'path';
// dotenv.config({ path: path.resolve(__dirname, '.env') });

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './tests',
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://127.0.0.1:3000',

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},

/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},

{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},

{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
},

/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },

/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],

/* Run your local dev server before starting the tests */
// webServer: {
// command: 'npm run start',
// url: 'http://127.0.0.1:3000',
// reuseExistingServer: !process.env.CI,
// },
});
27 changes: 27 additions & 0 deletions tests/contract/create-and-delete-project.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { expect, test } from '@playwright/test';
import { randomUUID } from 'crypto';
import { DELETE_PROJECT_LOCATOR } from 'tests/locators';
import { createProject, ProjectType } from 'tests/utils';

test('Create Blank Contract in Web IDE then delete', async ({ page }) => {
// Open Web IDE
await page.goto('https://ide.ton.org/');
const projectName = randomUUID();
// Create project
await createProject(page, projectName, ProjectType.BlankContract);

// Delete project
await expect(page.locator(DELETE_PROJECT_LOCATOR)).toBeVisible();
await page.locator(DELETE_PROJECT_LOCATOR).first().click();

await expect(
page.getByText(`Delete my \`${projectName}\` Project?`),
).toBeVisible();
await expect(page.getByRole('button', { name: 'Cancel' })).toBeVisible();
await expect(page.getByRole('button', { name: 'Delete' })).toBeVisible();
await page.getByRole('button', { name: 'Delete' }).click();
// Check that project is deleted
await expect(
page.getByRole('button', { name: 'Create a new project' }),
).toBeVisible();
});
52 changes: 52 additions & 0 deletions tests/contract/create-blank-contract-and-build.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { expect, test } from '@playwright/test';
import { randomUUID } from 'crypto';
import { XTERM_LOGS_LOCATOR } from 'tests/locators';
import {
getElementContent,
isCodeEditorLoaded,
logsContain,
openBuildAndDeployTab,
} from 'tests/utils';

test('Create Blank Contract in Web IDE then build', async ({ page }) => {
// Open Web IDE
await page.goto('https://ide.ton.org/');
await expect(
page.getByRole('button', { name: 'Create a new project' }),
).toBeVisible();
// Create new project
await page.getByRole('button', { name: 'Create a new project' }).click();

// Select Blank Contract
const projectName = randomUUID();
await page.getByRole('textbox', { name: 'Project name' }).click();
await page.getByRole('textbox', { name: 'Project name' }).fill(projectName);

await expect(page.getByText('TactFunc')).toBeVisible();
await expect(page.getByText('Blank ContractCounter Contract')).toBeVisible();
await expect(
page.getByRole('button', { name: 'Create', exact: true }),
).toBeVisible();

await page.getByText('Blank Contract').click();
await page.getByRole('button', { name: 'Create', exact: true }).click();

await isCodeEditorLoaded(page);
await openBuildAndDeployTab(page);

const getLogs = () => getElementContent(page, XTERM_LOGS_LOCATOR);
await logsContain(await getLogs(), [`Project '${projectName}' is opened`]);

// Build contract
await expect(page.getByRole('button', { name: 'Build' })).toBeVisible();
await page.getByRole('button', { name: 'Build' }).click();
await page.waitForTimeout(2000);

// Check build logs
await logsContain(await getLogs(), [
/Message sent: Deploy, from [A-Za-z0-9._-]{1,10}, to [A-Za-z0-9._-]{1,10}, value 0\.05, not bounced/,
/Transaction Executed: success, Exit Code: 0, Gas: 0.0028824/,
/Message sent: DeployOk, from [A-Za-z0-9._-]{1,10}, to [A-Za-z0-9._-]{1,10}, value 0.0466392/,
/Transaction Executed: success, Exit Code: 0, Gas: 0.0001236/,
]);
});
32 changes: 32 additions & 0 deletions tests/contract/create-counter-contract-and-build.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expect, test } from '@playwright/test';
import { randomUUID } from 'crypto';
import { XTERM_LOGS_LOCATOR } from 'tests/locators';
import {
createProject,
getElementContent,
isCodeEditorLoaded,
logsContain,
openBuildAndDeployTab,
ProjectType,
} from 'tests/utils';

test('Create Counter Contract in Web IDE then build', async ({ page }) => {
// Open Web IDE
await page.goto('https://ide.ton.org/');
const projectName = randomUUID();
// Create project
await createProject(page, projectName, ProjectType.CounterContract);

await isCodeEditorLoaded(page);
await openBuildAndDeployTab(page);

const getLogs = () => getElementContent(page, XTERM_LOGS_LOCATOR);
await logsContain(await getLogs(), [`Project '${projectName}' is opened`]);

// Build contract
await expect(page.getByRole('button', { name: 'Build' })).toBeVisible();
await page.getByRole('button', { name: 'Build' }).click();
await page.waitForTimeout(2000);
// Check build logs
await logsContain(await getLogs(), [/Built Successfully/]);
});
28 changes: 28 additions & 0 deletions tests/locators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const EDITOR_CONTAINER_LOCATOR =
"//div[contains(@class, 'Editor_container_')]";

export const CLONE_PROJECT_LOCATOR =
"//div[contains(@class, 'CloneProject_root')]";

export const MONACO_LINES_LOCATOR =
"//div[contains(@class, 'view-lines monaco-mouse-cursor-text')]";

export const XTERM_LOGS_LOCATOR = "//div[contains(@class, 'xterm-rows')]";

export const CODE_TAB_LOCATOR =
'//div[contains(@class, "WorkspaceSidebar_action_")][1]';

export const BUILD_AND_DEPLOY_LOCATOR =
'//div[contains(@class, "WorkspaceSidebar_action_")][2]';

export const UNIT_TESTS_LOCATOR =
'//div[contains(@class, "WorkspaceSidebar_action_")][3]';

export const GIT_TAB_LOCATOR =
'//div[contains(@class, "WorkspaceSidebar_action_")][4]';

export const DELETE_PROJECT_LOCATOR =
'//div[contains(@class, "ManageProject_deleteProject")]';

export const PROJECT_DROPDOWN_LOCATOR =
'(//span[contains(@class, "ant-select-selection-item") and @title])[1]';
53 changes: 53 additions & 0 deletions tests/project/clone-shared-code.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { encodeBase64 } from '@/utility/utils';
import { expect, test } from '@playwright/test';
import { randomUUID } from 'crypto';
import { CLONE_PROJECT_LOCATOR, MONACO_LINES_LOCATOR } from 'tests/locators';
import {
getActiveProject,
getActiveProjectLocator,
getElementContent,
isCodeEditorLoaded,
normalizeString,
} from 'tests/utils';

test('Clone code from shared link to local', async ({ page, context }) => {
// Grant clipboard permissions to browser context
await context.grantPermissions(['clipboard-read', 'clipboard-write']);

// Generate share code
const shareCode = `contract ${randomUUID()} with Deployable {\n\n\n}`;
const url = `https://ide.ton.org/?code=${encodeBase64(shareCode)}&lang=tact`;
const projectName = randomUUID();

// Open page with share code
await page.goto(url);
await isCodeEditorLoaded(page);
const ideCodeBefore = await getElementContent(page, MONACO_LINES_LOCATOR);

// Validate that project name is "temp" is active
await expect(await getActiveProject(page)).toBe('temp');
await expect(normalizeString(ideCodeBefore)).toBe(normalizeString(shareCode));
await page.locator(CLONE_PROJECT_LOCATOR).click();

await expect(
page.getByRole('textbox', { name: 'Project name' }),
).toBeVisible();
await expect(page.getByRole('button', { name: 'Save' })).toBeVisible();
await page.getByRole('textbox', { name: 'Project name' }).click();
await page.getByRole('textbox', { name: 'Project name' }).fill(projectName);
await page.getByRole('button', { name: 'Save' }).click();

// Validate that project name is new
await expect(await getActiveProjectLocator(page)).toHaveText(projectName);
await page
.locator('div')
.filter({ hasText: /^main\.tact$/ })
.nth(1)
.click();
await page.waitForTimeout(200);

// Validate IDE code
const ideCodeAfter = await getElementContent(page, MONACO_LINES_LOCATOR);

await expect(normalizeString(ideCodeAfter)).toBe(normalizeString(shareCode));
});
Loading

0 comments on commit ded49b1

Please sign in to comment.