码迷,mamicode.com
首页 > 编程语言 > 详细

【自动化__GUI自动化】__java__模拟功能操作__猴子测试

时间:2017-09-02 23:29:07      阅读:389      评论:0      收藏:0      [点我收藏+]

标签:err   time   ati   method   ring   键盘   keyboard   util   cat   

 

 

一、代码如下

package www.woniu.auto;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.io.IOException;
import java.util.Random;

public class MonkeyTest {
	Robot robot= null;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MonkeyTest monkey= new MonkeyTest();
		try {
			monkey.robot= new Robot();
			Runtime.getRuntime().exec("calc.exe");//IOException
			
			for(int i=0; i<100; i++) {
				monkey.move();
				Thread.sleep(1000); //InterruptedException
				monkey.mouse();
				Thread.sleep(1000);
				monkey.keyBoard();
				Thread.sleep(1000);
			}
			
		} catch (AWTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	
	//键盘随机输入
	private void keyBoard() {
		// TODO Auto-generated method stub
		int[] keys= {0x30, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39};
		int index= (int)(Math.random()*keys.length);
		this.robot.keyPress(keys[index]);
		this.robot.keyRelease(keys[index]);
	}
	
	private void mouse() {
		// TODO Auto-generated method stub
		int random= (int)(Math.random()*10);
		if(random >= 5) {
			this.robot.mousePress(InputEvent.BUTTON1_MASK);
			this.robot.mouseRelease(InputEvent.BUTTON1_MASK);
		}else {
			this.robot.mousePress(InputEvent.BUTTON3_MASK);
			this.robot.mouseRelease(InputEvent.BUTTON3_MASK);
		}

	}
	
	
	private void move() {
		// TODO Auto-generated method stub
		Random myrandom= new Random();
		int x= myrandom.nextInt(40);
		int y= myrandom.nextInt(40);
		robot.mouseMove(x, y);
	}
	

}

 

【自动化__GUI自动化】__java__模拟功能操作__猴子测试

标签:err   time   ati   method   ring   键盘   keyboard   util   cat   

原文地址:http://www.cnblogs.com/wujianbo123/p/7468092.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!