4

I've just gotten started with Behat and Mink. I'm using MinkExtension with Goutte and Selenium, and also DrupalExtension.

So far, so good. I can load a page, look for various elements, test links, etc.

But I don't see how to check for 404s on various assets - images, especially, but also css and js files.

Any tips or examples would be much appreciated.

joe casey
  • 126
  • 7
  • Related: [How To Download Files With Selenium And Why You Shouldn’t](http://web.archive.org/web/20160314071959/http://ardesco.lazerycode.com/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/) – kenorb Jul 26 '17 at 22:23

4 Answers4

3

When using Goutte web crawler you can do this:

$crawler = $client->request('GET', 'http://your-url.here');
$status_code = $client->getResponse()->getStatus();
if($status_code==404){
  // Do something
}
henrik
  • 1,558
  • 3
  • 14
  • 29
0

You can try the following methods:

<?php

use Behat\Behat\Context\Context;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Client;

class FeatureContext extends WebTestCase implements Context {

  /**
   * @var Client
   */
  private $client;

  /**
   * @When /^I send a "([^"]*)" request to "([^"]*)"$/
   *
   * @param $arg1
   * @param $arg2
   */
  public function iSendARequestTo($arg1, $arg2) {
    $this->client = static::createClient();
    $this->client->request($arg1, $arg2);
  }

  /**
   * @Then /^the output should contain: "([^"]*)"$/
   *
   * @param $arg1
   */
  public function theOutputShouldContain($arg1) {
    $this->assertContains($arg1, $this->client->getResponse()->getContent());
  }

  /**
   * @Then /^the status code should be "([^"]*)"$/
   *
   * @param $arg1
   */
  public function theStatusCodeShouldBe($arg1) {
    $this->assertEquals($arg1, $this->client->getResponse()->getStatusCode());
  }
}

Source: FeatureContext.php at jmquarck/kate

kenorb
  • 155,785
  • 88
  • 678
  • 743
0

Please check the following methods from this HelperContext.php (part of CWTest_Behat):

  /**
   * @Given get the HTTP response code :url
   * Anonymous users ONLY.
   */
  public function getHTTPResponseCode($url) {
    $headers = get_headers($url, 1);
    return substr($headers[0], 9, 3);
  }

  /**
   * @Given I check the HTTP response code is :code for :url
   */
  public function iCheckTheHttpResponseCodeIsFor($expected_response, $url) {
    $path = $this->getMinkParameter('base_url') . $url;
    $actual_response = $this->getHTTPResponseCode($path);
    $this->verifyResponseForURL($actual_response, $expected_response, $url);
  }

  /**
   * Compare the actual and expected status responses for a URL.
   */
  function verifyResponseForURL($actual_response, $expected_response, $url) {
    if (intval($actual_response) !== intval($expected_response)) {
      throw new Exception("This '{$url}' asset returned a {$actual_response} response.");
    }
  }

  /**
   * @Given I should get the following HTTP status responses:
   */
  public function iShouldGetTheFollowingHTTPStatusResponses(TableNode $table) {
    foreach ($table->getRows() as $row) {
      $this->getSession()->visit($row[0]);
      $this->assertSession()->statusCodeEquals($row[1]);
    }
  }

Here are the example scenarios written in Behat which are using the above methods:

  @roles @api @regression
  Scenario: Verify Anonymous User access to /user/login
    Given I am not logged in
    Then I check the HTTP response code is 200 for '/user/login'

  @roles @api @regression
  Scenario: Verify Anonymous User access to /admin
    Given I am not logged in
    Then I check the HTTP response code is 403 for '/admin'

  @roles @api @regression
  Scenario: Verify Administrator access to /admin
    Given I am logged in as a user with the admin role
    And I am on "/admin"
    Then the response status code should be 200
kenorb
  • 155,785
  • 88
  • 678
  • 743
0

It's possible to use Restler, a micro framework which can help with RESTful API testing in Behat. It support behavior Driven API testing using Behat and Guzzle.

Check the following example:

  Scenario: Saying
    When I request "/examples/_001_helloworld/say"
    Then the response status code should be 404
    And the response is JSON
    And the type is "array"
kenorb
  • 155,785
  • 88
  • 678
  • 743