Search code examples
intern

Intern: Finding Dynamically Generated ID's Using For Attribute


I'm using the Intern with a website that dynamically generates IDs for input boxes, buttons, menu items, etc. The issue that I am facing is that I want to be able to find the "for" attribute of a label and find the DOM element it is pointing to. I want to find the input that is associated with the label. Is this possible with the intern? Or how would I write this test case?

     <div> 
<label for="element_1x12">
</label>
<input id="element_1x12">
</input>
<label for="element_1x13">
</label>
<input id=element_1x13">
</input>
    </div>

Solution

  • Assuming you have a way to select the element, you could do something like:

    // find the label somehow
    .findByCssSelector(...)
    .getAttribute('for')
    .then(function (value, setContext) {
        return this.parent
            // pop the previously found element off the context stack
            .end()
            // find the input, which has an id equal to the label's for
            .findById(value)
            // set the context to the found input
            .then(function (input) {
                setContext(input);
            });
    })
    // do stuff with the input