无法使用Behat Mink扩展框架在输入字段中附加文件(csv文件)


not able to attach a file (csv file) in input field using Behat Mink extension Framework

我想使用Behat Framework(Mink Extension)将CSV文件附加到"file"类型的输入字段中。

输入字段的Html----input id="edit ab csv file"class="form file"type="file"size="60"name="files[ab_csv _file]">

我们尝试的第一种方法我已经尝试在Driverinterface 中使用attachfile()方法

/**
 * Attaches file to field with specified id|name|label|value.
 *
 * @When /^(?:|I )attach the file "(?P<path>[^"]*)" to "(?P<field>(?:[^"]|''")*)"$/
 */
 public function attachFileToField($field, $path)
{
    $field = $this->fixStepArgument($field);
    if ($this->getMinkParameter('files_path')) {
        $fullPath = rtrim(realpath($this->getMinkParameter('files_path')), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$path;
        if (is_file($fullPath)) {
            $path = $fullPath;
        }
    }
    $this->getSession()->getPage()->attachFileToField($field, $path);
}

}

第二种方法——我们尝试使用Java脚本

$jscript = "document.getElementById('edit-ab-csv-file').value='//home//developer//build//tools//behat//Invaliduploadfile.csv';";
  $this->getSession()->getDriver()->executeScript($jscript);

我得到一个例外,说

 The operation is insecure. (WARNING: The server did not provide any stack trace information)
      Command duration or timeout: 10 milliseconds

有人能帮忙解决这个问题吗。

第一个方法对我有效。不要忘记使用文件的完整路径或使用files_path Mink配置设置。

查看此论坛帖子:https://groups.google.com/forum/#!主题/behat/vxZAssi2Nf8

Mink需要一个绝对的路径来上传文件,但很明显path可能在其他环境中不起作用。相反,你可以为您的Yaml配置文件提供"file_path"属性:

扩展:

Behat'MinkExtension'Extension:
    files_path: "path/to/files"