【问题标题】:In Java Maven project how do I upload file (photo) from resource folder using selenium在 Java Maven 项目中,如何使用 selenium 从资源文件夹上传文件(照片)
【发布时间】:2020-04-03 06:36:13
【问题描述】:

我需要从存储图像的资源文件夹中上传 jpg 图像。但我无法上传图片

.

如何上传图片并测试我的 selenium 项目?任何帮助都会很棒。

这是下面给出的代码,目前不工作

// to upload file from resources
    WebElement chooseFile3 = driver.findElement(By.id("profile-picture"));
    chooseFile3.sendKeys("//src/test/resources/Profile_Picture.jpg");

【问题讨论】:

    标签: java maven selenium junit


    【解决方案1】:

    这将解决您的问题 --> System.getProperty("user.dir") 它将显示 pom 所在目录的位置。 您可以打印 System.getProperty("user.dir"),以检查返回的内容,然后检查您的路径,如下所示:

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

    替代方案是使用,因为它直接从资源中读取文件,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);
    

    【讨论】:

    • 谢谢!但它仍然显示无效参数:找不到文件。 String imagePath = System.getProperty("user.dir") + "/resources/Profile_Picture.jpg"; driver.findElement(By.id("profile-picture")).sendKeys(imagePath);
    • 检查文件路径是否真的正确。 System.getProperty("user.dir") 返回什么。我假设您正在使用 mvn test 运行测试?
    • /SeleniumAutomationTesting/src/test/resources/Profile_Picture.jpg 这是我的路径,但它没有打印任何东西@Ishan
    • 我认为因为您使用 eclipse 将其作为 junit 测试运行,所以它不起作用,您可以使用 File file = new File(getClass().getClassLoader().getResource("Profile_Picture") .getFile()); String imagePath = file.getAbsolutePath(); -> 从资源文件夹中读取文件目录。 stackoverflow.com/questions/28673651/…
    【解决方案2】:

    从资源文件夹上传文件

    步骤 1) 将图片路径保存在变量中

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

    第 2 步)使用机器人类上传文件

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

    【讨论】:

    • 谢谢!但我想要一个简单的方法。
    猜你喜欢
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多