Question

I've been working in BDD through Behat|Mink.

However I'm failing in finding an input (field) with an ID or Name if they aren't complete.

I intended to do something like:

  $field = $this->getSession()->getPage()->findField( "*first_name*" );

where the '*' meant that can be any piece of string there.

The problem is that i have a case where I can't know for sure the complete ID cause:

  id="<random-string>_<actual-field-name>_<random-number>"

Any idea how can I do this?

What have I tried

- get it through label (for some reasons there are some labels that dont have 'for' attribute and sometimes the name is a sentence and not an simple name)
- get it through css/classes (to many variables)

Edit:

I've created a php function to read the attribute and return the value of the intended attribute.

/**
 * 
 * @param string  $haystack The complete html
 * @param string  $needle   The string with the part of the attribute value
 * @param string  $tag      The tag of where the attribute should belong
 * @param string  $attr     The attribute to ifnd
 * @return string
 */
protected function findCompleteAttribute( $haystack, $needle, $tag = null, $attr = 'id' )

However, in cases that there is another fields (ex: hidden login form) and the needle is also in those inputs, it will get them if they're first.

So I need to specify the form, however, once more, there I haven't a way to find a form through name:

  $field = $this->getSession()->getPage()->find( 'named', array( 'form', $formName ) );
Was it helpful?

Solution 2

After studying more Xpath and based on my comment on @gontrollez answer it can be resolved by a single Xpath:

//form[ @name = 'Register' ]//input[ contains( @id, 'username' )]

This will return all input that have "username" on their id attribute and the input is inside any form that it's name attribute is equal to "Register"

<form>
    <input id="login_username" type="text"/>
</form>
<form name="Register>
    <div>
        <input id="other_username" type="text"/>
    </div>
</form>
</body>

this will return only the second input

OTHER TIPS

Try using XPath:

$el = $this->getSession()->getPage()->find('xpath', "//input[contains(@id,'first_name')]");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top