Search code examples
seleniumxpathbehatacceptance-testinggherkin

How to get the value inside a specific table cell using XPath


I'm learning how to use Behat/Mink and Selenium to write acceptance tests, and I need to verify the value of a specific table cell. I found that I have to use xpath to do this, but I can't figure out the exact syntax. The table is as follows:

enter image description here

The html:

<table class="admintable generaltable" id="relationships">
<thead>
...
</tr>
</thead>
<tbody><tr class="r0 lastrow">
<td class="leftalign cell c0" style="">Cohort 1</td>
<td class="leftalign cell c1" style="">0</td>
<td class="leftalign cell c2" style="">Non-editing teacher</td>
<td class="centeralign cell c3" style="">No</td>
<td class="leftalign cell c4" style="">No</td>
<td class="leftalign cell c5 lastcol" style=""><a href="http://150.162.242.121/mariana/unasus-cp/local/relationship/edit_cohort.php?relationshipcohortid=1&amp;delete=1"><img src="http://150.162.242.121/mariana/unasus-cp/theme/image.php/standard/core/1445875205/t/delete" alt="Delete" title="Delete" class="iconsmall" /></a> <a href="http://150.162.242.121/mariana/unasus-cp/local/relationship/edit_cohort.php?relationshipcohortid=1"><img src="http://150.162.242.121/mariana/unasus-cp/theme/image.php/standard/core/1445875205/t/edit" alt="Edit" title="Edit" class="iconsmall" /></a></td>
</tr>
</tbody>
</table>

The statement I'm trying to write is:

Then I should see "No" in the "???" "css_element"

How exactly should I write that statement to specify that I want that first "No", under "Inscrição em vários grupos", in the "No" part.

EDIT: I made a mistake originally, I should be using "xpath_element" as a selector, not "css_element"


Solution

  • I am assuming that the Inscrição em vários grupos text always appears in the 4th column. Writing XPath for this is pretty easy as we can directly use the index.

    //table[@id='relationships']/tbody/tr[1]/td[4]
    

    If the column number of the heading column varies you would have to find the index(column number) of the Inscrição em vários grupos heading and then use it to find the corresponding value column.

    -- the below code will get you the index of the heading column
    count(//thead//tr/th[.='Inscrição em vários grupo']/preceding-sibling::*)+1
    
    -- use this index to find the corresponding value
    //table[@id='relationships']/tbody/tr[1]/td[count(//thead//tr/th[.='Inscrição em vários grupo']/preceding-sibling::*)+1]
    

    Note: Index starts from 1 not 0

    From the following link, syntax in behat seems to be as follows:

    How to assert that a text only exists 1 time in Mink

    https://stackoverflow.com/questions/32202047/could-not-find-element-with-xpath-in-behat-mink

    $session = $this->getMainContext()->getSession();
    $element = $session->getPage()->find(
            'xpath',
            $session->getSelectorsHandler()->selectorToXpath('xpath', '//table[@id='relationships']/tbody/tr[1]/td[4]');
    $elementText = $element->getText();
    
    if ($elementText != 'no') {
        throw new Exception('Value us not correct');
    }