Skip to content

Commit

Permalink
move playwright to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed May 31, 2024
1 parent 09e2dac commit 108f5fa
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ overrides:
- files: ["*.config.*"]
rules:
i/no-unused-modules: [0]
- files: ["tests/e2e/**"]
plugins: [eslint-plugin-playwright]
extends: plugin:playwright/recommended
- files: ["**/*.test.*", "web_src/js/test/setup.js"]
env:
vitest-globals/env: true
Expand Down
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"eslint-plugin-jquery": "1.5.1",
"eslint-plugin-no-jquery": "2.7.0",
"eslint-plugin-no-use-extend-native": "0.5.0",
"eslint-plugin-playwright": "1.6.2",
"eslint-plugin-regexp": "2.6.0",
"eslint-plugin-sonarjs": "1.0.3",
"eslint-plugin-unicorn": "53.0.0",
Expand Down
14 changes: 5 additions & 9 deletions playwright.config.js → playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
// @ts-check
import {devices} from '@playwright/test';
import {env} from 'node:process';

const BASE_URL = process.env.GITEA_URL?.replace?.(/\/$/g, '') || 'http://localhost:3000';
const BASE_URL = env.GITEA_URL?.replace?.(/\/$/g, '') || 'http://localhost:3000';

/**
* @see https://playwright.dev/docs/test-configuration
* @type {import('@playwright/test').PlaywrightTestConfig}
*/
export default {
testDir: './tests/e2e/',
testMatch: /.*\.test\.e2e\.js/, // Match any .test.e2e.js files
Expand All @@ -24,13 +20,13 @@ export default {
},

/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: Boolean(process.env.CI),
forbidOnly: Boolean(env.CI),

/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
retries: env.CI ? 2 : 0,

/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: process.env.CI ? 'list' : [['list'], ['html', {outputFolder: 'tests/e2e/reports/', open: 'never'}]],
reporter: env.CI ? 'list' : [['list'], ['html', {outputFolder: 'tests/e2e/reports/', open: 'never'}]],

/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
Expand Down
12 changes: 6 additions & 6 deletions tests/e2e/example.test.e2e.js → tests/e2e/example.test.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// @ts-check
import {test, expect} from '@playwright/test';
import {login_user, save_visual, load_logged_in_context} from './utils_e2e.js';
import {login_user, save_visual, load_logged_in_context} from './utils_e2e.ts';

test.beforeAll(async ({browser}, workerInfo) => {
await login_user(browser, workerInfo, 'user2');
});

test('Load Homepage', async ({page}) => {
test('homepage', async ({page}) => {
const response = await page.goto('/');
await expect(response?.status()).toBe(200); // Status OK
await expect(page).toHaveTitle(/^Gitea: Git with a cup of tea\s*$/);
await expect(page.locator('.logo')).toHaveAttribute('src', '/assets/img/logo.svg');
});

test('Test Register Form', async ({page}, workerInfo) => {
test('register', async ({page}, workerInfo) => {
const response = await page.goto('/user/sign_up');
await expect(response?.status()).toBe(200); // Status OK
await page.type('input[name=user_name]', `e2e-test-${workerInfo.workerIndex}`);
Expand All @@ -29,22 +29,22 @@ test('Test Register Form', async ({page}, workerInfo) => {
save_visual(page);
});

test('Test Login Form', async ({page}, workerInfo) => {
test('login', async ({page}, workerInfo) => {
const response = await page.goto('/user/login');
await expect(response?.status()).toBe(200); // Status OK

await page.type('input[name=user_name]', `user2`);
await page.type('input[name=password]', `password`);
await page.click('form button.ui.primary.button:visible');

await page.waitForLoadState('networkidle');
await page.waitForLoadState('networkidle'); // eslint-disable-line playwright/no-networkidle

await expect(page.url()).toBe(`${workerInfo.project.use.baseURL}/`);

save_visual(page);
});

test('Test Logged In User', async ({browser}, workerInfo) => {
test('logged in user', async ({browser}, workerInfo) => {
const context = await load_logged_in_context(browser, workerInfo, 'user2');
const page = await context.newPage();

Expand Down
7 changes: 4 additions & 3 deletions tests/e2e/utils_e2e.js → tests/e2e/utils_e2e.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect} from '@playwright/test';
import {env} from 'node:process';

const ARTIFACTS_PATH = `tests/e2e/test-artifacts`;
const LOGIN_PASSWORD = 'password';
Expand All @@ -20,7 +21,7 @@ export async function login_user(browser, workerInfo, user) {
await page.type('input[name=password]', LOGIN_PASSWORD);
await page.click('form button.ui.primary.button:visible');

await page.waitForLoadState('networkidle');
await page.waitForLoadState('networkidle'); // eslint-disable-line playwright/no-networkidle

await expect(page.url(), {message: `Failed to login user ${user}`}).toBe(`${workerInfo.project.use.baseURL}/`);

Expand All @@ -44,8 +45,8 @@ export async function load_logged_in_context(browser, workerInfo, user) {

export async function save_visual(page) {
// Optionally include visual testing
if (process.env.VISUAL_TEST) {
await page.waitForLoadState('networkidle');
if (env.VISUAL_TEST) {
await page.waitForLoadState('networkidle'); // eslint-disable-line playwright/no-networkidle
// Mock page/version string
await page.locator('footer div.ui.left').evaluate((node) => node.innerHTML = 'MOCK');
await expect(page).toHaveScreenshot({
Expand Down

0 comments on commit 108f5fa

Please sign in to comment.