Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getByRole('form') should work without "name" attribute #937

Closed
kentcdodds opened this issue Apr 28, 2021 · 10 comments
Closed

getByRole('form') should work without "name" attribute #937

kentcdodds opened this issue Apr 28, 2021 · 10 comments

Comments

@kentcdodds
Copy link
Member

  • @testing-library/dom version: all
  • Testing Framework and version: Any
  • DOM Environment: Any

Relevant code or config:

// this passes, and that's great:
test('form with name', () => {
  const form = document.createElement('form')
  form.name = 'anything'
  const container = document.createElement('div')
  container.append(form)
  within(container).getByRole('form')
})

// this fails, but I think it should pass:
test('form without name', () => {
  const form = document.createElement('form')
  const container = document.createElement('div')
  container.append(form)
  within(container).getByRole('form')
})

What you did:

Originally I thought that a form didn't actually have the accessible role "form" unless they had a name, but I just did a quick check in the devtools and that appears to not be the case:

image

I did a bit of research and it appears there aren't any strong reasons to use a name attribute for a form, so it seems an odd limitation for us to have.

What happened:

Test fails.

Reproduction:

Put the above snippet in any jest environment you have working with testing library.

Alternatively, you'll find the same behavior in the browser as well: https://jsbin.com/ceyifo/edit?html,output

<!DOCTYPE html>
<html>
<head>
  <script src="https://unpkg.com/@testing-library/dom@7.30.4/dist/@testing-library/dom.umd.js"></script>
</head>
<body>
  <div>Check the console</div>
  <script>
    function withName() {
      const form = document.createElement('form')
      form.name = 'anything'
      const container = document.createElement('div')
      container.append(form)
      TestingLibraryDom.within(container).getByRole('form')
    }
    
    function withoutName() {
      const form = document.createElement('form')
      const container = document.createElement('div')
      container.append(form)
      TestingLibraryDom.within(container).getByRole('form')
    }
    
    try {
      withName()
      console.log('withName passes')
    } catch (error) {
      console.error('withName fails', error)
    }
    try {
      withoutName()
      console.log('withoutName passes')
    } catch (error) {
      console.error('withoutName fails', error)
    }
  </script>
</body>
</html>

Problem description:

It seems odd to me that a form requires a name attribute to be retrievable by a role query when it appears that the browser considers it to have a role of form.

Suggested solution:

Update getByRole to match forms without a name.

@mt9eet
Copy link

mt9eet commented May 1, 2021

@kentcdodds I think this is expected, or at least that's what the spec says.

Screen Shot 2021-05-02 at 05 37 49

@kentcdodds
Copy link
Member Author

Ah! That's interesting. So this sounds more like a bug in Chrome's accessibility devtools than a DTL bug 😅

Thanks for finding that @mt9eet! 👏

@ahsatha
Copy link

ahsatha commented Oct 13, 2021

I'm sorry to comment here but I'm trying to get a form by the role and name by setting the name prop on it and, it seems like it can't find it though it gets listed in the output

const form = getByRole('form', { name: /login/i })

image

Update
This works:

const form = getByRole('form', { name: "" })

@MatanBobi
Copy link
Member

MatanBobi commented Oct 14, 2021

@ahsatha thanks for reaching out.
This issue you're experiencing isn't really related to the one described in this issue.
The reason you're seeing this is because the name attribute on a form doesn't represent the accessible name.
You can read in the accessible name computation spec.
If I'm not mistaken, you need an aria-label an aria-labelledyby or a title.

image

@alexkrolick
Copy link
Collaborator

This is a good gotcha to mention in the docs pages somewhere.

@MatanBobi
Copy link
Member

MatanBobi commented Oct 16, 2021

@alexkrolick that's actually already in the docs page in the *ByRole page:

image

Do you think that's enough?

