/** * Copyright (c) Microsoft Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { stripAnsi } from '../config/utils'; import { test, expect } from './pageTest'; test.describe('toBeChecked', () => { test('default', async ({ page }) => { await page.setContent(''); const locator = page.locator('input'); await expect(locator).toBeChecked(); }); test('with checked:true', async ({ page }) => { await page.setContent(''); const locator = page.locator('input'); await expect(locator).toBeChecked({ checked: true }); }); test('with checked:false', async ({ page }) => { await page.setContent(''); const locator = page.locator('input'); await expect(locator).not.toBeChecked({ checked: false }); }); test('with indeterminate:true', async ({ page }) => { await page.setContent(''); await page.locator('input').evaluate((e: HTMLInputElement) => e.indeterminate = true); const locator = page.locator('input'); await expect(locator).toBeChecked({ indeterminate: true }); }); test('with indeterminate:true and checked', async ({ page }) => { await page.setContent(''); await page.locator('input').evaluate((e: HTMLInputElement) => e.indeterminate = true); const locator = page.locator('input'); const error = await expect(locator).toBeChecked({ indeterminate: true, checked: false }).catch(e => e); expect(error.message).toContain(`Can\'t assert indeterminate and checked at the same time`); }); test('fail', async ({ page }) => { await page.setContent(''); const locator = page.locator('input'); const error = await expect(locator).toBeChecked({ timeout: 1000 }).catch(e => e); expect(stripAnsi(error.message)).toContain(`Timed out 1000ms waiting for expect(locator).toBeChecked()`); expect(stripAnsi(error.message)).toContain(`- Expect "toBeChecked" with timeout 1000ms`); }); test('with not', async ({ page }) => { await page.setContent(''); const locator = page.locator('input'); await expect(locator).not.toBeChecked(); }); test('with not and checked:false', async ({ page }) => { await page.setContent(''); const locator = page.locator('input'); await expect(locator).toBeChecked({ checked: false }); }); test('fail with not', async ({ page }) => { await page.setContent(''); const locator = page.locator('input'); const error = await expect(locator).not.toBeChecked({ timeout: 1000 }).catch(e => e); expect(stripAnsi(error.message)).toContain(`Timed out 1000ms waiting for expect(locator).not.toBeChecked()`); expect(stripAnsi(error.message)).toContain(`- Expect "not toBeChecked" with timeout 1000ms`); expect(stripAnsi(error.message)).toContain(`locator resolved to `); }); test('fail with checked:false', async ({ page }) => { await page.setContent(''); const locator = page.locator('input'); const error = await expect(locator).toBeChecked({ checked: false, timeout: 1000 }).catch(e => e); expect(stripAnsi(error.message)).toContain(`Timed out 1000ms waiting for expect(locator).toBeChecked({ checked: false })`); expect(stripAnsi(error.message)).toContain(`- Expect "toBeChecked" with timeout 1000ms`); expect(stripAnsi(error.message)).toContain(`locator resolved to `); }); test('fail with indeterminate: true', async ({ page }) => { await page.setContent(''); const locator = page.locator('input'); const error = await expect(locator).toBeChecked({ indeterminate: true, timeout: 1000 }).catch(e => e); expect(stripAnsi(error.message)).toContain(`Timed out 1000ms waiting for expect(locator).toBeChecked({ indeterminate: true })`); expect(stripAnsi(error.message)).toContain(`- Expect "toBeChecked" with timeout 1000ms`); }); test('fail missing', async ({ page }) => { await page.setContent('
no inputs here
'); const locator2 = page.locator('input2'); const error = await expect(locator2).not.toBeChecked({ timeout: 1000 }).catch(e => e); expect(stripAnsi(error.message)).toContain(`Timed out 1000ms waiting for expect(locator).not.toBeChecked()`); expect(stripAnsi(error.message)).toContain(`- Expect "not toBeChecked" with timeout 1000ms`); expect(stripAnsi(error.message)).toContain(`- waiting for locator(\'input2\')`); }); test('with role', async ({ page }) => { for (const role of ['checkbox', 'menuitemcheckbox', 'option', 'radio', 'switch', 'menuitemradio', 'treeitem']) { await test.step(`role=${role}`, async () => { await page.setContent(`
I am checked
`); const locator = page.locator('div'); await expect(locator).toBeChecked(); }); } }); test('friendly log', async ({ page }) => { await page.setContent(''); const message1 = await expect(page.locator('input')).toBeChecked({ timeout: 1000 }).catch(e => e.message); expect(message1).toContain('unexpected value "unchecked"'); await page.setContent(''); const message2 = await expect(page.locator('input')).toBeChecked({ checked: false, timeout: 1000 }).catch(e => e.message); expect(message2).toContain('unexpected value "checked"'); }); test('with impossible timeout', async ({ page }) => { await page.setContent(''); await expect(page.locator('input')).toBeChecked({ timeout: 1 }); }); test('with impossible timeout .not', async ({ page }) => { await page.setContent(''); await expect(page.locator('input')).not.toBeChecked({ timeout: 1 }); }); }); test.describe('toBeEditable', () => { test('default', async ({ page }) => { await page.setContent(''); const locator = page.locator('input'); await expect(locator).toBeEditable(); }); test('with not', async ({ page }) => { await page.setContent(''); const locator = page.locator('input'); await expect(locator).not.toBeEditable(); }); test('with editable:true', async ({ page }) => { await page.setContent(''); const locator = page.locator('input'); await expect(locator).toBeEditable({ editable: true }); }); test('with editable:false', async ({ page }) => { await page.setContent(''); const locator = page.locator('input'); await expect(locator).toBeEditable({ editable: false }); }); test('with not and editable:false', async ({ page }) => { await page.setContent(''); const locator = page.locator('input'); await expect(locator).not.toBeEditable({ editable: false }); }); test('throws', async ({ page }) => { await page.setContent('