0

I need to upload an jpg image from my resources folder where I store my image. But I am unable to upload the image

enter image description here.

How can I upload the image and test my selenium project? Any help would be great.

Here is the code given below, which is not working at this moment

// to upload file from resources
    WebElement chooseFile3 = driver.findElement(By.id("profile-picture"));
    chooseFile3.sendKeys("//src/test/resources/Profile_Picture.jpg");
seenukarthi
  • 8,241
  • 10
  • 47
  • 68

2 Answers2

0

this will solve your problem --> System.getProperty("user.dir") it will show the location of directory where pom is present. You can print System.getProperty("user.dir"), to check what is returned and then check form your path as below:

String imagePath = System.getProperty("user.dir") + "/src/test/resources/Profile_Picture.jpg";
WebElement chooseFile3 = driver.findElement(By.id("profile-picture"));
chooseFile3.sendKeys(imagePath);

Alternate would be to use, as it directly reads the file from resources, How to get the path of src/test/resources directory in JUnit?

File file = new File(getClass().getClassLoader().getResource("Profile_Picture.jpg").getFile());
String imagePath = file.getAbsolutePath();
WebElement chooseFile3 = driver.findElement(By.id("profile-picture"));
chooseFile3.sendKeys(imagePath);
Ishan
  • 211
  • 1
  • 8
  • Thanks! But it still showing that invalid argument: File not found. String imagePath = System.getProperty("user.dir") + "/resources/Profile_Picture.jpg"; driver.findElement(By.id("profile-picture")).sendKeys(imagePath); – Easir Arafat Dec 10 '19 at 06:29
  • check if the file path is actually correct.what is System.getProperty("user.dir") returning. i assume you are using mvn test to run the tests? – Ishan Dec 10 '19 at 06:32
  • /SeleniumAutomationTesting/src/test/resources/Profile_Picture.jpg This is my path but it is not printing anything @Ishan – Easir Arafat Dec 10 '19 at 06:47
  • I think because you are using eclipse to run it as junit test, it is not working, you can use File file = new File(getClass().getClassLoader().getResource("Profile_Picture").getFile()); String imagePath = file.getAbsolutePath(); -> to read file directory from the resources folder. https://stackoverflow.com/questions/28673651/how-to-get-the-path-of-src-test-resources-directory-in-junit – Ishan Dec 10 '19 at 07:06
0

To upload file from Resource folder

Step 1) Save image path in Variable

String imagePath =  System.getProperty("user.dir") +"\\src\\test\\resources\\Profile_Pitcure.jpg";

Step 2) To upload file use Robot Class

public void uploadFileWithRobot(String imagePath) {
        StringSelection stringSelection = new StringSelection(imagePath);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(stringSelection, null);

        Robot robot = null;

        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }

        robot.delay(250);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.delay(150);
        robot.keyRelease(KeyEvent.VK_ENTER);
    }
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Jagrut
  • 96
  • 1
  • 8