How can I detect 404 errors for page resources?

I just started working with Behat and Minka. I use MinkExtension with Goutte and Selenium, as well as DrupalExtension.

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

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

Any advice or examples would be greatly appreciated.

+4
source share
4 answers

When using the 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
}
+2
source

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.phpat jmquarck / kate

0
source

, HelperContext.php ( 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 is an example of scripts written in Behat that use 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
0
source

You can use Restler , a microarchitecture that can help with testing RESTful APIs in Behat. It supports Driven API testing using Behat and Guzzle.

Check out 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"
0
source

Source: https://habr.com/ru/post/1542498/


All Articles