# 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 os
import sys
import pytest
from playwright.async_api import Page
async def test_accessibility_should_work(page: Page, is_firefox: bool, is_chromium: bool) -> None:
await page.set_content(
"""
Accessibility Test
Inputs
This is a description!
"""
)
# autofocus happens after a delay in chrome these days
await page.wait_for_function("document.activeElement.hasAttribute('autofocus')")
if is_firefox:
golden = {
"role": "document",
"name": "Accessibility Test",
"children": [
{"role": "heading", "name": "Inputs", "level": 1},
{"role": "textbox", "name": "Empty input", "focused": True},
{"role": "textbox", "name": "readonly input", "readonly": True},
{"role": "textbox", "name": "disabled input", "disabled": True},
{"role": "textbox", "name": "Input with whitespace", "value": " "},
{"role": "textbox", "name": "", "value": "value only"},
{
"role": "textbox",
"name": "",
"value": "and a value",
}, # firefox doesn't use aria-placeholder for the name
{
"role": "textbox",
"name": "",
"value": "and a value",
"description": "This is a description!",
}, # and here
],
}
elif is_chromium:
golden = {
"role": "WebArea",
"name": "Accessibility Test",
"children": [
{"role": "heading", "name": "Inputs", "level": 1},
{"role": "textbox", "name": "Empty input", "focused": True},
{"role": "textbox", "name": "readonly input", "readonly": True},
{"role": "textbox", "name": "disabled input", "disabled": True},
{"role": "textbox", "name": "Input with whitespace", "value": " "},
{"role": "textbox", "name": "", "value": "value only"},
{"role": "textbox", "name": "placeholder", "value": "and a value"},
{
"role": "textbox",
"name": "placeholder",
"value": "and a value",
"description": "This is a description!",
},
],
}
else:
golden = {
"role": "WebArea",
"name": "Accessibility Test",
"children": [
{"role": "heading", "name": "Inputs", "level": 1},
{"role": "textbox", "name": "Empty input", "focused": True},
{"role": "textbox", "name": "readonly input", "readonly": True},
{"role": "textbox", "name": "disabled input", "disabled": True},
{"role": "textbox", "name": "Input with whitespace", "value": " "},
{"role": "textbox", "name": "", "value": "value only"},
{"role": "textbox", "name": "placeholder", "value": "and a value"},
{
"role": "textbox",
"name": (
"placeholder"
if (
sys.platform == "darwin" and int(os.uname().release.split(".")[0]) >= 21
)
else "This is a description!"
),
"value": "and a value",
}, # webkit uses the description over placeholder for the name
],
}
assert await page.accessibility.snapshot() == golden
async def test_accessibility_should_work_with_regular_text(page: Page, is_firefox: bool) -> None:
await page.set_content("