1

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"

Tuma
  • 603
  • 1
  • 7
  • 35

2 Answers2

2

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');
}
Community
  • 1
  • 1
StrikerVillain
  • 3,719
  • 2
  • 24
  • 41
  • Thanks a lot for responding! The xpath you suggested returned an error (Css matching locator "//table[@id='relationships']/tbody//td[3]" not found.) The position doesnt vary, so I don't understand why this locator doesnt work – Tuma Oct 28 '15 at 16:04
  • It still doesn't seem to work. The step I'm writing is "Then I should see "No" in the "//table[@id='relationships']/tbody//tr[4]" "css_element"", is that correct? – Tuma Oct 28 '15 at 16:13
  • Please check my answer. The xpath is `//table[@id='relationships']/tbody/tr[1]/td[4]`. What is this "css_element"? – StrikerVillain Oct 28 '15 at 16:16
  • It's part of the Behat step – Tuma Oct 28 '15 at 16:23
  • My bad, I should be using "xpath_element"! Your original suggestion for the xpath worked perfectly! – Tuma Oct 28 '15 at 16:47
0

I know you already accepted an answer but did you try a simple CSS selector like td.c3? It seems a lot more straightforward to me.

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • I don't remember, but I think I tried it and for some reason it wasn't working via css selector. Maybe there were other td with c3 as a class – Tuma Nov 17 '15 at 22:57