0

In my case, works below method. I use Windows now, but after writing some tests, want transfer it to the *nix environment. And some cool guys says that path must be abstract one.

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

But of I try:

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

or

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

It doesn't wants to upload the goddamn file.

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

Above code prints: FilePath: C:\SeleniumTests\FirstWebDriverTest

Full path to my file in project is:

C:\SeleniumTests\FirstWebDriverTest\src\test\resources\Koala.jpg
Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
Boris
  • 125
  • 1
  • 1
  • 11

2 Answers2

0

You can use com.google.common.io.Resources to get the file from your resources folder.

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

Or refer the logic from How to get the path of src/test/resources directory in JUnit?

Community
  • 1
  • 1
Bharath
  • 1,535
  • 1
  • 12
  • 15
0

With help of one good man, found two working methods:

    @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());
}
Boris
  • 125
  • 1
  • 1
  • 11