chore: allow passing path to property in toHaveJSProperty (#27495)
Fixes https://github.com/microsoft/playwright/issues/27487
This commit is contained in:
parent
7381099ad1
commit
b807c974c3
|
|
@ -1249,7 +1249,14 @@ export class InjectedScript {
|
||||||
{
|
{
|
||||||
// JS property
|
// JS property
|
||||||
if (expression === 'to.have.property') {
|
if (expression === 'to.have.property') {
|
||||||
const received = (element as any)[options.expressionArg];
|
let target = element;
|
||||||
|
const properties = options.expressionArg.split('.');
|
||||||
|
for (let i = 0; i < properties.length - 1; i++) {
|
||||||
|
if (typeof target !== 'object' || !(properties[i] in target))
|
||||||
|
return { received: undefined, matches: false };
|
||||||
|
target = (target as any)[properties[i]];
|
||||||
|
}
|
||||||
|
const received = (target as any)[properties[properties.length - 1]];
|
||||||
const matches = deepEquals(received, options.expectedValue);
|
const matches = deepEquals(received, options.expectedValue);
|
||||||
return { received, matches };
|
return { received, matches };
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { stripAnsi } from '../config/utils';
|
||||||
import { test, expect } from './pageTest';
|
import { test, expect } from './pageTest';
|
||||||
|
|
||||||
test.describe('toHaveCount', () => {
|
test.describe('toHaveCount', () => {
|
||||||
|
|
@ -165,6 +166,26 @@ test.describe('toHaveJSProperty', () => {
|
||||||
const locator = page.locator('div');
|
const locator = page.locator('div');
|
||||||
await expect(locator).toHaveJSProperty('foo', null);
|
await expect(locator).toHaveJSProperty('foo', null);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('pass nested', async ({ page }) => {
|
||||||
|
await page.setContent('<div></div>');
|
||||||
|
await page.$eval('div', e => (e as any).foo = { nested: { a: 1, b: 'string', c: new Date(1627503992000) } });
|
||||||
|
const locator = page.locator('div');
|
||||||
|
await expect(locator).toHaveJSProperty('foo.nested', { a: 1, b: 'string', c: new Date(1627503992000) });
|
||||||
|
await expect(locator).toHaveJSProperty('foo.nested.a', 1);
|
||||||
|
await expect(locator).toHaveJSProperty('foo.nested.b', 'string');
|
||||||
|
await expect(locator).toHaveJSProperty('foo.nested.c', new Date(1627503992000));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('fail nested', async ({ page }) => {
|
||||||
|
await page.setContent('<div></div>');
|
||||||
|
await page.$eval('div', e => (e as any).foo = { nested: { a: 1, b: 'string', c: new Date(1627503992000) } });
|
||||||
|
const locator = page.locator('div');
|
||||||
|
const error1 = await expect(locator).toHaveJSProperty('foo.bar', { a: 1, b: 'string', c: new Date(1627503992001) }, { timeout: 1000 }).catch(e => e);
|
||||||
|
expect.soft(stripAnsi(error1.message)).toContain(`Received: undefined`);
|
||||||
|
const error2 = await expect(locator).toHaveJSProperty('foo.nested.a', 2, { timeout: 1000 }).catch(e => e);
|
||||||
|
expect.soft(stripAnsi(error2.message)).toContain(`Received: 1`);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test.describe('toHaveClass', () => {
|
test.describe('toHaveClass', () => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue