2017-09-15 15 views
13

我嘗試在使用Selenium Standalone Server的遠程主機上執行測試套件。它應該上傳一個文件。我用下面的代碼來處理文件上傳:使用Selenium Server Standalone處理文件上傳

FileBrowserDialogHandler fileBrowserDialogHandler = new FileBrowserDialogHandler(); 
fileBrowserDialogHandler.fileUploadDialog(fileSource); 

,因爲它是不能夠打開文件選擇窗口,它不會,當我遠程執行它的工作。 輸入字段看起來像這樣在網頁:

< INPUT TYPE = 「文本」 ID = 「文件路徑」>

我替換基於WebElement一個當前的解決方案,以避免圖形窗口,但它不起作用。

WebElement fileInput = driver.findElement(By.id("filepathelement")); 
fileInput.sendKeys(filepath); 

輸入類型不是文件,所以下面的代碼是不工作:

driver.findElement(By.id("myUploadElement")).sendKeys("<absolutePathToMyFile>"); 
+0

它不應該打開文件選擇器窗口。 – JeffC

+1

[如何使用Java中的Selenium WebDriver上傳文件]可能的重複(https://stackoverflow.com/questions/16896685/how-to-upload-file-using-selenium-webdriver-in-java) – JeffC

+0

@JeffC:它不是重複的,因爲字段類型不同,並且解決方案在這種情況下不起作用。 – plaidshirt

回答

5

上傳使用Java Selenium: sendKeys()Robot Class文件。

此方法是將指定的文件路徑設置到剪貼板。

  1. 將數據複製到ClipBoard中。

public static void setClipboardData(String filePath) { 
    StringSelection stringSelection = new StringSelection(filePath); 
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null); 
} 

  • 定位取景窗的文件,並按下OK選擇文件。
    • WIN [Ctrl鍵 + V]
    • MAC
      • Go To Folder」 - 命令⌘ + + G.
      • 粘貼 - 命令⌘ + V和
      • OK將其打開。

  • enum Action { 
        WIN, MAC, LINUX, SEND_KEYS; 
    } 
    public static boolean FileUpload(String locator, String filePath, Action type) { 
        WebDriverWait explicitWait = new WebDriverWait(driver, 10); 
    
        WebElement element = explicitWait.until(ExpectedConditions.elementToBeClickable(By.xpath(locator))); 
        if(type == Action.SEND_KEYS) { 
         element.sendKeys(filePath); 
         return true; 
        } else { 
         try { 
          element.click(); 
    
          Thread.sleep(1000 * 5); 
    
          setClipboardData(filePath); 
    
          Robot robot = new Robot(); 
          if(type == Action.MAC) { // Apple's Unix-based operating system. 
    
           // 「Go To Folder」 on Mac - Hit Command+Shift+G on a Finder window. 
           robot.keyPress(KeyEvent.VK_META); 
           robot.keyPress(KeyEvent.VK_SHIFT); 
           robot.keyPress(KeyEvent.VK_G); 
           robot.keyRelease(KeyEvent.VK_G); 
           robot.keyRelease(KeyEvent.VK_SHIFT); 
           robot.keyRelease(KeyEvent.VK_META); 
    
           // Paste the clipBoard content - Command ⌘ + V. 
           robot.keyPress(KeyEvent.VK_META); 
           robot.keyPress(KeyEvent.VK_V); 
           robot.keyRelease(KeyEvent.VK_V); 
           robot.keyRelease(KeyEvent.VK_META); 
    
           // Press Enter (GO - To bring up the file.) 
           robot.keyPress(KeyEvent.VK_ENTER); 
           robot.keyRelease(KeyEvent.VK_ENTER); 
           return true; 
          } else if (type == Action.WIN || type == Action.LINUX) { // Ctrl + V to paste the content. 
    
           robot.keyPress(KeyEvent.VK_CONTROL); 
           robot.keyPress(KeyEvent.VK_V); 
           robot.keyRelease(KeyEvent.VK_V); 
           robot.keyRelease(KeyEvent.VK_CONTROL); 
          } 
    
          robot.delay(1000 * 4); 
    
          robot.keyPress(KeyEvent.VK_ENTER); 
          robot.keyRelease(KeyEvent.VK_ENTER); 
          return true; 
         } catch (AWTException e) { 
          e.printStackTrace(); 
         } catch (InterruptedException e) { 
          e.printStackTrace(); 
         } 
        } 
        return false; 
    } 
    

    文件上傳測試: -您可以通過點擊Try it Yourself找到fileUploadBytes.html文件。

    public static void uploadTest(RemoteWebDriver driver) throws Exception { 
        //driver.setFileDetector(new LocalFileDetector()); 
        String baseUrl = "file:///D:/fileUploadBytes.html"; 
        driver.get(baseUrl); 
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
    
        FileUpload("//input[1]", "D:\\log.txt", Action.SEND_KEYS); 
    
        Thread.sleep(1000 * 10); 
    
        FileUpload("//input[1]", "D:\\DB_SQL.txt", Action.WIN); 
    
        Thread.sleep(1000 * 10); 
    
        driver.quit(); 
    } 
    

    欲瞭解更多信息,請參閱my post