Skip to content

Commit

Permalink
Merge pull request #733 from simonihmig/wc-e2e
Browse files Browse the repository at this point in the history
Playwright e2e tests for wc-lit demo app
  • Loading branch information
simonihmig authored Nov 9, 2024
2 parents 7f43562 + 8fb1def commit 08fd775
Show file tree
Hide file tree
Showing 11 changed files with 517 additions and 277 deletions.
8 changes: 4 additions & 4 deletions apps/ember-vite/tests/acceptance/local-images-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module('Acceptance | local images', function (hooks) {
.hasAttribute(
'srcset',
new RegExp(`/assets/aurora-640w(-[a-zA-Z0-9-_]+)?.${ext} 2x`),
`has ${type} with a width of 1x`
`has ${type} with a width of 2x`
);
}
});
Expand Down Expand Up @@ -147,7 +147,7 @@ module('Acceptance | local images', function (hooks) {
.hasAttribute(
'srcset',
new RegExp(`/assets/aurora-640w(-[a-zA-Z0-9-_]+)?.${ext} 2x`),
`has ${type} with a width of 1x`
`has ${type} with a width of 2x`
);
}
});
Expand Down Expand Up @@ -183,7 +183,7 @@ module('Acceptance | local images', function (hooks) {
.hasAttribute(
'srcset',
new RegExp(`/assets/aurora-640w(-[a-zA-Z0-9-_]+)?.${ext} 2x`),
`has ${type} with a width of 1x`
`has ${type} with a width of 2x`
);
}
});
Expand Down Expand Up @@ -218,7 +218,7 @@ module('Acceptance | local images', function (hooks) {
.hasAttribute(
'srcset',
new RegExp(`/assets/aurora-640w(-[a-zA-Z0-9-_]+)?.${ext} 2x`),
`has ${type} with a width of 1x`
`has ${type} with a width of 2x`
);
}
});
Expand Down
6 changes: 6 additions & 0 deletions apps/wc-lit/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ dist-ssr
*.njsproj
*.sln
*.sw?

