如何检测页面资产的404错误?

时间:2014-05-29 14:12:01

标签: selenium behat mink goutte

我刚刚开始使用Behat和Mink。我将MinkExtension与Goutte和Selenium以及DrupalExtension一起使用。

到目前为止,这么好。我可以加载页面,查找各种元素,测试链接等。

但我不知道如何检查各种资产上的404 - 特别是图像,还有css和js文件。

非常感谢任何提示或示例。

4 个答案:

答案 0 :(得分:2)

使用Goutte网络抓取工具时,您可以执行以下操作:

$crawler = $client->request('GET', 'http://your-url.here');
$status_code = $client->getResponse()->getStatus();
if($status_code==404){
  // Do something
}

答案 1 :(得分:0)

您可以尝试以下方法:

<?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());
  }
}

来源:jmquarck / kate的FeatureContext.php

答案 2 :(得分:0)

请检查此HelperContext.phpCWTest_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]);
    }
  }

以下是用Behat编写的使用上述方法的示例场景:

  @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

答案 3 :(得分:0)

可以使用Restler,一个可以帮助在Behat中进行RESTful API测试的微框架。它支持使用Behat和Guzzle进行驱动API测试的行为。

检查以下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"