changeableface
changeableface

Reputation: 33

Behat/Mink Test to check if element contains content

I'm learning to write these test to help increase overall quality of the code I write, however I've been unable to find any example of a test that checks to see if an element contains anything - there are plenty of test that check for exact phrases.

In my case it's a CMS driven application, so if the user changes the content the test will fail, so it's seems more reasonable to check to make sure there is some content in the page, regards of the content.

I've found a way to do with this selenium and jQuery, but it's a lot slower to run tests this way, and so I'm trying where possible to keep to command line only.

Any one had experience with this sort of thing before? My application is written in PHP, I've had a bit of luck with some custom tests actions, but I'm by no means a advanced coder.

Upvotes: 3

Views: 10245

Answers (2)

E Ciotti
E Ciotti

Reputation: 4963

Asserting that a page contains anything is not implemented in behat (I think) because it is not a good practice. The page could return an exception, or an HTTP error 403/500 and your test would pass.

  • If you just want to assert the page loaded OK, check for the page return code 200
Then the response status code should be
  • If you want to assert the page contains something, rely on an element that is always there in the layout, e.g. footer (that at least check that nothing stopped the rendering), the page title h1, or even the "body" element (still not a good idea if it contains an error message)
Then I should see a ".footer" element
Then I should see a "body" element

Upvotes: 5

Rob Squires
Rob Squires

Reputation: 2188

If you're using Behat + Mink you could use some of the built in helper functions to achieve this:

Assert the element exists on the page:

$this->assertSession()->elementExists('css', '.selector');

Check the content is not empty:

if ( empty($this->getPage()->find('css', '.selector')->getText()) ) {
    throw new Exception;
}

I find the WebAssert class to be a particularly useful object + the best documentation is the class itself: WebAssert.php

Upvotes: 7

Related Questions