0

I'm using Behat and Mink framework for BDD (using PHP)

I was looping through td elements to verify the values and text. Since all the elements returned are span elements, I could use getText() on the elements and it was all good.

$page = $this->getSession()->getPage();      
$expected_values = ["Tuesday", 22, 22, 22, 22];     
$actual_rows = $page->findAll('css', 'table.admin-table tbody tr td');
$actual_Values = array();
foreach($actual_rows as $row) {               
$actual_Values[] = $row->getText();
}
assertEquals($expected_values, $actual_Values);

But recently, the design of the page has changed. And some of the span elements have been replaced by input elements. And getText() returns null so I replaced it with getValue(). But since the first td element is a span it returns null if we use getText().

Is there anyway I can skip the first td within the loop from the code snippet above.

Update: Here is what I put in comments which hasn't rendered properly:

$actual_Values[] = $row->find('css', 'input')->getValue();
vijay pujar
  • 1,683
  • 4
  • 19
  • 32
  • You can, but most likely you'll need to use xpath. Can you tell what the html looks like for those `td`'s when they have spans and inputs? – Ian Bytchek Aug 14 '14 at 11:30
  • It's so stupid people downvote a question and don't even bother to put a comment. Absolute bonkers!! – vijay pujar Aug 19 '14 at 15:09

1 Answers1

3

I misread and thought you want to get text independently whether there is an input or span. To skip the first value you can just:

foreach($actual_rows as $row) {               
    if ($row !== reset($actual_rows) {
        $actual_Values[] = $row->getText();
    }
}

Or a better approach would be to array_shift($actual_rows) to remove the first value and do the normal loop then.

Or another approach would be to change your selector to $page->findAll('css', 'table.admin-table tbody tr td:not(:first-child)'); to select everything but the first td child.

Ian Bytchek
  • 8,804
  • 6
  • 46
  • 72
  • Hi Ian, the 3rd option looks pretty good to keep the code clean. I will try that and will let you know. Thanks – vijay pujar Aug 14 '14 at 12:35
  • Hi Ian, I tried the 3rd option, it's returning null at the moment. May be it's due to the fact that the JS is not fetching all the required data on the page. I have gone with a long winded process of evaluating the fields one by one. Thanks for your help though – vijay pujar Aug 31 '14 at 23:03
  • Hi Ian, I have just given you an up vote..The 3rd Option worked fine. However, I had to use the following inside the loop: $actual_Values[] = $row->find('css', 'input')->getValue(); – vijay pujar Sep 01 '14 at 15:00
  • Sorry, I didn't figure out the html structure from the words. Please update the question with it and I'll review the code. Certainly what I have in mind doesn't match the real case, if the code isn't working. – Ian Bytchek Sep 02 '14 at 08:17