Java的Selenium WebDriver使用Java Robot API进行文件上传的一个解决方案 [英] One solution for File Upload using Java Robot API with Selenium WebDriver by Java

查看:478
本文介绍了Java的Selenium WebDriver使用Java Robot API进行文件上传的一个解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到很多人在使用Selenium WebDriver在测试环境中上传文件时遇到问题。我使用硒WebDriver和Java,并有同样的问题。我终于找到了一个解决方案,所以我会在这里发布它希望它可以帮助别人。

当我需要在测试中上传文件,我点击Webdriver在按钮中等待窗口打开弹出。然后我将路径复制到剪贴板中的文件,然后将其粘贴到打开窗口中,然后单击Enter。这是工作,因为当弹出窗口打开,焦点总是在打开按钮。

您将需要这些类和方法:

  import java.awt.Robot; 
import java.awt.event.KeyEvent;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;


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

这就是我刚刚打开打开窗口后所做的事情:

  setClipboardData(C:\\\ path to file\\example.jpg); 
// CTRL,V和ENTER键的原始键击
Robot robot = new Robot();
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);

就是这样。它是为我工作,我希望它适用于你们中的一些人。


实际上,有一种内置的技术这个也是。它应该在所有的浏览器和操作系统中工作。

Selenium 2(WebDriver)Java示例:

$ pre > //假设驱动程序是一个健康的WebDriver实例
WebElement fileInput = driver.findElement(By.xpath(// input [@ type ='file']));
fileInput.sendKeys(C:/path/to/file.jpg);

这个想法是直接将文件的绝对路径发送到您通常会点击的元素获取模态窗口 - 即< input type ='file'/> 元素。


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 finally have found a solution, so i will post it here hoping that it helps someone else.

When i need to upload a file in a test, i click with Webdriver in the button and wait for the window "Open" to pop. And then i copy the path to the file in the clipboard and then paste it in the "open" window and click "Enter". This is working because when the window "open" pops up, the focus is always in the "open" button.

You will need these classes and method:

import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;


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

And that is what i do, just after opening the "open" window:

setClipboardData("C:\\path to file\\example.jpg");
//native key strokes for CTRL, V and ENTER keys
Robot robot = new Robot();
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);

And that´s it. It is working for me, i hope it works for some of you.

解决方案

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.

这篇关于Java的Selenium WebDriver使用Java Robot API进行文件上传的一个解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