【问题标题】:Opening files using selenium WebDriver when it's in the resources folder in a maven project使用 selenium WebDriver 打开位于 maven 项目的资源文件夹中的文件
【发布时间】:2018-04-18 18:18:05
【问题描述】:

我正在尝试使用 webdriver(.txt 和 pdf)打开不同类型的文件。所以我在资源文件夹中有文件,但是当我打开它们时,我收到以下错误消息:格式错误的异常

但是如果我从我的桌面打开文件,它工作得非常好:

例如,这就是我所拥有的:

 String file_loc = logInPage.getFile(file_name);
    logInPage.navigateToFileLoc(file_location);

这是两个方法的实现

 public String getFile(String fileName) {

        String result = "";

        ClassLoader classLoader = getClass().getClassLoader();
        try {
            result = IOUtils.toString(classLoader.getResourceAsStream(fileName), java.nio.charset.StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

public void navigateToFileLoc(String fileLoc){
   webDriver.get(fileLoc);
}

但是当我尝试从桌面获取文件时,它工作得非常好: 例如 webDriver.get("file:///C:/Users/test.pdf")

这就像你试图在 IDE 中打开一个 pdf 或 html

【问题讨论】:

    标签: java selenium selenium-webdriver cucumber


    【解决方案1】:

    能否请您更改 getFile 方法如下:

    public String getFile(String fileName) {
            String filePath = null;
            ClassLoader classLoader = getClass().getClassLoader();
            try {
                filePath= classLoader.getResource(fileName).getPath();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return  "file://"+filePath;
        }
    

    【讨论】:

      猜你喜欢
      • 2020-04-03
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多