feat(assertions): support toBeEnabled({ enabled }) (#17058)

This commit is contained in:
Dmitry Gozman 2022-09-06 11:40:34 -07:00 committed by GitHub
parent f9b2fe38e3
commit 306ab34aa3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 33 deletions

View file

@ -593,6 +593,9 @@ var locator = Page.Locator("button.submit");
await Expect(locator).toBeEnabledAsync(); await Expect(locator).toBeEnabledAsync();
``` ```
### option: LocatorAssertions.toBeEnabled.enabled
* since: v1.26
- `enabled` <[boolean]>
### option: LocatorAssertions.toBeEnabled.timeout = %%-js-assertions-timeout-%% ### option: LocatorAssertions.toBeEnabled.timeout = %%-js-assertions-timeout-%%
* since: v1.18 * since: v1.18
### option: LocatorAssertions.toBeEnabled.timeout = %%-csharp-java-python-assertions-timeout-%% ### option: LocatorAssertions.toBeEnabled.timeout = %%-csharp-java-python-assertions-timeout-%%

View file

@ -78,10 +78,11 @@ export function toBeEmpty(
export function toBeEnabled( export function toBeEnabled(
this: ReturnType<Expect['getState']>, this: ReturnType<Expect['getState']>,
locator: LocatorEx, locator: LocatorEx,
options?: { timeout?: number }, options?: { enabled?: boolean, timeout?: number },
) { ) {
return toBeTruthy.call(this, 'toBeEnabled', locator, 'Locator', async (isNot, timeout, customStackTrace) => { return toBeTruthy.call(this, 'toBeEnabled', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
return await locator._expect(customStackTrace, 'to.be.enabled', { isNot, timeout }); const enabled = !options || options.enabled === undefined || options.enabled === true;
return await locator._expect(customStackTrace, enabled ? 'to.be.enabled' : 'to.be.disabled', { isNot, timeout });
}, options); }, options);
} }

View file

@ -3308,6 +3308,8 @@ interface LocatorAssertions {
* @param options * @param options
*/ */
toBeEnabled(options?: { toBeEnabled(options?: {
enabled?: boolean;
/** /**
* Time to retry the assertion for. Defaults to `timeout` in `TestConfig.expect`. * Time to retry the assertion for. Defaults to `timeout` in `TestConfig.expect`.
*/ */

View file

@ -84,41 +84,61 @@ test('toBeEditable', async ({ page }) => {
await expect(locator).toBeEditable(); await expect(locator).toBeEditable();
}); });
test('toBeEnabled', async ({ page }) => { test.describe('toBeEnabled', () => {
await page.setContent('<button>Text</button>'); test('default', async ({ page }) => {
const locator = page.locator('button'); await page.setContent('<button>Text</button>');
await expect(locator).toBeEnabled(); const locator = page.locator('button');
}); await expect(locator).toBeEnabled();
});
test('toBeEnabled failed', async ({ page }) => { test('with enabled:true', async ({ page }) => {
await page.setContent('<button disabled>Text</button>'); await page.setContent('<button>Text</button>');
const locator = page.locator('button'); const locator = page.locator('button');
const error = await expect(locator).toBeEnabled({ timeout: 1000 }).catch(e => e); await expect(locator).toBeEnabled({ enabled: true });
expect(error.message).toContain(`selector resolved to <button disabled>Text</button>`); });
});
test('toBeEnabled eventually', async ({ page }) => { test('with enabled:false', async ({ page }) => {
await page.setContent('<button disabled>Text</button>'); await page.setContent('<button disabled>Text</button>');
const locator = page.locator('button'); const locator = page.locator('button');
setTimeout(() => { await expect(locator).toBeEnabled({ enabled: false });
locator.evaluate(e => e.removeAttribute('disabled')).catch(() => {}); });
}, 500);
await expect(locator).toBeEnabled();
});
test('not.toBeEnabled eventually', async ({ page }) => { test('failed', async ({ page }) => {
await page.setContent('<button>Text</button>'); await page.setContent('<button disabled>Text</button>');
const locator = page.locator('button'); const locator = page.locator('button');
setTimeout(() => { const error = await expect(locator).toBeEnabled({ timeout: 1000 }).catch(e => e);
locator.evaluate(e => e.setAttribute('disabled', '')).catch(() => {}); expect(error.message).toContain(`selector resolved to <button disabled>Text</button>`);
}, 500); });
await expect(locator).not.toBeEnabled();
});
test('toBeDisabled', async ({ page }) => { test('eventually', async ({ page }) => {
await page.setContent('<button disabled>Text</button>'); await page.setContent('<button disabled>Text</button>');
const locator = page.locator('button'); const locator = page.locator('button');
await expect(locator).toBeDisabled(); setTimeout(() => {
locator.evaluate(e => e.removeAttribute('disabled')).catch(() => {});
}, 500);
await expect(locator).toBeEnabled();
});
test('eventually with not', async ({ page }) => {
await page.setContent('<button>Text</button>');
const locator = page.locator('button');
setTimeout(() => {
locator.evaluate(e => e.setAttribute('disabled', '')).catch(() => {});
}, 500);
await expect(locator).not.toBeEnabled();
});
test('with not and enabled:false', async ({ page }) => {
await page.setContent('<button>Text</button>');
const locator = page.locator('button');
await expect(locator).not.toBeEnabled({ enabled: false });
});
test('toBeDisabled', async ({ page }) => {
await page.setContent('<button disabled>Text</button>');
const locator = page.locator('button');
await expect(locator).toBeDisabled();
});
}); });
test('toBeEmpty input', async ({ page }) => { test('toBeEmpty input', async ({ page }) => {

View file

@ -222,6 +222,13 @@ test('should propose only the relevant matchers when custom expect matcher class
await test.expect(page).not.toBeEnabled(); await test.expect(page).not.toBeEnabled();
await test.expect(page.locator('foo')).toBeEnabled(); await test.expect(page.locator('foo')).toBeEnabled();
await test.expect(page.locator('foo')).toBeEnabled({ enabled: false });
await test.expect(page.locator('foo')).not.toBeEnabled({ enabled: true });
// @ts-expect-error
await test.expect(page.locator('foo')).toBeEnabled({ unknown: false });
// @ts-expect-error
await test.expect(page.locator('foo')).toBeEnabled({ enabled: 'foo' });
await test.expect(page.locator('foo')).toBe(true); await test.expect(page.locator('foo')).toBe(true);
// @ts-expect-error // @ts-expect-error
await test.expect(page.locator('foo')).toHaveURL('https://example.com'); await test.expect(page.locator('foo')).toHaveURL('https://example.com');