在我的情况下,以下方法工作。 我现在使用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
答案 0 :(得分:0)
您可以使用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?
中的逻辑答案 1 :(得分:0)
在一位好人的帮助下,找到了两种工作方法:
@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());
}