Skip to content

Add "label=" locator strategy #533

@boakley

Description

@boakley

I would like to have another locator strategy added, tentatively named "label". This would look for a label element with the given text, then return the element pointed to by the "for" attribute of that label element.

Rationale:

Django (and other frameworks?) create forms that look like this:

<label for="id_some_form_field">I accept the terms</label>
<input id='id_some_form_field' type='checkbox'>

I would like to interact with the form field by specifying the label rather than having to use some other locator. For example, to validate that this checkbox is checked I want to do something like this:

click element    label=I accept the terms
checkbox should be selected    label=I accept the terms

I think this would make it easier for people who didn't write the code be able to write tests, because they wouldn't have to know the underlying element IDs; instead, they can simply enter in what they see on the screen.

Further, I recommend we support the stripping of leading and trailing spaces and dots to allow for a simple type of pattern matching. Say, for example, you have a form field that has the label "I have read the terms of service and agree to be bound by them". It would be cumbersome to type all of that into a test case, so it would be nice to support something like this:

checkbox should be selected    label=I have read the terms of service...

I am willing to write the implementation. This issue is for generating discussion, and to see if this would be a generally useful locator strategy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions