gpt4 book ai didi

java - 使用 Selenium Server Standalone 处理文件上传

转载 作者:搜寻专家 更新时间:2023-10-30 21:10:29 26 4
gpt4 key购买 nike

我尝试使用 Selenium Standalone Server 在远程主机上执行测试套件。它应该上传一个文件。我使用下面的代码来处理文件上传:

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()Robot Class .

该方法是将指定的文件路径设置到剪贴板。

  1. 将数据复制到剪贴板。

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

  1. 在 Finder 窗口中找到文件,然后按 OK 选择文件。
    • 赢 [ Ctrl + V ]
    • MAC
      • "Go To Folder "- Command ⌘ + Shift + G.
      • 粘贴 - Command ⌘ + V 和
      • 确定打开它。

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

文件上传测试:- 你可以找到fileUploadBytes.html单击 Try it Yourself 文件.

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 .

关于java - 使用 Selenium Server Standalone 处理文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46239016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com