簡體   English   中英

使用Selenium Server Standalone處理文件上載

[英]Handle file upload with Selenium Server Standalone

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

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

當我遠程執行它時它不起作用,因為它無法打開文件選擇器窗口。 輸入字段在網頁上如下所示:

<input type =“text”id =“file-path”>

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

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

輸入類型不是文件,因此下面的代碼不起作用:

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

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

此方法是將指定的文件路徑設置為ClipBoard。

  1. 將數據復制到ClipBoard為。

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

  1. 在Finder窗口中找到該文件,然后按OK選擇該文件。
    • WIN [ Ctrl + V]
    • 蘋果電腦
      • Go To Folder ” - 命令⌘ + Shift + 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

有問題的文件應該在運行程序的計算機(無論是本地服務器還是遠程服務器)上可用,例如,在/ resources目錄中

在您的本地計算機上,這應該工作。

chooseFileElement.waitForVisible().type("/file/path/filename.jpg");
clickButton("Attach File");

但是,在遠程服務器上,您需要將LocalFileDetector的新實例與<input type=file>元素相關聯。

LocalFileDetector detector = new LocalFileDetector();
File localFile = detector.getLocalFile("/file/path/filename.jpg");
RemoteWebElement input = (RemoteWebElement) myDriver().findElement(By.id("fileUpload"));
input.setFileDetector(detector);
input.sendKeys(localFile.getAbsolutePath());
clickButton("Attach File");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM