This commit is contained in:
rsgltzyd 2024-09-30 12:08:58 +08:00
parent e55b756264
commit f4a31555c9
10 changed files with 163 additions and 97 deletions

3
.env Normal file
View File

@ -0,0 +1,3 @@
BASE_URL=https://hlk.meiguanjia.net
BOSS_ACCOUNT=17770720274
BOSS_PASSWORD=a123456

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ node_modules/
/blob-report/ /blob-report/
/playwright/.cache/ /playwright/.cache/
/.idea/ /.idea/
/.auth/

13
.prettierrc Normal file
View File

@ -0,0 +1,13 @@
{
"printWidth": 100,
"tabWidth": 4,
"semi": true,
"singleQuote": true,
"quoteProps": "consistent",
"trailingComma": "es5",
"bracketSpacing": true,
"arrowParens": "avoid",
"proseWrap": "never",
"endOfLine": "lf",
"embeddedLanguageFormatting": "off"
}

15
package-lock.json generated
View File

@ -8,6 +8,9 @@
"name": "playwright", "name": "playwright",
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": {
"dotenv": "^16.4.5"
},
"devDependencies": { "devDependencies": {
"@playwright/test": "^1.47.2", "@playwright/test": "^1.47.2",
"@types/node": "^22.7.4" "@types/node": "^22.7.4"
@ -39,6 +42,18 @@
"undici-types": "~6.19.2" "undici-types": "~6.19.2"
} }
}, },
"node_modules/dotenv": {
"version": "16.4.5",
"resolved": "https://registry.npmmirror.com/dotenv/-/dotenv-16.4.5.tgz",
"integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==",
"license": "BSD-2-Clause",
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://dotenvx.com"
}
},
"node_modules/fsevents": { "node_modules/fsevents": {
"version": "2.3.2", "version": "2.3.2",
"resolved": "https://registry.npmmirror.com/fsevents/-/fsevents-2.3.2.tgz", "resolved": "https://registry.npmmirror.com/fsevents/-/fsevents-2.3.2.tgz",

View File

@ -10,5 +10,8 @@
"devDependencies": { "devDependencies": {
"@playwright/test": "^1.47.2", "@playwright/test": "^1.47.2",
"@types/node": "^22.7.4" "@types/node": "^22.7.4"
},
"dependencies": {
"dotenv": "^16.4.5"
} }
} }

View File

@ -1,79 +1,88 @@
// @ts-check // @ts-check
const { defineConfig, devices } = require('@playwright/test'); const { defineConfig, devices } = require('@playwright/test');
const { resolve } = require('path');
/** /**
* Read environment variables from file. * Read environment variables from file.
* https://github.com/motdotla/dotenv * https://github.com/motdotla/dotenv
*/ */
// require('dotenv').config({ path: path.resolve(__dirname, '.env') }); require('dotenv').config({ path: resolve(__dirname, '.env') });
const boss_storageState = '.auth/boss_user.json';
/** /**
* @see https://playwright.dev/docs/test-configuration * @see https://playwright.dev/docs/test-configuration
*/ */
module.exports = defineConfig({ module.exports = defineConfig({
testDir: './tests', testDir: './tests',
/* Run tests in files in parallel */ /* Run tests in files in parallel */
fullyParallel: true, fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */ /* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI, forbidOnly: !!process.env.CI,
/* Retry on CI only */ /* Retry on CI only */
retries: process.env.CI ? 2 : 0, retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */ /* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined, workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */ /* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'html', reporter: 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: { use: {
/* Base URL to use in actions like `await page.goto('/')`. */ /* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://127.0.0.1:3000', // baseURL: 'http://127.0.0.1:3000',
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry', trace: 'on-first-retry',
},
/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
}, },
{ /* Configure projects for major browsers */
name: 'firefox', projects: [
use: { ...devices['Desktop Firefox'] }, // Setup project
}, { name: 'setup', testMatch: /.*\.setup\.js/ },
{ {
name: 'webkit', name: 'chromium',
use: { ...devices['Desktop Safari'] }, use: {
}, ...devices['Desktop Chrome'],
storageState: boss_storageState,
},
dependencies: ['setup'],
},
/* Test against mobile viewports. */ // {
// { // name: 'firefox',
// name: 'Mobile Chrome', // use: { ...devices['Desktop Firefox'] },
// use: { ...devices['Pixel 5'] }, // },
// }, //
// { // {
// name: 'Mobile Safari', // name: 'webkit',
// use: { ...devices['iPhone 12'] }, // use: { ...devices['Desktop Safari'] },
// }, // },
/* Test against branded browsers. */ /* Test against mobile viewports. */
// { // {
// name: 'Microsoft Edge', // name: 'Mobile Chrome',
// use: { ...devices['Desktop Edge'], channel: 'msedge' }, // use: { ...devices['Pixel 5'] },
// }, // },
// { // {
// name: 'Google Chrome', // name: 'Mobile Safari',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' }, // use: { ...devices['iPhone 12'] },
// }, // },
],
/* Run your local dev server before starting the tests */ /* Test against branded browsers. */
// webServer: { // {
// command: 'npm run start', // name: 'Microsoft Edge',
// url: 'http://127.0.0.1:3000', // use: { ...devices['Desktop Edge'], channel: 'msedge' },
// reuseExistingServer: !process.env.CI, // },
// }, // {
// 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,
// },
}); });

29
tests/common.ts Normal file
View File

@ -0,0 +1,29 @@
import { Locator, Page } from "@playwright/test";
import { test as base } from "@playwright/test";
type MyFixtures = {
homePage: HomePage;
};
export const test = base.extend<MyFixtures>({
homePage: async ({ page }, use) => {
const homePage = new HomePage(page);
await use(homePage);
},
});
class HomePage {
private page: Page;
private $start: Locator;
constructor(page: Page) {
this.page = page;
this.$start = this.page.getByRole("link", { name: "Get started" });
}
async clickStart() {
await this.$start.click();
}
}
export { expect } from "@playwright/test";

View File

@ -1,29 +0,0 @@
import { Locator, Page } from "@playwright/test";
import { test as base } from "@playwright/test";
type MyFixtures = {
homePage: HomePage;
};
export const test = base.extend<MyFixtures>({
homePage: async ({ page }, use) => {
const homePage = new HomePage(page);
await use(homePage);
},
});
class HomePage {
private page: Page;
private $start: Locator;
constructor(page: Page) {
this.page = page;
this.$start = this.page.getByRole("link", { name: "Get started" });
}
async clickStart() {
await this.$start.click();
}
}
export { expect } from "@playwright/test";

View File

@ -0,0 +1,22 @@
import { test as setup, expect } from '@playwright/test';
const authFile = '.auth/boss_user.json';
setup('authenticate', async ({ page }) => {
const baseURL = process.env.BASE_URL;
const account = process.env.BOSS_ACCOUNT;
const password = process.env.BOSS_PASSWORD;
const $account = page.getByRole('textbox', { name: '请输入您的手机号码' });
const $password = page.getByRole('textbox', { name: '请输入登录密码' });
const $loginConfirm = page.getByRole('button', { name: /登\s录/ });
await page.goto(baseURL);
await $account.fill(account);
const $accountStatus = page.locator('form div').filter({ has: $account }).locator('span i').last();
await expect($accountStatus).toHaveClass(/pass/);
await $password.fill(password);
await page.getByLabel('请同意慧来客隐私政策和用户协议').check();
await $loginConfirm.click();
await expect($loginConfirm).not.toBeVisible();
await page.context().storageState({ path: authFile });
});

View File

@ -1,14 +1,14 @@
// @ts-check // @ts-check
const { test, expect } = require("./common"); const { test, expect } = require('./common');
test("get started link", async ({ page, homePage }) => { test('get started link', async ({ page, homePage }) => {
await page.goto("https://playwright.dev/"); await page.goto(process.env.BASE_URL);
// Click the get started link. // Click the get started link.
// await page.getByRole('link', {name: 'Get started'}).click(); // await page.getByRole('link', {name: 'Get started'}).click();
await homePage.clickStart(); // await homePage.clickStart();
// Expects page to have a heading with the name of Installation. await page.waitForTimeout(3000);
await expect(page.getByRole("heading", { name: "Installation" })).toBeVisible(); // Expects page to have a heading with the name of Installation.
await expect(page.getByRole('button', { name: /登\s录/ })).not.toBeVisible();
}); });