Skip to content

Commit

Permalink
feat(assertions): support toBeEditable({ editable }) (#17065)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman committed Sep 6, 2022
1 parent 92f44e3 commit f0c5810
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 6 deletions.
3 changes: 3 additions & 0 deletions docs/src/api/class-locatorassertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,9 @@ var locator = Page.Locator("input");
await Expect(locator).ToBeEditableAsync();
```

### option: LocatorAssertions.toBeEditable.editable
* since: v1.26
- `editable` <[boolean]>
### option: LocatorAssertions.toBeEditable.timeout = %%-js-assertions-timeout-%%
* since: v1.18
### option: LocatorAssertions.toBeEditable.timeout = %%-csharp-java-python-assertions-timeout-%%
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,8 @@ export class InjectedScript {
elementState = progress.injectedScript.elementState(element, 'disabled');
} else if (expression === 'to.be.editable') {
elementState = progress.injectedScript.elementState(element, 'editable');
} else if (expression === 'to.be.readonly') {
elementState = !progress.injectedScript.elementState(element, 'editable');
} else if (expression === 'to.be.empty') {
if (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA')
elementState = !(element as HTMLInputElement).value;
Expand Down
5 changes: 3 additions & 2 deletions packages/playwright-test/src/matchers/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ export function toBeDisabled(
export function toBeEditable(
this: ReturnType<Expect['getState']>,
locator: LocatorEx,
options?: { timeout?: number },
options?: { editable?: boolean, timeout?: number },
) {
return toBeTruthy.call(this, 'toBeEditable', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
return await locator._expect(customStackTrace, 'to.be.editable', { isNot, timeout });
const editable = !options || options.editable === undefined || options.editable === true;
return await locator._expect(customStackTrace, editable ? 'to.be.editable' : 'to.be.readonly', { isNot, timeout });
}, options);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/playwright-test/types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3274,6 +3274,8 @@ interface LocatorAssertions {
* @param options
*/
toBeEditable(options?: {
editable?: boolean;

/**
* Time to retry the assertion for. Defaults to `timeout` in `TestConfig.expect`.
*/
Expand Down
34 changes: 30 additions & 4 deletions tests/page/expect-boolean.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,36 @@ test.describe('toBeChecked', () => {
});
});

test('toBeEditable', async ({ page }) => {
await page.setContent('<input></input>');
const locator = page.locator('input');
await expect(locator).toBeEditable();
test.describe('toBeEditable', () => {
test('default', async ({ page }) => {
await page.setContent('<input></input>');
const locator = page.locator('input');
await expect(locator).toBeEditable();
});

test('with not', async ({ page }) => {
await page.setContent('<input readonly></input>');
const locator = page.locator('input');
await expect(locator).not.toBeEditable();
});

test('with editable:true', async ({ page }) => {
await page.setContent('<input></input>');
const locator = page.locator('input');
await expect(locator).toBeEditable({ editable: true });
});

test('with editable:false', async ({ page }) => {
await page.setContent('<input readonly></input>');
const locator = page.locator('input');
await expect(locator).toBeEditable({ editable: false });
});

test('with not and editable:false', async ({ page }) => {
await page.setContent('<input></input>');
const locator = page.locator('input');
await expect(locator).not.toBeEditable({ editable: false });
});
});

test.describe('toBeEnabled', () => {
Expand Down
6 changes: 6 additions & 0 deletions tests/playwright-test/expect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ test('should propose only the relevant matchers when custom expect matcher class
await test.expect(res as any).toHaveURL('https://example.com');
// @ts-expect-error
await test.expect(123).toHaveURL('https://example.com');
await test.expect(page.locator('foo')).toBeChecked();
await test.expect(page.locator('foo')).not.toBeChecked({ checked: true });
await test.expect(page.locator('foo')).not.toBeEditable();
await test.expect(page.locator('foo')).toBeEditable({ editable: false });
});
`
});
Expand Down

0 comments on commit f0c5810

Please sign in to comment.