Determining field identifier that is dynamically generated

Re: Determining field identifier that is dynamically generated

by Rajesh Taneja -
Number of replies: 3

Hello Mike,


You can use either one of the following to select radio.

# Select green radio button.
And I set the field "Green" to "1"
# Select another colour radio and set the text color.
And I set the field "Another colour:" to "1"
And I set the field "q213_85" to "White"
# Select orange radio button uing xpath.
And I set the field with xpath "//label[contains(normalize-space(.), 'Orange')]/preceding-sibling::input[@type='radio'][1]" to "1"
# Set value in text field using xpath.
And I set the field with xpath "//input[@type='text' and @name='q213_85']" to "bool"

Average of ratings: Useful (1)
In reply to Rajesh Taneja

Re: Determining field identifier that is dynamically generated

by Mike Churchward -
Picture of Core developers Picture of Plugin developers Picture of Testers

Hi Raj -

I can't do something like:

And I set the field with xpath "//input[@type='text' and @name='q213_85']" to "bool"

Because the name 'q213_85' is generated at run time and used database id values. I can't know the name when I write the test.

The 'xpath' example you provided might work. But in the end, Tim's advice was correct. Really, I needed a label around all of the form fields, even if the labels are set to not display. The labels will be better for accessibility issues, and provide a better identifier for the Behat test.

mike

In reply to Mike Churchward

Re: Determining field identifier that is dynamically generated

by Rajesh Taneja -

Hello Mike,

Surely adding label is a better solution for accessibility and easy/clear behat scenario. 

Reason I added that comment is to show that, if you don't have a label then you can still use behat steps to achieve what you want. If q213_85 is random then you can change xpath to look for a text field after radio input or in the fieldset.

Cheers

Rajesh

In reply to Rajesh Taneja

Re: Determining field identifier that is dynamically generated

by Mike Churchward -
Picture of Core developers Picture of Plugin developers Picture of Testers

Thanks Raj.

I put that "xpath" part in my hip pocket. That will be something useful as I continue to write more tests. wink

mike