One solution for File Upload using Java Robot API with Selenium WebDriver by Java

后端 未结 4 1805
你的背包
你的背包 2020-11-29 00:45

I saw that lots of people have Problems uploading a file in a test Environment with Selenium WebDriver. I use the selenium WebDriver and java, and had the same problem. I fi

相关标签:
4条回答
  • 2020-11-29 01:24

    Actually, there is an in-built technique for this, too. It should work in all browsers and operating systems.

    Selenium 2 (WebDriver) Java example:

    // assuming driver is a healthy WebDriver instance
    WebElement fileInput = driver.findElement(By.xpath("//input[@type='file']"));
    fileInput.sendKeys("C:/path/to/file.jpg");
    

    The idea is to directly send the absolute path to the file to an element which you would usually click at to get the modal window - that is <input type='file' /> element.

    0 讨论(0)
  • 2020-11-29 01:26

    Thanks Alex! I needed this since I could not get the sendKeys function to work when used via Play framework 2.1 (fluentlenium wrapper). I am testing over Firefox [17.0.7] for Mac and had to make a few mods to get it working. Below is an approximation of the final snippet I used.

        val file = new File(PATH_TO_IPA)
        val stringSelection: StringSelection = new StringSelection(file.getAbsolutePath)
        Toolkit.getDefaultToolkit.getSystemClipboard().setContents(stringSelection, null)
        val robot: Robot = new Robot()
        // Cmd + Tab is needed since it launches a Java app and the browser looses focus
        robot.keyPress(KeyEvent.VK_META)
        robot.keyPress(KeyEvent.VK_TAB)
        robot.keyRelease(KeyEvent.VK_META)
        robot.keyRelease(KeyEvent.VK_TAB)
        robot.delay(500)
        robot.keyPress(KeyEvent.VK_META)
        robot.keyPress(KeyEvent.VK_SHIFT)
        robot.keyPress(KeyEvent.VK_G)
        robot.keyRelease(KeyEvent.VK_META)
        robot.keyRelease(KeyEvent.VK_SHIFT)
        robot.keyRelease(KeyEvent.VK_G)
        robot.keyPress(KeyEvent.VK_META)
        robot.keyPress(KeyEvent.VK_V)
        robot.keyRelease(KeyEvent.VK_META)
        robot.keyRelease(KeyEvent.VK_V)
        robot.keyPress(KeyEvent.VK_ENTER)
        robot.keyRelease(KeyEvent.VK_ENTER)
        robot.delay(500)
        robot.keyPress(KeyEvent.VK_ENTER)
        robot.keyRelease(KeyEvent.VK_ENTER)
    
    0 讨论(0)
  • 2020-11-29 01:29

    The switching of application on Mac is much better to do with AppleScript. AppleScript is integrated to system, so it will be always functional and does not depend on order of apps on Cmd+Tab. Your test/app will be less fragile. https://developer.apple.com/library/content/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html

    You need only detect you are on mac and has name of the application.

    Runtime runtime = Runtime.getRuntime();
                String[] args = { "osascript", "-e", "tell app \"Chrome\" to activate" };
                Process process = runtime.exec(args);
    
    0 讨论(0)
  • 2020-11-29 01:34

    Thanks Alex,

    Java Robot API helped me for uploading file. I was fedup with File Upload using WebDriver. Following is the code I used (Small modification to yours):

    Robot robot = new Robot();
    robot.delay(1000);
    
    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.keyRelease(KeyEvent.VK_ENTER);
    robot.delay(1000);
    
    0 讨论(0)
提交回复
热议问题