victorg1991 added a commit to victorg1991/liferay-portal that referenced this issue Dec 21, 2021
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
victorg1991 added a commit to victorg1991/liferay-portal that referenced this issue Dec 21, 2021
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
- waitForElement has been removed, now we can use find* variants, which return a promise, or waitFor
victorg1991 added a commit to victorg1991/liferay-portal that referenced this issue Dec 21, 2021
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
- waitForElement has been removed, now we can use find* variants, which return a promise, or waitFor
victorg1991 added a commit to victorg1991/liferay-portal that referenced this issue Dec 22, 2021
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
victorg1991 added a commit to victorg1991/liferay-portal that referenced this issue Dec 22, 2021
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
- waitForElement has been removed, now we can use find* variants, which return a promise, or waitFor
victorg1991 added a commit to victorg1991/liferay-portal that referenced this issue Dec 22, 2021
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
- waitForElement has been removed, now we can use find* variants, which return a promise, or waitFor
victorg1991 added a commit to victorg1991/liferay-portal that referenced this issue Jan 3, 2022
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
victorg1991 added a commit to victorg1991/liferay-portal that referenced this issue Jan 3, 2022
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
- waitForElement has been removed, now we can use find* variants, which return a promise, or waitFor
victorg1991 added a commit to victorg1991/liferay-portal that referenced this issue Jan 3, 2022
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
- waitForElement has been removed, now we can use find* variants, which return a promise, or waitFor
victorg1991 added a commit to victorg1991/liferay-portal that referenced this issue Jan 4, 2022
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
victorg1991 added a commit to victorg1991/liferay-portal that referenced this issue Jan 4, 2022
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
- waitForElement has been removed, now we can use find* variants, which return a promise, or waitFor
victorg1991 added a commit to victorg1991/liferay-portal that referenced this issue Jan 4, 2022
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
- waitForElement has been removed, now we can use find* variants, which return a promise, or waitFor
victorg1991 added a commit to victorg1991/liferay-portal that referenced this issue Jan 6, 2022
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
victorg1991 added a commit to victorg1991/liferay-portal that referenced this issue Jan 6, 2022
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
- waitForElement has been removed, now we can use find* variants, which return a promise, or waitFor
victorg1991 added a commit to victorg1991/liferay-portal that referenced this issue Jan 6, 2022
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
- waitForElement has been removed, now we can use find* variants, which return a promise, or waitFor
victorg1991 added a commit to victorg1991/liferay-portal that referenced this issue Jan 6, 2022
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
victorg1991 added a commit to victorg1991/liferay-portal that referenced this issue Jan 6, 2022
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
- waitForElement has been removed, now we can use find* variants, which return a promise, or waitFor
victorg1991 added a commit to victorg1991/liferay-portal that referenced this issue Jan 6, 2022
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
- waitForElement has been removed, now we can use find* variants, which return a promise, or waitFor
brianchandotcom pushed a commit to brianchandotcom/liferay-portal that referenced this issue Jan 10, 2022
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
brianchandotcom pushed a commit to brianchandotcom/liferay-portal that referenced this issue Jan 10, 2022
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
- waitForElement has been removed, now we can use find* variants, which return a promise, or waitFor
brianchandotcom pushed a commit to brianchandotcom/liferay-portal that referenced this issue Jan 10, 2022
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
- waitForElement has been removed, now we can use find* variants, which return a promise, or waitFor
DiegoHu97 pushed a commit to DiegoHu97/liferay-portal that referenced this issue Jan 11, 2022
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
DiegoHu97 pushed a commit to DiegoHu97/liferay-portal that referenced this issue Jan 11, 2022
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
- waitForElement has been removed, now we can use find* variants, which return a promise, or waitFor
DiegoHu97 pushed a commit to DiegoHu97/liferay-portal that referenced this issue Jan 11, 2022
- cleanup is done automatically after each test
- only forms with an accessible name have the role "form"
   Check: https://www.w3.org/TR/html-aria/#docconformance and testing-library/dom-testing-library#937
- waitForElement has been removed, now we can use find* variants, which return a promise, or waitFor
@challet
Copy link

challet commented Oct 22, 2024

image

Setting a title still makes the form hidden from getByRole("form")

Copy link

Uh oh! @challet, the image you shared is missing helpful alt text. Check #937 (comment).

Alt text is an invisible description that helps screen readers describe images to blind or low-vision users. If you are using markdown to display images, add your alt text inside the brackets of the markdown image.

Learn more about alt text at Basic writing and formatting syntax: images on GitHub Docs.

@MatanBobi
Copy link
Member

Thanks @challet. This looks like an issue with aria-query: https://github.com/A11yance/aria-query/blob/main/src/etc/roles/literal/formRole.js

Maybe @jlp-craigmorten will know?

@jlp-craigmorten
Copy link
Contributor

jlp-craigmorten commented Oct 28, 2024

Exactly as @MatanBobi suggests, the role of form currently has constraints set within aria-query which only cater to:

  1. aria-label
  2. aria-labelledby
  3. name

it seems. So title is not being considered.

See also #1293 which might be a better place to track this rather this older closed issue? There is an open draft MR A11yance/aria-query#547 to try address the fact that the implicit form role conditions appear to have changed whereby it is not now necessary to name a form to get a form role. If this lands it would make the missing title constraint moot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants