Skip to content

Commit 6a0d37a

Browse files
authored
Simplify setup; example login spec (#2)
1 parent 3257bc3 commit 6a0d37a

8 files changed

Lines changed: 96 additions & 67 deletions

File tree

.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
BASE_URL=https://example.com
1+
BASE_URL=https://s2testorg.stridespace.com
2+
PASSWORD=testuser

.github/workflows/ci.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,20 @@ jobs:
3131
run: npx playwright install --with-deps chromium
3232

3333
- name: Run Playwright tests
34+
env:
35+
BASE_URL: ${{ vars.BASE_URL }}
36+
PASSWORD: ${{ secrets.PASSWORD }}
3437
run: npm test
3538

3639
- name: Setup Testspace client
40+
if: always()
3741
uses: testspace-com/setup-testspace@v1
3842
with:
3943
domain: samples.stridespace.com
4044

4145
- name: Publish results to Testspace
42-
run: testspace test-results/junit/results.xml
4346
if: always()
47+
run: testspace test-results/junit/results.xml
4448

4549
- name: Upload JUnit report
4650
if: always()

README.md

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,40 @@ Minimal Playwright setup for JavaScript-based browser scenarios with:
1313
- Node.js 20+
1414
- npm 10+
1515

16-
## Initial setup
16+
## Getting started
1717

18-
If starting from an empty folder, create a project manifest:
18+
Clone the repo and install dependencies:
1919

2020
```bash
21-
npm init -y
21+
npm install
2222
```
2323

24-
Install project dependencies:
24+
Install the browser:
2525

2626
```bash
27-
npm install --save-dev @playwright/test dotenv
27+
npx playwright install chromium
2828
```
2929

30-
Optional: create a local environment file from the example:
30+
Copy the environment file and set `BASE_URL` to your target server:
3131

3232
```bash
3333
cp .env.example .env
3434
```
3535

36-
Install the browser used by this starter project:
36+
Run the hello-world verification test:
3737

3838
```bash
39-
npx playwright install chromium
39+
npm run test:hello
4040
```
4141

42-
Run the hello-world verification test:
42+
## Starting from scratch
43+
44+
If you are creating a new project rather than cloning this one:
4345

4446
```bash
45-
npm run test:hello
47+
npm init -y
48+
npm install --save-dev @playwright/test dotenv
49+
npx playwright install chromium
4650
```
4751

4852
## Running tests
@@ -59,24 +63,12 @@ Run a single file directly:
5963
npx playwright test tests/hello-world.spec.js
6064
```
6165

62-
Run headed mode:
63-
64-
```bash
65-
npm run test:headed
66-
```
67-
68-
Run with Playwright UI mode:
66+
Run with Playwright UI mode (interactive, useful for debugging):
6967

7068
```bash
7169
npm run test:ui
7270
```
7371

74-
Run in debug mode:
75-
76-
```bash
77-
npm run test:debug
78-
```
79-
8072
## Reports
8173

8274
Every test run writes:
@@ -120,22 +112,18 @@ CI publishes:
120112

121113
## Changing the target URL
122114

123-
The sample test uses `BASE_URL` and defaults to `https://example.com`.
124-
125-
You can store it in a `.env` file:
115+
`BASE_URL` must be set before running tests. Set it in your `.env` file:
126116

127117
```bash
128-
BASE_URL=https://example.com
118+
BASE_URL=https://your-server.example.com
129119
```
130120

131-
Example:
121+
Or pass it inline:
132122

133123
```bash
134-
BASE_URL=https://playwright.dev npm run test:hello
124+
BASE_URL=https://your-server.example.com npm test
135125
```
136126

137-
If you change `BASE_URL`, update the sample assertions to match that page.
138-
139127
## Project structure
140128

141129
```text

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
"description": "Starter setup for running JavaScript Playwright scenarios with HTML and JUnit reporting.",
66
"scripts": {
77
"test": "playwright test",
8-
"test:headed": "playwright test --headed",
9-
"test:ui": "playwright test --ui",
10-
"test:debug": "playwright test --debug",
118
"test:hello": "playwright test tests/hello-world.spec.js",
12-
"report:html": "playwright show-report playwright-report",
13-
"install:browsers": "playwright install chromium"
9+
"test:ui": "playwright test --ui",
10+
"report:html": "playwright show-report playwright-report"
1411
},
1512
"devDependencies": {
1613
"@playwright/test": "^1.60.0",

playwright.config.js

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,27 @@
11
require('dotenv').config();
22

3-
const { defineConfig, devices } = require('@playwright/test');
4-
const isWSL = !!process.env.WSL_DISTRO_NAME;
3+
if (!process.env.BASE_URL) {
4+
throw new Error('BASE_URL is not set. Copy .env.example to .env and update BASE_URL.');
5+
}
6+
7+
const { defineConfig } = require('@playwright/test');
58

69
module.exports = defineConfig({
710
testDir: './tests',
8-
timeout: 30_000,
9-
fullyParallel: true,
10-
forbidOnly: !!process.env.CI,
11-
retries: process.env.CI ? 2 : 0,
12-
workers: process.env.CI ? 1 : undefined,
11+
timeout: 60000,
12+
retries: 0,
13+
workers: 1,
1314
reporter: [
1415
['list'],
1516
['html', { outputFolder: 'playwright-report', open: 'never' }],
1617
['junit', { outputFile: 'test-results/junit/results.xml' }]
1718
],
1819
use: {
19-
baseURL: process.env.BASE_URL || 'https://example.com',
20-
trace: 'on-first-retry',
21-
screenshot: 'only-on-failure',
22-
video: 'retain-on-failure'
20+
baseURL: process.env.BASE_URL,
21+
browserName: 'chromium',
22+
headless: !!process.env.CI,
23+
viewport: { width: 1280, height: 720 },
24+
ignoreHTTPSErrors: true,
2325
},
24-
projects: [
25-
{
26-
name: 'chromium',
27-
use: {
28-
...devices['Desktop Chrome'],
29-
launchOptions: {
30-
args: [
31-
'--window-position=80,80',
32-
'--window-size=1440,900',
33-
'--start-maximized',
34-
...(isWSL ? ['--ozone-platform=x11', '--disable-gpu'] : [])
35-
]
36-
}
37-
}
38-
}
39-
],
4026
outputDir: 'test-results/artifacts'
4127
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const { test, expect } = require('@playwright/test');
2+
const { loginComponent } = require('../login-component');
3+
4+
test.describe("login", () => {
5+
test("login", async ({ page }) => {
6+
// TODO: Add structured test cases in the Validation panel to generate test stubs here.
7+
process.env.BASE_URL = process.env.BASE_URL || 'https://s2testorg.stridespace.com';
8+
process.env.PASSWORD = process.env.PASSWORD || '';
9+
await loginComponent(page);
10+
// Add assertions here
11+
});
12+
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Component: login
3+
* Description: Component to login into Testspace
4+
*
5+
* Steps:
6+
* 1. Navigate to $env.BASE_URL
7+
* 2. Click on username or email address
8+
* 3. Enter 'testuser' in username or email address
9+
* 4. Press Tab on username or email address
10+
* 5. Enter password
11+
* 6. Click on "SUBMIT"
12+
* 7. Submit signin form
13+
*/
14+
async function loginComponent(page) {
15+
const BASE_URL = process.env.BASE_URL;
16+
if (!BASE_URL) throw new Error('Environment variable BASE_URL is required but not defined');
17+
const PASSWORD = process.env.PASSWORD;
18+
if (!PASSWORD) throw new Error('Environment variable PASSWORD is required but not defined');
19+
20+
// Step 1: Navigate to $env.BASE_URL
21+
await page.goto(BASE_URL);
22+
23+
// Step 2: Click on username or email address
24+
// Step 3: Enter 'testuser' in username or email address
25+
await page.getByPlaceholder('username or email address').fill('testuser');
26+
27+
// Step 4: Press Tab on username or email address
28+
await page.getByPlaceholder('username or email address').press('Tab');
29+
30+
// Step 5: Enter password
31+
await page.getByPlaceholder('password').fill(PASSWORD);
32+
33+
// Step 6: Click on "SUBMIT"
34+
// Step 7: Submit signin form
35+
await page.getByRole('button', { name: 'Submit', exact: true }).click();
36+
// Form uses data-remote="true" (Rails UJS); Turbolinks follows the redirect as a top-level
37+
// navigation. Wait for the browser to leave the signin subdomain, then land on BASE_URL.
38+
await page.waitForURL(url => !url.hostname.startsWith('signin.'), { timeout: 30000 });
39+
await page.goto(BASE_URL);
40+
}
41+
42+
module.exports = { loginComponent };

tests/hello-world.spec.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ const { test, expect } = require('@playwright/test');
22

33
test('hello world smoke test', async ({ page }) => {
44
await page.goto('/');
5-
await expect(page).toHaveTitle(/Example Domain/);
6-
await expect(page.getByRole('heading', { name: 'Example Domain' })).toBeVisible();
5+
await expect(page).toHaveTitle(/.+/);
76
});

0 commit comments

Comments
 (0)