【发布时间】:2016-10-17 10:06:40
【问题描述】:
我确实有一个页面,其中导入按钮看起来像这样。
上述导入按钮的Dom结构如下
<button ngf-select="" ng-model="$parent.enduserMashups.files" class="btn btn-default text-center ng-pristine ng-untouched ng-valid ng-empty">
<i class="icon-import-data"></i>
<span>Import</span></button>
我已经编写了如下的 selenium 上传实用程序,在 NON-GRID 环境(本地机器)的情况下可以正常工作
public void uploadFile(String filePath)
{
// Setting up clipBoard location
StringSelection ss = new StringSelection(filePath);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
// Using Robot class to upload file
Robot robot;
try
{
robot = new Robot();
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
// Press Enter
robot.keyPress(KeyEvent.VK_ENTER);
// Release Enter
robot.keyRelease(KeyEvent.VK_ENTER);
// Press CTRL+V
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
// Release CTRL+V
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
e.printStackTrace();
};
// Press Enter
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
} catch (AWTException e)
{
e.printStackTrace();
}
}
如果在 selenium 网格节点环境中运行上述代码,则在节点浏览器实例上单击“导入”按钮,并且此处找不到文件(显然),因为它保存在另一台运行测试用例的机器上. 谷歌搜索了很多,我找到了一些解决方案,其中指出如果有按钮,我们可以使用 webdriver.sendKeys(file) 方法,该方法直接将文件发送到 selenium 节点,但这仅适用于标签的情况。
如果没有元素怎么办?
【问题讨论】:
标签: file-upload selenium-webdriver selenium-grid