我正在使用硒 RC。我需要将文件上传 java 代码传递到 selenium 节点以从网格执行,这可能吗?
我有以下文件上传代码,我从硒代码中调用:
package com.wipro.Base.Base20.QA.SeleniumTests.FullRegression;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.*;
/**
* @author Bogdan Gusiev
* Date 29.03.2009
*/
public class FileChooserThread extends Thread {`
public FileChooserThread(String file) {
super(new FileRunner(file));
}
}
class FileRunner implements Runnable {
private String fullName;
public FileRunner(String fileName) {
this.fullName = fileName;
// run();
}
public void run() {
try {
Thread.sleep(1000);
Robot robot = new Robot(); //input simulation class
for (char c : fullName.toCharArray()) {
if (c == ':') {
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SHIFT);
} else if (c == '/') {
robot.keyPress(KeyEvent.VK_BACK_SLASH);
} else {
robot.keyPress(KeyStroke.getKeyStroke(
Character.toUpperCase(c), 0).getKeyCode());
}
}
robot.keyPress(KeyEvent.VK_ENTER);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}`
`//这里是selenium调用方法:
protected void chooseFile(String element, String fileName) {
Number positionLeft = selenium.getElementPositionLeft(element);
Number positionTop = selenium.getElementPositionTop(element);
new FileChooserThread(fileName).start(); //launch input thread.
//this method will held current thread while FileChooser gives the file name
selenium.clickAt("file", positionLeft + ","+ positionTop);
}`