【问题标题】:Java. Selenium. File Uploading Another One爪哇。硒。文件上传另一个
【发布时间】:2015-12-04 18:42:34
【问题描述】:

在我的情况下,按以下方法工作。 我现在使用 Windows,但是在编写了一些测试之后,想将其转移到 *nix 环境。一些很酷的人说路径必须是抽象的。

driver.findElement(By.id("admin_offer_kind_logo")).sendKeys("C:\\Path\\To\\File");

但我尝试:

driver.findElement(By.id("admin_offer_kind_logo")).sendKeys(System.getProperty("user.dir")+"\\src\\test\\resources\\Koala.jpg");

driver.findElement(By.id("admin_offer_kind_logo")).sendKeys(System.getProperty("user.dir")+"/src/test/resources/Koala.jpg");

它不想上传该死的文件。

@Test
public void FileFinding() {
    String file = System.getProperty("user.dir");
    System.out.print("FilePath:  ");
    System.out.println(file);
}

上面的代码打印:FilePath: C:\SeleniumTests\FirstWebDriverTest

我在项目中的文件的完整路径是:

C:\SeleniumTests\FirstWebDriverTest\src\test\resources\Koala.jpg

【问题讨论】:

    标签: java selenium file-upload selenium-webdriver


    【解决方案1】:

    您可以使用 com.google.common.io.Resources 从资源文件夹中获取文件。

    String filePath = "Koala.jpg";
    URL resource = Resources.getResource(filePath);
    String uploadFullPath = resource.toURI().getPath();
    

    或者参考How to get the path of src/test/resources directory in JUnit?的逻辑

    【讨论】:

      【解决方案2】:

      在一位好人的帮助下,找到了两种工作方法:

          @Test
              public void FileFinding_Right_One() {
              String file = getClass().getClassLoader().getResource("Koala.jpg").getFile();
              System.out.println(file.substring(1,file.length()));
      }
          @Test
              public void File_Finding_Right_Second
              File f = new File("src/test/resources/Koala.jpg");
              System.out.println(f.getAbsolutePath());
      }
      

      【讨论】:

        猜你喜欢
        • 2018-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-02
        • 1970-01-01
        • 2012-11-19
        相关资源
        最近更新 更多