0

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 ) );
mloureiro
  • 957
  • 1
  • 12
  • 34

2 Answers2

2

Try using XPath:

$el = $this->getSession()->getPage()->find('xpath', "//input[contains(@id,'first_name')]");
gontrollez
  • 6,372
  • 2
  • 28
  • 36
  • This does all the work I've made by and on the function I did by hand (i wish i knew it before). However I still have the problem of when there are 2 or more fields that contain 'first_name' so I've decided to add more details to the find and now I'm using that way – mloureiro Sep 30 '13 at 09:58
  • You can refine the XPath expression including more than 1 attribute condition: http://stackoverflow.com/questions/6029232/xpath-select-multiple-attr – gontrollez Oct 01 '13 at 11:16
  • Thanks @gontrollez, but I don't think that multi attribute search will do, cause the problem is that I need a way to find a 'form' so that i can then search for the input inside. something like: `$form = $this->getSession()->getPage()->find( 'xpath', "//form[conains(@name,'Register']"); $input = $form->find( 'xpath', //input[contains(@id,'first_name')]");` but till now wasn't successful on searching for the form – mloureiro Oct 01 '13 at 15:34
-1

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

mloureiro
  • 957
  • 1
  • 12
  • 34