Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feat(ct): vue set props (#16058)

sand4rt4a47e275c817
diff --git a/packages/playwright-ct-vue/index.d.ts b/packages/playwright-ct-vue/index.d.ts
index 5981c4725d..918fe4f012 100644
--- a/packages/playwright-ct-vue/index.d.ts
+++ b/packages/playwright-ct-vue/index.d.ts
@@ -49,6 +49,7 @@ export interface ComponentFixtures {
hooksConfig?: any,
}): Promise<Locator>;
unmount(locator: Locator): Promise<void>;
+ setProps<Props = { [key: string]: any }>(locator: Locator, props: Props): Promise<void>
}
export const test: TestType<
diff --git a/packages/playwright-ct-vue/registerSource.mjs b/packages/playwright-ct-vue/registerSource.mjs
index b33eb0d5b8..057d54b70a 100644
--- a/packages/playwright-ct-vue/registerSource.mjs
+++ b/packages/playwright-ct-vue/registerSource.mjs
@@ -108,14 +108,14 @@ function render(component) {
if (component.kind === 'object') {
// Vue test util syntax.
- for (const [key, value] of Object.entries(component.options.slots || {})) {
+ for (const [key, value] of Object.entries(component.options?.slots || {})) {
if (key === 'default')
children.push(value);
else
slots[key] = value;
}
- props = component.options.props || {};
- for (const [key, value] of Object.entries(component.options.on || {}))
+ props = component.options?.props || {};
+ for (const [key, value] of Object.entries(component.options?.on || {}))
listeners[key] = value;
}
@@ -155,9 +155,13 @@ function createDevTools() {
};
}
+const appKey = Symbol('appKey');
+const componentKey = Symbol('componentKey');
+
window.playwrightMount = async (component, rootElement, hooksConfig) => {
+ const wrapper = render(component);
const app = createApp({
- render: () => render(component)
+ render: () => wrapper
});
setDevtoolsHook(createDevTools(), {});
@@ -165,15 +169,23 @@ window.playwrightMount = async (component, rootElement, hooksConfig) => {
await hook({ app, hooksConfig });
const instance = app.mount(rootElement);
instance.$el[appKey] = app;
+ instance.$el[componentKey] = wrapper;
for (const hook of /** @type {any} */(window).__pw_hooks_after_mount || [])
await hook({ app, hooksConfig, instance });
};
-window.playwrightUnmount = async element => {
+window.playwrightUnmount = async element => {
const app = /** @type {import('vue').App} */ (element[appKey]);
if (!app)
throw new Error('Component was not mounted');
app.unmount();
};
-const appKey = Symbol('appKey');
+window.playwrightSetProps = async (element, props) => {
+ const component = element[componentKey].component;
+ if (!component)
+ throw new Error('Component was not mounted');
+
+ for (const [key, value] of Object.entries(props))
+ component.props[key] = value;
+};
diff --git a/packages/playwright-test/src/mount.ts b/packages/playwright-test/src/mount.ts
index 4cc0cc6326..19347a6992 100644
--- a/packages/playwright-test/src/mount.ts
+++ b/packages/playwright-test/src/mount.ts
@@ -23,6 +23,7 @@ export const fixtures: Fixtures<
PlaywrightTestArgs & PlaywrightTestOptions & {
mount: (component: any, options: any) => Promise<Locator>;
unmount: (locator: Locator) => Promise<void>;
+ setProps: (locator: Locator, props: { [key: string]: any }) => Promise<void>;
},
PlaywrightWorkerArgs & PlaywrightWorkerOptions & { _ctWorker: { context: BrowserContext | undefined, hash: string } },
{ _contextFactory: (options?: BrowserContextOptions) => Promise<BrowserContext>, _contextReuseEnabled: boolean }> = {
@@ -61,6 +62,14 @@ export const fixtures: Fixtures<
});
});
},
+
+ setProps: async ({}, use) => {
+ await use(async (locator: Locator, props: { [key: string]: any }) => {
+ return await locator.evaluate(async (element, props) => {
+ return await window.playwrightSetProps(element, props);
+ }, props);
+ });
+ }
};
async function innerMount(page: Page, jsxOrType: JsxComponent | string, options: ObjectComponentOptions = {}): Promise<string> {
diff --git a/packages/playwright-test/types/component.d.ts b/packages/playwright-test/types/component.d.ts
index 872d1810c8..82fdec613c 100644
--- a/packages/playwright-test/types/component.d.ts
+++ b/packages/playwright-test/types/component.d.ts
@@ -40,5 +40,6 @@ declare global {
interface Window {
playwrightMount(component: Component, rootElement: Element, hooksConfig: any): Promise<void>;
playwrightUnmount(element: Element, rootElement: Element): Promise<void>;
+ playwrightSetProps(element: Element, props: { [key: string]: any }): Promise<void>;
}
}
diff --git a/tests/components/ct-vue-vite/src/notation-jsx.spec.tsx b/tests/components/ct-vue-vite/src/notation-jsx.spec.tsx
index 50c6de2caa..483140f559 100644
--- a/tests/components/ct-vue-vite/src/notation-jsx.spec.tsx
+++ b/tests/components/ct-vue-vite/src/notation-jsx.spec.tsx
@@ -10,6 +10,13 @@ test('props should work', async ({ mount }) => {
await expect(component).toContainText('Submit')
})
+test('update props should work', async ({ mount, setProps }) => {
+ const component = await mount(<Button title='Submit'></Button>);
+ await expect(component).toContainText('Submit');
+ await setProps(component, { title: 'Loading' });
+ await expect(component).toContainText('Loading');
+})
+
test('event should work', async ({ mount }) => {
const messages = []
const component = await mount(<Button title='Submit' v-on:submit={data => {
diff --git a/tests/components/ct-vue-vite/src/notation-vue.spec.js b/tests/components/ct-vue-vite/src/notation-vue.spec.js
index 1bfe754604..6d208ec374 100644
--- a/tests/components/ct-vue-vite/src/notation-vue.spec.js
+++ b/tests/components/ct-vue-vite/src/notation-vue.spec.js
@@ -17,6 +17,17 @@ test('props should work', async ({ mount }) => {
await expect(component).toContainText('Submit')
})
+test('update props should work', async ({ mount, setProps }) => {
+ const component = await mount(Button, {
+ props: {
+ title: 'Submit'
+ }
+ });
+ await expect(component).toContainText('Submit');
+ await setProps(component, { title: 'Loading' });
+ await expect(component).toContainText('Loading');
+})
+
test('event should work', async ({ mount }) => {
const messages = []
const component = await mount(Button, {
diff --git a/tests/components/ct-vue-vite/src/notation-vue.spec.ts b/tests/components/ct-vue-vite/src/notation-vue.spec.ts
index 8e946a70da..20a1a6e992 100644
--- a/tests/components/ct-vue-vite/src/notation-vue.spec.ts
+++ b/tests/components/ct-vue-vite/src/notation-vue.spec.ts
@@ -16,6 +16,17 @@ test('props should work', async ({ mount }) => {
await expect(component).toContainText('Submit')
})
+test('update props should work', async ({ mount, setProps }) => {
+ const component = await mount(Button, {
+ props: {
+ title: 'Submit'
+ }
+ });
+ await expect(component).toContainText('Submit')
+ await setProps(component, { title: 'Loading' });
+ await expect(component).toContainText('Loading');
+})
+
test('event should work', async ({ mount }) => {
const messages = []
const component = await mount(Button, {