drupalextension icon indicating copy to clipboard operation
drupalextension copied to clipboard

Testing Views

Open jonathanjfshaw opened this issue 8 years ago • 5 comments

Drupal Views can be complex and critical features of a site, good candidates surely for some BDD testing.

Testing Views currently is hard, no ready-made steps offer much to help with testing views. I've been working on a step to do just this. The step I have created is: @Then I should see :elements_css (elements ):

Given HTML like this:

<div class ="views-myview">
  <div class="views-row,active">
    <span class="field--name-title"> Item1 </span>
    <span class="field--name-field-color"> Blue </span>
    <span> Something else here </span>
  </div>
  <div class="views-row">
    <span class="field--name-title"> Item2 </span>
    <span class="field--name-field-color"> Yellow </span>
    <span class="special"> This one is special </span>
  </div>
  <div class="views-row">
    <span class="field--name-title">Item3</span>
  </div>
</div>

Then the following step would pass:

Then I should see "div.views-myview > div.views-row" elements:
| contains            | .field--name-title:contains | .field-name-field-color:contains | .special | .active |
| Something else | Item1                                 | Blue                                             |              | Yes      |
|                           | Item2                                 | Yellow                                          | Yes        | No      |
|                           | Item3                                 |                                                     |  Yes       | No      |

Or equivalently, if behat.yml has:

region_map:
  Articles: "div.views-myview > div.views-row"
  Title field:  ".field--name-title"
  Color field: ".field-name-field-color"
Then I should see "articles":
| contains            | Title field: contains | Color field: contains | .special | .active |
| Something else | Item1                      | Blue                          |             | Yes      |
|                           | Item2                      | Yellow                       | Yes       | No      |
|                           | Item3                      |                                  | Yes       | No      |

Is this step something that you would be interested in adding to the Drupal extension? If it is, I will prepare a PR.

jonathanjfshaw avatar Jan 20 '17 15:01 jonathanjfshaw

This is not likely to be accepted since this is not valid from a BDD standpoint. Behat scenarios are using a domain language that is intended to be understandable by non-technical people like clients, product owners and helpdesk employees.

Remember that BDD is not about testing, it is intended to be able to effectively communicate the expected user behavior between all stakeholders of a project. If the only people that can understand the scenario are web developers then this goal is not met.

The Wikipedia page on BDD explains it pretty well, see https://en.wikipedia.org/wiki/Behavior-driven_development

pfrenssen avatar Jan 22 '17 00:01 pfrenssen

This is not likely to be accepted since this is not valid from a BDD standpoint.

That is fair for some of the implementation details of what I suggested, but I don't understand it as a response to the whole idea. Let me re-propose in a maximally naturalistic syntax:

If behat.yml has:

region_map:
  a list of articles: "div.views-myview > div.views-row"
  Title:  ".field--name-title"
  Color: ".field-name-field-color"

Then a feature file could have

Then I should see "a list of articles":
| Title    | Color  |
| Item1  | Blue    |
| Item2  | Yellow |
| Item3  |             |

Is this not valid from a BDD standpoint? If not, what would be a valid BDD approach to specifying a multi-element multi-value order-sensitive set?

jonathanjfshaw avatar Jan 22 '17 18:01 jonathanjfshaw

Isn't it easier to just quickly roll up a step definition of your own that is tailored to your use case? There are a million ways a view can be rendered.

The use case you propose would look something like this (untested):

  /**
   * @Then I should see the following news articles in the specified order:
   */
  public function assertNewsArticles(TableNode $table) {
    $articles = $this->getSession()->getPage()->findAll('css', 'div.views-myview > div.views-row');
    $i = 0;
    foreach ($table->getHash() as $expected) {
      $article = $articles[$i++];
      \PHPUnit_Framework_Assert::assertEquals($article->find('css', '.field--name-title')->getText(), $expected['title']);
      \PHPUnit_Framework_Assert::assertEquals($article->find('css', '.field-name-field-color')->getText(), $expected['color']);
    }
  }

pfrenssen avatar Jan 23 '17 10:01 pfrenssen

For me, not easier. I'd rather have one generic function I can reuse for multiple views, rather than many separate ones. There may be a million ways a view can be rendered, but I should be able to capture them OK with CSS selectors.

It may not be an important enough use case to justify adding the maintenance burden of it to this extension.

And there is something awkward about using the "region_map" in behat.yml for something other than Drupal regions, even though it works perfectly well for it.

jonathanjfshaw avatar Jan 26 '17 12:01 jonathanjfshaw

We can explore adding a ViewsContext that just provides low level functionality, no step definitions...

jhedstrom avatar Dec 05 '17 19:12 jhedstrom