下面的获取屏幕位置代码可以直接使用,打开运用程序进行测试需要根据你的具体情况使用代码,例如”D:\lanqiao\lanqiao2006.exe”是运用程序的位置。后续获取位置,可以使用最后一段代码在页面获取后使用,web项目自动测试工具还可以使用selenium
【打开运用程序示例】
package cn.com.robotest;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class RobotTest
{
public static void main(String[] args) throws AWTException, InterruptedException, IOException
{
Runtime rt = Runtime.getRuntime();
try {
rt.exec("D:\\lanqiao\\lanqiao2006.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //
}
}
【公共方法示例】
package cn.com;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.ImageIcon;
public class CommonAuto {
/**
* 鼠标单击(左击),要双击就连续调用
*
* @param r
* @param x
* x坐标位置
* @param y
* y坐标位置
* @param delay
* 该操作后的延迟时间
*/
public static void clickLMouse(Robot r, int x, int y, int delay) {
r.mouseMove(x, y);
r.mousePress(InputEvent.BUTTON1_MASK);
r.delay(10);
r.mouseRelease(InputEvent.BUTTON1_MASK);
r.delay(delay);
}
/**
* 鼠标右击,要双击就连续调用
*
* @param r
* @param x
* x坐标位置
* @param y
* y坐标位置
* @param delay
* 该操作后的延迟时间
*/
public static void clickRMouse(Robot r, int x, int y, int delay) {
r.mouseMove(x, y);
r.mousePress(InputEvent.BUTTON3_MASK);
r.delay(10);
r.mouseRelease(InputEvent.BUTTON3_MASK);
r.delay(delay);
}
/**
* 键盘输入(一次只能输入一个字符)
*
* @param r
* @param ks
* 键盘输入的字符数组
* @param delay
* 输入一个键后的延迟时间
*/
public static void pressKeys(Robot r, int[] ks, int delay) {
for (int i = 0; i < ks.length; i++) {
r.keyPress(ks[i]);
r.delay(10);
r.keyRelease(ks[i]);
r.delay(delay);
}
}
/**
* 复制
*
* @param r
* @throws InterruptedException
*/
void doCopy(Robot r) throws InterruptedException {
Thread.sleep(3000);
r.setAutoDelay(200);
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_C);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_C);
}
/**
* 粘贴
*
* @param r
* @throws InterruptedException
*/
void doParse(Robot r) throws InterruptedException {
r.setAutoDelay(500);
Thread.sleep(2000);
r.mouseMove(300, 300);
r.mousePress(InputEvent.BUTTON1_MASK);
r.mouseRelease(InputEvent.BUTTON1_MASK);
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_V);
}
/**
* 捕捉全屏慕
*
* @param r
* @return
*/
public Icon captureFullScreen(Robot r) {
BufferedImage fullScreenImage = r.createScreenCapture(new Rectangle(
Toolkit.getDefaultToolkit().getScreenSize()));
ImageIcon icon = new ImageIcon(fullScreenImage);
return icon;
}
/**
* 捕捉屏幕的一个矫形区域
*
* @param r
* @param x
* x坐标位置
* @param y
* y坐标位置
* @param width
* 矩形的宽
* @param height
* 矩形的高
* @return
*/
public Icon capturePartScreen(Robot r, int x, int y, int width, int height) {
r.mouseMove(x, y);
BufferedImage fullScreenImage = r.createScreenCapture(new Rectangle(
width, height));
ImageIcon icon = new ImageIcon(fullScreenImage);
return icon;
}
}
【登录并进行后续操作示例】
package cn.com;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.io.IOException;
public class AutoTest {
public static void main(String[] args) throws AWTException, InterruptedException, IOException
{
Robot robot = new Robot();
//设置Robot产生一个动作后的休眠时间,否则执行过快
//获取屏幕分辨率
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
double height=900;
System.out.println(d);
Runtime rt = Runtime.getRuntime();
try {
rt.exec("D:\\lanqiao\\lanqiao\\lanqiao2006.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //
robot.setAutoDelay(5000);
robot.mouseMove(500, 500);
robot.setAutoDelay(500);
int[] login = { KeyEvent.VK_SHIFT,KeyEvent.VK_S,KeyEvent.VK_Y,KeyEvent.VK_0,KeyEvent.VK_5,KeyEvent.VK_0,KeyEvent.VK_TAB,
KeyEvent.VK_1,KeyEvent.VK_2,KeyEvent.VK_3,KeyEvent.VK_4,KeyEvent.VK_5,KeyEvent.VK_6,
KeyEvent.VK_ENTER};//登录
CommonAuto.pressKeys(robot, login, 50);
CommonAuto.clickLMouse(robot,300, (int) ((double)847/900*height), 3000);//营运管理
CommonAuto.clickLMouse(robot, 457, (int) ((double)208/900*height), 3000);//受理开单
CommonAuto.clickLMouse(robot, 299, (int) ((double)152/900*height), 3000);//时间
int[] time = {KeyEvent.VK_LEFT,KeyEvent.VK_LEFT,KeyEvent.VK_LEFT,KeyEvent.VK_LEFT,KeyEvent.VK_LEFT,
KeyEvent.VK_2,KeyEvent.VK_0,KeyEvent.VK_1,KeyEvent.VK_6,KeyEvent.VK_RIGHT,KeyEvent.VK_1,KeyEvent.VK_RIGHT,KeyEvent.VK_1};//时间
CommonAuto.pressKeys(robot, time, 50);
CommonAuto.clickLMouse(robot, 882, (int) ((double)152/900*height), 60000);//提取
CommonAuto.clickLMouse(robot, 527, (int) ((double)838/900*height), 3000);//导出
int[] name = {KeyEvent.VK_2,KeyEvent.VK_0,KeyEvent.VK_1,KeyEvent.VK_6};//时间
CommonAuto.pressKeys(robot, name, 50);
CommonAuto.clickLMouse(robot, 629, (int) ((double)480/900*height), 3000);//保存
}
}
【代码示例】
package cn.com.robotest;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
@SuppressWarnings("serial")
public class PositionOfMouse extends JFrame{
private JLabel label;
public PositionOfMouse(){
super("测试程序");//建立新窗体
this.setSize(400,300);//设置窗体的宽和高
this.setVisible(true);//设置窗体可见
this.setLayout(new FlowLayout(FlowLayout.CENTER));//框架流布局且居中对齐
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置点击关闭按钮时的默认操作
final JLabel label = new JLabel("此处显示鼠标右键点击的坐标");
this.add(label); // 将标签放入窗体
// label.setText("3");
this.addMouseListener(new MouseAdapter(){ //匿名内部类,鼠标事件
public void mouseClicked(MouseEvent e){ //鼠标完成点击事件
if(e.getButton() == MouseEvent.BUTTON3){ //e.getButton就会返回点鼠标的那个键,左键还是右健,3代表右键
int x = e.getX(); //得到鼠标x坐标
int y = e.getY(); //得到鼠标y坐标
String banner = "鼠标当前点击位置的坐标是" + x + "," + y;
System.out.println("鼠标当前点击位置的坐标是" + x + "," + y);
label.setText(banner); //修改标签内容
}
}
});
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
new PositionOfMouse();
}
}