Java语言硒.正在上载另一个文件



在我的例子中,它在下面的方法中工作。我现在使用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:SeleniumTestsFirstWebDriverTest

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

C:SeleniumTestsFirstWebDriverTestsrctestresourcesKoala.jpg

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

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

或者参考《如何获取JUnit中src/test/resources目录的路径?

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

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

相关内容