代码之家  ›  专栏  ›  技术社区  ›  Patrick Kenny

如何检查Behat/Mink中的(任何)IP地址?

  •  0
  • Patrick Kenny  · 技术社区  · 8 年前

    我正在使用Behat/Mink测试Drupal 8站点。

    我有一个带有IP地址的输入字段,我想确保已经记录了IP地址。我不想检查特定的IP地址,因为它可能会更改,所以我只想确保该字段有一些值。

    基于IP地址将包含一个周期的假设,我尝试了以下方法:

     Then the "#edit-field-ip-0-value" element should contain "."
    

    但这失败了,错误如下:

        The string "." was not found in the HTML of the element matching css "#edit-field-ip-0-value". (Behat\Mink\Exception\ElementHtmlException)
    

    但是,该字段的值为:

    172.19.0.1
    

    我也试着这样检查:

     Then the "#edit-field-ip-0-value" element should contain "0"
    

    但它失败了,并出现了相同的错误(在元素的HTML中找不到字符串)。

    编辑

    <input class="js-text-full text-full form-text" data-drupal-selector="edit-field-ip-0-value" type="text" id="edit-field-ip-0-value" name="field_ip[0][value]" value="172.19.0.1" size="60" maxlength="255" placeholder="">
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Patrick Kenny    5 年前

    以下是我最终使用的代码:

      /**
       * @Then the :element element should contain an IP address
       *
       * Checks that the element with the specified CSS contains IP address value.
       */
      public function assertElementContainsIpAddress($element) {
        $page = $this->getSession()->getPage();
        // Alternately, substitute with getText() for the label.
        $element_value = $page->find('css', "$element")->getValue();
        $valid_ip_address = filter_var($element_value, FILTER_VALIDATE_IP);
        if ($valid_ip_address === FALSE) {
          throw new Exception("Valid IP address not found in element $element, which had a value of $element_value.");
        }
      }