From df47b782947aeeca9bd97e60fde13bddb0c05d8c Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Fri, 7 Jun 2024 18:54:25 +0200 Subject: [PATCH] Playwright --- .gitignore | 1 - includes/MslsMetaBox.php | 2 +- package.json | 1 + playwright.config.ts | 12 +++++------- tests/playwright/.env.local.example | 5 +++-- tests/playwright/admin-dashboard.spec.ts | 12 ++++++++++-- tests/playwright/auth.setup.ts | 16 ---------------- tests/playwright/homepage.spec.ts | 6 +++--- 8 files changed, 23 insertions(+), 32 deletions(-) delete mode 100644 tests/playwright/auth.setup.ts diff --git a/.gitignore b/.gitignore index b3ace305..9f7288e6 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,6 @@ tests/coverage/ tests/playwright-results/ tests/playwright-report/ tests/playwright/.env.local -tests/playwright/.auth/ node_modules/ vendor/ diff --git a/includes/MslsMetaBox.php b/includes/MslsMetaBox.php index c1844d61..ec0302f3 100644 --- a/includes/MslsMetaBox.php +++ b/includes/MslsMetaBox.php @@ -270,7 +270,7 @@ public function render_options( $type, $msls_id ): string { * * @return string */ - public function render_option( $post_id, $msls_id ) { + public function render_option( str $post_id, $msls_id ) { return sprintf( '', $post_id, diff --git a/package.json b/package.json index 1883e429..88974166 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "devDependencies": { "@playwright/test": "^1.44.0", "@types/node": "^20.12.11", + "@wordpress/e2e-test-utils": "^11.0.0", "@wordpress/scripts": "^27.9.0", "dotenv": "^16.4.5" } diff --git a/playwright.config.ts b/playwright.config.ts index b4768c7a..334251c2 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -1,4 +1,7 @@ import { defineConfig, devices } from '@playwright/test'; +import * as dotenv from "dotenv"; + +dotenv.config({ path: __dirname + '/tests/playwright/.env.local' }); export default defineConfig({ testDir: './tests/playwright/', @@ -12,20 +15,15 @@ export default defineConfig({ ['json', { outputFile: './tests/playwright-report/test-results.json' }] ], use: { + baseURL: 'https://msls.co', trace: 'on-first-retry', }, projects: [ - { - name: 'setup', - testMatch: 'tests/playwright/auth.setup.ts', - }, { name: 'chromium', use: { ...devices['Desktop Chrome'], - storageState: 'tests/playwright/.auth/user.json', }, - dependencies: ['setup'], }, - ], + ] }); diff --git a/tests/playwright/.env.local.example b/tests/playwright/.env.local.example index 623d7742..391497f4 100644 --- a/tests/playwright/.env.local.example +++ b/tests/playwright/.env.local.example @@ -1,2 +1,3 @@ -user_login=set_a_username -user_pass=set_a_password \ No newline at end of file +WP_USERNAME=set_a_username +WP_PASSWORD=set_a_password +WP_BASE_URL=https://your_server \ No newline at end of file diff --git a/tests/playwright/admin-dashboard.spec.ts b/tests/playwright/admin-dashboard.spec.ts index f2ea9719..ce278ee2 100644 --- a/tests/playwright/admin-dashboard.spec.ts +++ b/tests/playwright/admin-dashboard.spec.ts @@ -1,15 +1,23 @@ +import { loginUser } from '@wordpress/e2e-test-utils'; import { test, expect } from '@playwright/test'; test.describe.configure({ mode: 'parallel' }); + +test.beforeEach(async ({page}) => { + page.goto('/wp-login.php?jetpack-sso-show-default-form=1' ); + + await loginUser(process.env.WP_USERNAME, process.env.WP_PASSWORD); +}); + test('test edit posts', async ({ page }) => { - await page.goto('https://msls.co/wp-admin/edit.php'); + page.goto('/wp-admin/edit.php' ); const mslscol = await page.locator('#mslscol'); await expect(mslscol).toHaveAttribute('scope', 'col'); }); test('test edit pages', async ({ page }) => { - await page.goto('https://msls.co/wp-admin/edit.php?post_type=page'); + page.goto('/wp-admin/edit.php?post_type=page' ); const mslscol = await page.locator('#mslscol'); await expect(mslscol).toHaveAttribute('scope', 'col'); diff --git a/tests/playwright/auth.setup.ts b/tests/playwright/auth.setup.ts deleted file mode 100644 index 8b22f10f..00000000 --- a/tests/playwright/auth.setup.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { test as setup, expect } from '@playwright/test'; -import dotenv from 'dotenv'; - -const authFile = 'tests/playwright/.auth/user.json'; - -setup('authenticate', async ({ page }) => { - dotenv.config({ path: 'tests/playwright/.env.local'}); - - await page.goto('https://msls.co/wp-login.php?jetpack-sso-show-default-form=1'); - - await page.locator('#user_login').fill(process.env.user_login as string); - await page.locator('#user_pass').fill(process.env.user_pass as string); - await page.locator('#wp-submit').click(); - - await page.context().storageState({ path: authFile }); -}); \ No newline at end of file diff --git a/tests/playwright/homepage.spec.ts b/tests/playwright/homepage.spec.ts index 789c3459..5dd828c0 100644 --- a/tests/playwright/homepage.spec.ts +++ b/tests/playwright/homepage.spec.ts @@ -1,13 +1,13 @@ import { test, expect } from '@playwright/test'; test('test', async ({ page }) => { - await page.goto('https://msls.co/'); + await page.goto('/'); await expect(page).toHaveTitle(/Multisite Language Switcher - WordPress multilingual/); - await page.getByRole('link', { name: 'de_DE' }).click(); + await page.getByRole('link', { name: 'de_DE' }).first().click(); await expect(page).toHaveTitle(/Multisite Language Switcher - WordPress mehrsprachig/); - await page.getByRole('link', { name: 'en_GB' }).click(); + await page.getByRole('link', { name: 'en_GB' }).first().click(); await expect(page).toHaveTitle(/Multisite Language Switcher - WordPress multilingual/); }); \ No newline at end of file