# Playwright
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
6 changes: 5 additions & 1 deletion apps/wc-lit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"scripts": {
"start": "vite",
"build": "vite build",
"preview": "vite preview"
"preview": "vite preview",
"test": "playwright test",
"test:ui": "playwright test --ui"
},
"dependencies": {
"@responsive-image/cdn": "workspace:*",
Expand All @@ -15,7 +17,9 @@
"lit": "^3.2.1"
},
"devDependencies": {
"@playwright/test": "^1.48.2",
"@responsive-image/vite-plugin": "workspace:*",
"@types/node": "22.9.0",
"typescript": "5.6.3",
"vite": "5.4.10"
}
Expand Down
75 changes: 75 additions & 0 deletions apps/wc-lit/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { defineConfig, devices } from '@playwright/test';

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './tests',
snapshotPathTemplate:
'{testDir}/__screenshots__{/projectName}/{testFilePath}/{arg}{ext}',
/* 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://localhost:4173',

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

testIdAttribute: '',
},

/* 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: {
// serve the prod build, as the dev build uses vite middleware URLs for images that we don't want in tests
command: 'pnpm build && pnpm preview',
url: 'http://localhost:4173',
},
});
1 change: 0 additions & 1 deletion apps/wc-lit/public/vite.svg

This file was deleted.

7 changes: 6 additions & 1 deletion apps/wc-lit/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class MyApp extends LitElement {
<h2>Local</h2>
<responsive-image .src=${image}></responsive-image>
<responsive-image .src=${image} data-test-local-image="responsive"></responsive-image>
<responsive-image
.src=${image}
width=320
Expand Down Expand Up @@ -75,6 +75,11 @@ export class MyApp extends LitElement {
`;
}

// no shadow DOM needed
protected createRenderRoot() {
return this;
}

static styles = css``;
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
214 changes: 214 additions & 0 deletions apps/wc-lit/tests/responsive-image.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
import { test, expect } from '@playwright/test';

const sizes = [640, 750, 828, 1080, 1200, 1920, 2048, 3840];
const imageTypes = [
['jpeg', 'jpg'],
['webp', 'webp'],
];

test('responsive layout', async ({ page }) => {
await page.goto('/');

const img = page.locator('[data-test-local-image="responsive"] img');

await expect(img).toHaveClass(/ri-responsive/);
await expect(img).toHaveAttribute(
'src',
new RegExp(`/assets/aurora-[0-9]+w(-[a-zA-Z0-9-_]+)?.jpg`),
);
await expect(img).toHaveScreenshot();

const picture = page.locator('[data-test-local-image="responsive"] picture');

for (const [type, ext] of imageTypes) {
for (const size of sizes) {
await expect(
picture.locator(`source[type="image/${type}"]`),
`has ${type} with a width of ${size}`,
).toHaveAttribute(
'srcset',
new RegExp(`/assets/aurora-${size}w(-[a-zA-Z0-9-_]+)?.${ext} ${size}w`),
);
}
}
});

test('fixed layout', async ({ page }) => {
await page.goto('/');

const img = page.locator('[data-test-local-image="fixed"] img');

await expect(img).toHaveClass(/ri-fixed/);
await expect(img).toHaveAttribute(
'src',
new RegExp(`/assets/aurora-[0-9]+w(-[a-zA-Z0-9-_]+)?.jpg`),
);
await expect(img).toHaveAttribute('width', '320');
await expect(img).toHaveAttribute('height', /213/);
await expect(img).toHaveScreenshot();

const picture = page.locator('[data-test-local-image="fixed"] picture');

for (const [type, ext] of imageTypes) {
for (const size of sizes) {
await expect(
picture.locator(`source[type="image/${type}"]`),
`has ${type} with a width of 1x`,
).toHaveAttribute(
'srcset',
new RegExp(`/assets/aurora-640w(-[a-zA-Z0-9-_]+)?.${ext} 1x`),
);

await expect(
picture.locator(`source[type="image/${type}"]`),
`has ${type} with a width of 2x`,
).toHaveAttribute(
'srcset',
new RegExp(`/assets/aurora-640w(-[a-zA-Z0-9-_]+)?.${ext} 2x`),
);
}
}
});

test('fixed layout w/ aspect', async ({ page }) => {
await page.goto('/');

const img = page.locator('[data-test-local-image="fixed,aspect"] img');

await expect(img).toHaveClass(/ri-fixed/);
await expect(img).toHaveAttribute(
'src',
new RegExp(`/assets/aurora-[0-9]+w(-[a-zA-Z0-9-_]+)?.jpg`),
);
await expect(img).toHaveAttribute('width', '320');
await expect(img).toHaveAttribute('height', '480');
await expect(img).toHaveScreenshot();

const picture = page.locator('[data-test-local-image="fixed"] picture');

for (const [type, ext] of imageTypes) {
for (const size of sizes) {
await expect(
picture.locator(`source[type="image/${type}"]`),
`has ${type} with a width of 1x`,
).toHaveAttribute(
'srcset',
new RegExp(`/assets/aurora-640w(-[a-zA-Z0-9-_]+)?.${ext} 1x`),
);

await expect(
picture.locator(`source[type="image/${type}"]`),
`has ${type} with a width of 2x`,
).toHaveAttribute(
'srcset',
new RegExp(`/assets/aurora-640w(-[a-zA-Z0-9-_]+)?.${ext} 2x`),
);
}
}
});

test.describe('LQIP', () => {
test('color', async ({ page }) => {
await page.goto('/');

const img = page.locator('[data-test-local-image="fixed,lqip-color"] img');

await expect(img).toHaveClass(/ri-fixed/);
await expect(img).toHaveAttribute(
'src',
new RegExp(`/assets/aurora-[0-9]+w(-[a-zA-Z0-9-_]+)?.jpg`),
);

const picture = page.locator('[data-test-local-image="fixed"] picture');

for (const [type, ext] of imageTypes) {
for (const size of sizes) {
await expect(
picture.locator(`source[type="image/${type}"]`),
`has ${type} with a width of 1x`,
).toHaveAttribute(
'srcset',
new RegExp(`/assets/aurora-640w(-[a-zA-Z0-9-_]+)?.${ext} 1x`),
);

await expect(
picture.locator(`source[type="image/${type}"]`),
`has ${type} with a width of 2x`,
).toHaveAttribute(
'srcset',
new RegExp(`/assets/aurora-640w(-[a-zA-Z0-9-_]+)?.${ext} 2x`),
);
}
}
});

test('inline', async ({ page }) => {
await page.goto('/');

const img = page.locator('[data-test-local-image="fixed,lqip-inline"] img');

await expect(img).toHaveClass(/ri-fixed/);
await expect(img).toHaveAttribute(
'src',
new RegExp(`/assets/aurora-[0-9]+w(-[a-zA-Z0-9-_]+)?.jpg`),
);

const picture = page.locator('[data-test-local-image="fixed"] picture');

for (const [type, ext] of imageTypes) {
for (const size of sizes) {
await expect(
picture.locator(`source[type="image/${type}"]`),
`has ${type} with a width of 1x`,
).toHaveAttribute(
'srcset',
new RegExp(`/assets/aurora-640w(-[a-zA-Z0-9-_]+)?.${ext} 1x`),
);

await expect(
picture.locator(`source[type="image/${type}"]`),
`has ${type} with a width of 2x`,
).toHaveAttribute(
'srcset',
new RegExp(`/assets/aurora-640w(-[a-zA-Z0-9-_]+)?.${ext} 2x`),
);
}
}
});

test('blurhash', async ({ page }) => {
await page.goto('/');

const img = page.locator(
'[data-test-local-image="fixed,lqip-blurhash"] img',
);

await expect(img).toHaveClass(/ri-fixed/);
await expect(img).toHaveAttribute(
'src',
new RegExp(`/assets/aurora-[0-9]+w(-[a-zA-Z0-9-_]+)?.jpg`),
);

const picture = page.locator('[data-test-local-image="fixed"] picture');

for (const [type, ext] of imageTypes) {
for (const size of sizes) {
await expect(
picture.locator(`source[type="image/${type}"]`),
`has ${type} with a width of 1x`,
).toHaveAttribute(
'srcset',
new RegExp(`/assets/aurora-640w(-[a-zA-Z0-9-_]+)?.${ext} 1x`),
);

await expect(
picture.locator(`source[type="image/${type}"]`),
`has ${type} with a width of 2x`,
).toHaveAttribute(
'srcset',
new RegExp(`/assets/aurora-640w(-[a-zA-Z0-9-_]+)?.${ext} 2x`),
);
}
}
});
});
Loading

0 comments on commit 08fd775

Please sign in to comment.